Lines Matching refs:self

67     def mode(self):
68 return self.prefix + self.suffix
96 def setUp(self):
97 self.tar = tarfile.open(self.tarname, mode=self.mode,
100 def tearDown(self):
101 self.tar.close()
106 def test_fileobj_regular_file(self):
107 tarinfo = self.tar.getmember("ustar/regtype")
108 with self.tar.extractfile(tarinfo) as fobj:
110 self.assertEqual(len(data), tarinfo.size,
112 self.assertEqual(sha256sum(data), sha256_regtype,
115 def test_fileobj_readlines(self):
116 self.tar.extract("ustar/regtype", TEMPDIR, filter='data')
117 tarinfo = self.tar.getmember("ustar/regtype")
121 with self.tar.extractfile(tarinfo) as fobj:
124 self.assertEqual(lines1, lines2,
126 self.assertEqual(len(lines2), 114,
128 self.assertEqual(lines2[83],
133 def test_fileobj_iter(self):
134 self.tar.extract("ustar/regtype", TEMPDIR, filter='data')
135 tarinfo = self.tar.getmember("ustar/regtype")
138 with self.tar.extractfile(tarinfo) as fobj2:
140 self.assertEqual(lines1, lines2,
143 def test_fileobj_seek(self):
144 self.tar.extract("ustar/regtype", TEMPDIR,
149 tarinfo = self.tar.getmember("ustar/regtype")
150 with self.tar.extractfile(tarinfo) as fobj:
153 self.assertEqual(0, fobj.tell(),
156 self.assertEqual(2048, fobj.tell(),
159 self.assertEqual(1024, fobj.tell(),
162 self.assertEqual(2048, fobj.tell(),
165 self.assertEqual(s, data[2048:2058],
168 self.assertEqual(tarinfo.size, fobj.tell(),
170 self.assertEqual(fobj.read(), b"",
173 self.assertEqual(0, fobj.tell(),
179 self.assertEqual(s1, s2,
182 self.assertEqual(len(fobj.readline()), fobj.tell(),
185 self.assertEqual(len(fobj.readline()) + 512, fobj.tell(),
189 self.assertEqual(fobj.read(), data[len(line):],
192 def test_fileobj_text(self):
193 with self.tar.extractfile("ustar/regtype") as fobj:
196 self.assertEqual(sha256sum(data), sha256_regtype)
202 self.fail("seeking failed in text mode")
207 def _test_fileobj_link(self, lnktype, regtype):
208 with self.tar.extractfile(lnktype) as a, \
209 self.tar.extractfile(regtype) as b:
210 self.assertEqual(a.name, b.name)
212 def test_fileobj_link1(self):
213 self._test_fileobj_link("ustar/lnktype", "ustar/regtype")
215 def test_fileobj_link2(self):
216 self._test_fileobj_link("./ustar/linktest2/lnktype",
219 def test_fileobj_symlink1(self):
220 self._test_fileobj_link("ustar/symtype", "ustar/regtype")
222 def test_fileobj_symlink2(self):
223 self._test_fileobj_link("./ustar/linktest2/symtype",
226 def test_issue14160(self):
227 self._test_fileobj_link("symtype2", "ustar/regtype")
229 def test_add_dir_getmember(self):
231 self.add_dir_and_getmember('bar')
232 self.add_dir_and_getmember('a'*101)
236 def add_dir_and_getmember(self, name):
250 self.assertEqual(
268 def setUp(self):
269 self.tar = tarfile.open(self.tarname, mode=self.mode)
271 def test_list(self):
274 self.tar.list(verbose=False)
276 self.assertIn(b'ustar/conttype', out)
277 self.assertIn(b'ustar/regtype', out)
278 self.assertIn(b'ustar/lnktype', out)
279 self.assertIn(b'ustar' + (b'/12345' * 40) + b'67/longname', out)
280 self.assertIn(b'./ustar/linktest2/symtype', out)
281 self.assertIn(b'./ustar/linktest2/lnktype', out)
283 self.assertIn(b'ustar/dirtype/', out)
284 self.assertIn(b'ustar/dirtype-with-size/', out)
287 s = b.decode(self.tar.encoding, 'surrogateescape')
289 self.assertIn(conv(b'ustar/umlauts-\xc4\xd6\xdc\xe4\xf6\xfc\xdf'), out)
290 self.assertIn(conv(b'misc/regtype-hpux-signed-chksum-'
292 self.assertIn(conv(b'misc/regtype-old-v7-signed-chksum-'
294 self.assertIn(conv(b'pax/bad-pax-\xe4\xf6\xfc'), out)
295 self.assertIn(conv(b'pax/hdrcharset-\xe4\xf6\xfc'), out)
302 self.assertRegex(out, br'ustar/conttype ?\r?\n'
305 self.assertNotIn(b'link to', out)
306 self.assertNotIn(b'->', out)
308 def test_list_verbose(self):
311 self.tar.list(verbose=True)
319 self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 '
323 self.assertIn(b'ustar/symtype -> regtype', out)
324 self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
325 self.assertIn(b'./ustar/linktest2/lnktype link to '
327 self.assertIn(b'gnu' + (b'/123' * 125) + b'/longlink link to gnu' +
329 self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' +
332 def test_list_members(self):
339 self.tar.list(verbose=False, members=members(self.tar))
341 self.assertIn(b'ustar/regtype', out)
342 self.assertNotIn(b'ustar/conttype', out)
359 def test_is_tarfile_erroneous(self):
364 self.assertFalse(tarfile.is_tarfile(tmpname))
367 self.assertFalse(tarfile.is_tarfile(pathlib.Path(tmpname)))
371 self.assertFalse(tarfile.is_tarfile(fobj))
374 self.assertFalse(tarfile.is_tarfile(io.BytesIO(b"invalid")))
376 def test_is_tarfile_valid(self):
378 self.assertTrue(tarfile.is_tarfile(self.tarname))
381 self.assertTrue(tarfile.is_tarfile(pathlib.Path(self.tarname)))
384 with open(self.tarname, "rb") as fobj:
385 self.assertTrue(tarfile.is_tarfile(fobj))
388 with open(self.tarname, "rb") as fobj:
389 self.assertTrue(tarfile.is_tarfile(io.BytesIO(fobj.read())))
391 def test_is_tarfile_keeps_position(self):
394 with open(self.tarname, "rb") as fobj:
396 self.assertEqual(fobj.tell(), 0)
398 with open(self.tarname, "rb") as fobj:
401 self.assertEqual(file_like.tell(), 0)
403 def test_empty_tarfile(self):
408 with tarfile.open(tmpname, self.mode.replace("r", "w")):
411 tar = tarfile.open(tmpname, self.mode)
414 self.fail("tarfile.open() failed on empty archive")
416 self.assertListEqual(tar.getmembers(), [])
420 def test_non_existent_tarfile(self):
423 with self.assertRaisesRegex(FileNotFoundError, "xxx"):
424 tarfile.open("xxx", self.mode)
426 def test_null_tarfile(self):
432 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, self.mode)
433 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname)
435 def test_ignore_zeros(self):
442 with self.open(tmpname, "w") as fobj:
451 self.assertListEqual(tar.getnames(), ["foo"],
457 def test_premature_end_of_archive(self):
468 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
475 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
478 with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"):
481 def test_length_zero_header(self):
484 with self.assertRaisesRegex(tarfile.ReadError, "file could not be opened successfully"):
489 def requires_name_attribute(self):
492 def test_no_name_argument(self):
493 self.requires_name_attribute()
494 with open(self.tarname, "rb") as fobj:
495 self.assertIsInstance(fobj.name, str)
496 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
497 self.assertIsInstance(tar.name, str)
498 self.assertEqual(tar.name, os.path.abspath(fobj.name))
500 def test_no_name_attribute(self):
501 with open(self.tarname, "rb") as fobj:
504 self.assertRaises(AttributeError, getattr, fobj, "name")
505 tar = tarfile.open(fileobj=fobj, mode=self.mode)
506 self.assertIsNone(tar.name)
508 def test_empty_name_attribute(self):
509 with open(self.tarname, "rb") as fobj:
513 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
514 self.assertIsNone(tar.name)
516 def test_int_name_attribute(self):
519 fd = os.open(self.tarname, os.O_RDONLY)
521 self.assertIsInstance(fobj.name, int)
522 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
523 self.assertIsNone(tar.name)
525 def test_bytes_name_attribute(self):
526 self.requires_name_attribute()
527 tarname = os.fsencode(self.tarname)
529 self.assertIsInstance(fobj.name, bytes)
530 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
531 self.assertIsInstance(tar.name, bytes)
532 self.assertEqual(tar.name, os.path.abspath(fobj.name))
534 def test_pathlike_name(self):
535 tarname = pathlib.Path(self.tarname)
536 with tarfile.open(tarname, mode=self.mode) as tar:
537 self.assertIsInstance(tar.name, str)
538 self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname)))
539 with self.taropen(tarname) as tar:
540 self.assertIsInstance(tar.name, str)
541 self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname)))
542 with tarfile.TarFile.open(tarname, mode=self.mode) as tar:
543 self.assertIsInstance(tar.name, str)
544 self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname)))
545 if self.suffix == '':
547 self.assertIsInstance(tar.name, str)
548 self.assertEqual(tar.name, os.path.abspath(os.fspath(tarname)))
550 def test_illegal_mode_arg(self):
553 with self.assertRaisesRegex(ValueError, 'mode must be '):
554 tar = self.taropen(tmpname, 'q')
555 with self.assertRaisesRegex(ValueError, 'mode must be '):
556 tar = self.taropen(tmpname, 'rw')
557 with self.assertRaisesRegex(ValueError, 'mode must be '):
558 tar = self.taropen(tmpname, '')
560 def test_fileobj_with_offset(self):
563 tar = tarfile.open(self.tarname, mode=self.mode)
575 with self.open(self.tarname) as fobj:
579 with tar.open(self.tarname, mode="r:", fileobj=fobj) as tar:
581 self.assertEqual(t.name, name)
585 self.assertEqual(tar.extractfile(t).read(), data,
588 def test_fail_comp(self):
590 self.assertRaises(tarfile.ReadError, tarfile.open, tarname, self.mode)
592 self.assertRaises(tarfile.ReadError, tarfile.open,
593 fileobj=fobj, mode=self.mode)
595 def test_v7_dirtype(self):
599 tarinfo = self.tar.getmember("misc/dirtype-old-v7")
600 self.assertEqual(tarinfo.type, tarfile.DIRTYPE,
603 def test_xstar_type(self):
608 self.tar.getmember("misc/regtype-xstar")
610 self.fail("failed to find misc/regtype-xstar (mangled prefix?)")
612 def test_check_members(self):
613 for tarinfo in self.tar:
614 self.assertEqual(int(tarinfo.mtime), 0o7606136617,
618 self.assertEqual(tarinfo.uname, "tarfile",
621 def test_find_members(self):
622 self.assertEqual(self.tar.getmembers()[-1].name, "misc/eof",
628 def test_extract_hardlink(self):
632 self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/regtype"))
635 self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/lnktype"))
638 self.assertEqual(sha256sum(data), sha256_regtype)
641 self.addCleanup(os_helper.unlink, os.path.join(TEMPDIR, "ustar/symtype"))
644 self.assertEqual(sha256sum(data), sha256_regtype)
647 def test_extractall(self):
660 self.assertEqual(tarinfo.mode & 0o777,
673 self.assertEqual(tarinfo.mtime, file_mtime, errmsg)
679 def test_extract_directory(self):
688 self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime)
690 self.assertEqual(os.stat(extracted).st_mode & 0o777, 0o755)
694 def test_extractall_pathlike_name(self):
702 self.assertEqual(os.path.getmtime(path), tarinfo.mtime)
704 def test_extract_pathlike_name(self):
712 self.assertEqual(os.path.getmtime(extracted), tarinfo.mtime)
714 def test_init_close_fobj(self):
727 self.assertTrue(tar.fileobj.closed)
729 self.fail("ReadError not raised")
733 def test_parallel_iteration(self):
736 with tarfile.open(self.tarname) as tar:
738 self.assertEqual(m1.offset, m2.offset)
739 self.assertEqual(m1.get_info(), m2.get_info())
742 def test_zlib_error_does_not_leak(self):
747 with self.assertRaises(tarfile.ReadError):
748 tarfile.open(self.tarname)
750 def test_next_on_empty_tarfile(self):
757 self.assertEqual(tf.next(), None)
761 self.assertEqual(tf.next(), None)
770 def requires_name_attribute(self):
771 self.skipTest("BZ2File have no name attribute")
774 def requires_name_attribute(self):
775 self.skipTest("LZMAFile have no name attribute")
782 def test_read_through(self):
785 for tarinfo in self.tar:
788 with self.tar.extractfile(tarinfo) as fobj:
793 self.fail("simple read-through using "
798 def test_fileobj_regular_file(self):
799 tarinfo = self.tar.next() # get "regtype" (can't use getmember)
800 with self.tar.extractfile(tarinfo) as fobj:
802 self.assertEqual(len(data), tarinfo.size,
804 self.assertEqual(sha256sum(data), sha256_regtype,
807 def test_provoke_stream_error(self):
808 tarinfos = self.tar.getmembers()
809 with self.tar.extractfile(tarinfos[0]) as f: # read the first member
810 self.assertRaises(tarfile.StreamError, f.read)
812 def test_compare_members(self):
815 tar2 = self.tar
822 self.assertIsNotNone(t2, "stream.next() failed.")
825 with self.assertRaises(tarfile.StreamError):
833 self.assertIsNotNone(v2, "stream.extractfile() failed")
834 self.assertEqual(v1.read(), v2.read(),
850 def _testfunc_file(self, name, mode):
854 self.fail()
858 def _testfunc_fileobj(self, name, mode):
863 self.fail()
867 def _test_modes(self, testfunc):
868 if self.suffix:
869 with self.assertRaises(tarfile.ReadError):
870 tarfile.open(tarname, mode="r:" + self.suffix)
871 with self.assertRaises(tarfile.ReadError):
872 tarfile.open(tarname, mode="r|" + self.suffix)
873 with self.assertRaises(tarfile.ReadError):
874 tarfile.open(self.tarname, mode="r:")
875 with self.assertRaises(tarfile.ReadError):
876 tarfile.open(self.tarname, mode="r|")
877 testfunc(self.tarname, "r")
878 testfunc(self.tarname, "r:" + self.suffix)
879 testfunc(self.tarname, "r:*")
880 testfunc(self.tarname, "r|" + self.suffix)
881 testfunc(self.tarname, "r|*")
883 def test_detect_file(self):
884 self._test_modes(self._testfunc_file)
886 def test_detect_fileobj(self):
887 self._test_modes(self._testfunc_fileobj)
893 def test_detect_stream_bz2(self):
905 self._testfunc_file(tmpname, "r|*")
913 def _test_member(self, tarinfo, chksum=None, **kwargs):
915 with self.tar.extractfile(tarinfo) as f:
916 self.assertEqual(sha256sum(f.read()), chksum,
927 self.assertEqual(getattr(tarinfo, k), v,
930 def test_find_regtype(self):
931 tarinfo = self.tar.getmember("ustar/regtype")
932 self._test_member(tarinfo, size=7011, chksum=sha256_regtype)
934 def test_find_conttype(self):
935 tarinfo = self.tar.getmember("ustar/conttype")
936 self._test_member(tarinfo, size=7011, chksum=sha256_regtype)
938 def test_find_dirtype(self):
939 tarinfo = self.tar.getmember("ustar/dirtype")
940 self._test_member(tarinfo, size=0)
942 def test_find_dirtype_with_size(self):
943 tarinfo = self.tar.getmember("ustar/dirtype-with-size")
944 self._test_member(tarinfo, size=255)
946 def test_find_lnktype(self):
947 tarinfo = self.tar.getmember("ustar/lnktype")
948 self._test_member(tarinfo, size=0, linkname="ustar/regtype")
950 def test_find_symtype(self):
951 tarinfo = self.tar.getmember("ustar/symtype")
952 self._test_member(tarinfo, size=0, linkname="regtype")
954 def test_find_blktype(self):
955 tarinfo = self.tar.getmember("ustar/blktype")
956 self._test_member(tarinfo, size=0, devmajor=3, devminor=0)
958 def test_find_chrtype(self):
959 tarinfo = self.tar.getmember("ustar/chrtype")
960 self._test_member(tarinfo, size=0, devmajor=1, devminor=3)
962 def test_find_fifotype(self):
963 tarinfo = self.tar.getmember("ustar/fifotype")
964 self._test_member(tarinfo, size=0)
966 def test_find_sparse(self):
967 tarinfo = self.tar.getmember("ustar/sparse")
968 self._test_member(tarinfo, size=86016, chksum=sha256_sparse)
970 def test_find_gnusparse(self):
971 tarinfo = self.tar.getmember("gnu/sparse")
972 self._test_member(tarinfo, size=86016, chksum=sha256_sparse)
974 def test_find_gnusparse_00(self):
975 tarinfo = self.tar.getmember("gnu/sparse-0.0")
976 self._test_member(tarinfo, size=86016, chksum=sha256_sparse)
978 def test_find_gnusparse_01(self):
979 tarinfo = self.tar.getmember("gnu/sparse-0.1")
980 self._test_member(tarinfo, size=86016, chksum=sha256_sparse)
982 def test_find_gnusparse_10(self):
983 tarinfo = self.tar.getmember("gnu/sparse-1.0")
984 self._test_member(tarinfo, size=86016, chksum=sha256_sparse)
986 def test_find_umlauts(self):
987 tarinfo = self.tar.getmember("ustar/umlauts-"
989 self._test_member(tarinfo, size=7011, chksum=sha256_regtype)
991 def test_find_ustar_longname(self):
993 self.assertIn(name, self.tar.getnames())
995 def test_find_regtype_oldv7(self):
996 tarinfo = self.tar.getmember("misc/regtype-old-v7")
997 self._test_member(tarinfo, size=7011, chksum=sha256_regtype)
999 def test_find_pax_umlauts(self):
1000 self.tar.close()
1001 self.tar = tarfile.open(self.tarname, mode=self.mode,
1003 tarinfo = self.tar.getmember("pax/umlauts-"
1005 self._test_member(tarinfo, size=7011, chksum=sha256_regtype)
1010 def test_read_longname(self):
1012 longname = self.subdir + "/" + "123/" * 125 + "longname"
1014 tarinfo = self.tar.getmember(longname)
1016 self.fail("longname not found")
1017 self.assertNotEqual(tarinfo.type, tarfile.DIRTYPE,
1020 def test_read_longlink(self):
1021 longname = self.subdir + "/" + "123/" * 125 + "longname"
1022 longlink = self.subdir + "/" + "123/" * 125 + "longlink"
1024 tarinfo = self.tar.getmember(longlink)
1026 self.fail("longlink not found")
1027 self.assertEqual(tarinfo.linkname, longname, "linkname wrong")
1029 def test_truncated_longname(self):
1030 longname = self.subdir + "/" + "123/" * 125 + "longname"
1031 tarinfo = self.tar.getmember(longname)
1033 self.tar.fileobj.seek(offset)
1034 fobj = io.BytesIO(self.tar.fileobj.read(3 * 512))
1035 with self.assertRaises(tarfile.ReadError):
1038 def test_header_offset(self):
1041 longname = self.subdir + "/" + "123/" * 125 + "longname"
1042 offset = self.tar.getmember(longname).offset
1047 self.assertEqual(tarinfo.type, self.longnametype)
1049 def test_longname_directory(self):
1054 tar.format = self.format
1061 self.assertIsNotNone(tar.getmember(longdir))
1062 self.assertIsNotNone(tar.getmember(longdir.removesuffix('/')))
1077 def _test_sparse_file(self, name):
1078 self.tar.extract(name, TEMPDIR, filter='data')
1082 self.assertEqual(sha256sum(data), sha256_sparse,
1085 if self._fs_supports_holes():
1087 self.assertLess(s.st_blocks * 512, s.st_size)
1089 def test_sparse_file_old(self):
1090 self._test_sparse_file("gnu/sparse")
1092 def test_sparse_file_00(self):
1093 self._test_sparse_file("gnu/sparse-0.0")
1095 def test_sparse_file_01(self):
1096 self._test_sparse_file("gnu/sparse-0.1")
1098 def test_sparse_file_10(self):
1099 self._test_sparse_file("gnu/sparse-1.0")
1130 def test_pax_global_headers(self):
1134 self.assertEqual(tarinfo.uname, "foo")
1135 self.assertEqual(tarinfo.gname, "bar")
1136 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
1140 self.assertEqual(tarinfo.uname, "")
1141 self.assertEqual(tarinfo.gname, "bar")
1142 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
1146 self.assertEqual(tarinfo.uname, "tarfile")
1147 self.assertEqual(tarinfo.gname, "tarfile")
1148 self.assertEqual(tarinfo.pax_headers.get("VENDOR.umlauts"),
1153 def test_pax_number_fields(self):
1158 self.assertEqual(tarinfo.size, 7011)
1159 self.assertEqual(tarinfo.uid, 123)
1160 self.assertEqual(tarinfo.gid, 123)
1161 self.assertEqual(tarinfo.mtime, 1041808783.0)
1162 self.assertEqual(type(tarinfo.mtime), float)
1163 self.assertEqual(float(tarinfo.pax_headers["atime"]), 1041808783.0)
1164 self.assertEqual(float(tarinfo.pax_headers["ctime"]), 1041808783.0)
1168 def test_pax_header_bad_formats(self):
1186 with self.subTest(header=replacement):
1200 self.assertIn(b"11 foo=bar\n", data)
1207 with self.assertRaisesRegex(tarfile.ReadError, r"method tar: ReadError\('invalid header'\)"):
1215 def test_fileobj_no_close(self):
1217 with tarfile.open(fileobj=fobj, mode=self.mode) as tar:
1219 self.assertFalse(fobj.closed, "external fileobjs must never closed")
1224 self.assertFalse(fobj.closed)
1225 self.assertEqual(data, fobj.getvalue())
1227 def test_eof_marker(self):
1232 with tarfile.open(tmpname, self.mode) as tar:
1237 with self.open(tmpname, "rb") as fobj:
1238 self.assertEqual(len(fobj.read()), tarfile.RECORDSIZE * 2)
1245 def test_100_char_name(self):
1251 tar = tarfile.open(tmpname, self.mode)
1260 self.assertEqual(tar.getnames()[0], name,
1265 def test_tar_size(self):
1267 tar = tarfile.open(tmpname, self.mode)
1275 self.assertGreater(os.path.getsize(tmpname), 0,
1279 def test_file_size(self):
1280 tar = tarfile.open(tmpname, self.mode)
1286 self.assertEqual(tarinfo.size, 0)
1291 self.assertEqual(tarinfo.size, 3)
1295 def test_directory_size(self):
1299 tar = tarfile.open(tmpname, self.mode)
1302 self.assertEqual(tarinfo.size, 0)
1310 def test_ordered_recursion(self):
1316 tar = tarfile.open(tmpname, self.mode)
1324 self.assertEqual(paths, ["directory", "1", "2"]);
1332 def test_gettarinfo_pathlike_name(self):
1333 with tarfile.open(tmpname, self.mode) as tar:
1339 self.assertIsInstance(tarinfo.name, str)
1340 self.assertEqual(tarinfo.name, tarinfo2.name)
1341 self.assertEqual(tarinfo.size, 3)
1345 def test_link_size(self):
1353 self.skipTest('os.link(): %s' % e)
1355 tar = tarfile.open(tmpname, self.mode)
1360 self.assertEqual(tarinfo.size, 0)
1368 def test_symlink_size(self):
1372 tar = tarfile.open(tmpname, self.mode)
1375 self.assertEqual(tarinfo.size, 0)
1381 def test_add_self(self):
1384 tar = tarfile.open(tmpname, self.mode)
1386 self.assertEqual(tar.name, dstname,
1389 self.assertEqual(tar.getnames(), [],
1394 self.assertEqual(tar.getnames(), [],
1399 def test_filter(self):
1414 tar = tarfile.open(tmpname, self.mode, encoding="iso8859-1")
1421 with self.assertRaises(TypeError):
1427 self.assertEqual(tarinfo.uid, 123)
1428 self.assertEqual(tarinfo.uname, "foo")
1429 self.assertEqual(len(tar.getmembers()), 3)
1439 def _test_pathname(self, path, cmp_path=None, dir=False):
1448 tar = tarfile.open(tmpname, self.mode)
1465 self.assertEqual(t.name, cmp_path or path.replace(os.sep, "/"))
1469 def test_extractall_symlinks(self):
1490 self.fail("extractall failed with symlinked files")
1495 def test_pathnames(self):
1496 self._test_pathname("foo")
1497 self._test_pathname(os.path.join("foo", ".", "bar"))
1498 self._test_pathname(os.path.join("foo", "..", "bar"))
1499 self._test_pathname(os.path.join(".", "foo"))
1500 self._test_pathname(os.path.join(".", "foo", "."))
1501 self._test_pathname(os.path.join(".", "foo", ".", "bar"))
1502 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1503 self._test_pathname(os.path.join(".", "foo", "..", "bar"))
1504 self._test_pathname(os.path.join("..", "foo"))
1505 self._test_pathname(os.path.join("..", "foo", ".."))
1506 self._test_pathname(os.path.join("..", "foo", ".", "bar"))
1507 self._test_pathname(os.path.join("..", "foo", "..", "bar"))
1509 self._test_pathname("foo" + os.sep + os.sep + "bar")
1510 self._test_pathname("foo" + os.sep + os.sep, "foo", dir=True)
1512 def test_abs_pathnames(self):
1514 self._test_pathname("C:\\foo", "foo")
1516 self._test_pathname("/foo", "foo")
1517 self._test_pathname("///foo", "foo")
1519 def test_cwd(self):
1522 tar = tarfile.open(tmpname, self.mode)
1532 self.assertTrue(t.name.startswith("./"), t.name)
1536 def test_open_nonwritable_fileobj(self):
1540 def write(self, data):
1541 if self.first:
1542 self.first = False
1546 with self.assertRaises(exctype):
1547 tar = tarfile.open(tmpname, self.mode, fileobj=f,
1550 self.assertFalse(f.closed)
1570 def test_stream_padding(self):
1572 tar = tarfile.open(tmpname, self.mode)
1574 if self.decompressor:
1575 dec = self.decompressor()
1579 self.assertFalse(dec.unused_data, "found trailing data")
1581 with self.open(tmpname) as fobj:
1583 self.assertEqual(data.count(b"\0"), tarfile.RECORDSIZE,
1592 def test_file_mode(self):
1600 tar = tarfile.open(tmpname, self.mode)
1603 self.assertEqual(mode, 0o644, "wrong file permissions")
1609 def test_source_directory_not_leaked(self):
1614 tarfile.open(tmpname, self.mode).close()
1630 def _length(self, s):
1634 def _calc_size(self, name, link=None):
1641 count += self._length(name)
1645 count += self._length(link)
1648 def _test(self, name, link=None):
1659 v1 = self._calc_size(name, link)
1661 self.assertEqual(v1, v2, "GNU longname/longlink creation failed")
1668 self.assertIsNotNone(member,
1670 self.assertEqual(tarinfo.name, member.name,
1672 self.assertEqual(tarinfo.linkname, member.linkname,
1677 def test_longname_1023(self):
1678 self._test(("longnam/" * 127) + "longnam")
1680 def test_longname_1024(self):
1681 self._test(("longnam/" * 127) + "longname")
1683 def test_longname_1025(self):
1684 self._test(("longnam/" * 127) + "longname_")
1686 def test_longlink_1023(self):
1687 self._test("name", ("longlnk/" * 127) + "longlnk")
1689 def test_longlink_1024(self):
1690 self._test("name", ("longlnk/" * 127) + "longlink")
1692 def test_longlink_1025(self):
1693 self._test("name", ("longlnk/" * 127) + "longlink_")
1695 def test_longnamelink_1023(self):
1696 self._test(("longnam/" * 127) + "longnam",
1699 def test_longnamelink_1024(self):
1700 self._test(("longnam/" * 127) + "longname",
1703 def test_longnamelink_1025(self):
1704 self._test(("longnam/" * 127) + "longname_",
1712 def test_headers_written_only_for_device_files(self):
1717 tar = tarfile.open(tmpname, self.mode)
1735 self.assertEqual(output_blk.devmajor, 0)
1736 self.assertEqual(output_blk.devminor, 0)
1737 self.assertEqual(output_reg.devmajor, 0)
1738 self.assertEqual(output_reg.devminor, 0)
1748 self.assertEqual(buf_blk[device_headers], b"0000000\0" * 2)
1749 self.assertEqual(buf_reg[device_headers], b"\0" * 16)
1760 def setUp(self):
1772 def test_create(self):
1773 with tarfile.open(tmpname, self.mode) as tobj:
1774 tobj.add(self.file_path)
1776 with self.taropen(tmpname) as tobj:
1778 self.assertEqual(len(names), 1)
1779 self.assertIn('spameggs42', names[0])
1781 def test_create_existing(self):
1782 with tarfile.open(tmpname, self.mode) as tobj:
1783 tobj.add(self.file_path)
1785 with self.assertRaises(FileExistsError):
1786 tobj = tarfile.open(tmpname, self.mode)
1788 with self.taropen(tmpname) as tobj:
1790 self.assertEqual(len(names), 1)
1791 self.assertIn('spameggs42', names[0])
1793 def test_create_taropen(self):
1794 with self.taropen(tmpname, "x") as tobj:
1795 tobj.add(self.file_path)
1797 with self.taropen(tmpname) as tobj:
1799 self.assertEqual(len(names), 1)
1800 self.assertIn('spameggs42', names[0])
1802 def test_create_existing_taropen(self):
1803 with self.taropen(tmpname, "x") as tobj:
1804 tobj.add(self.file_path)
1806 with self.assertRaises(FileExistsError):
1807 with self.taropen(tmpname, "x"):
1810 with self.taropen(tmpname) as tobj:
1812 self.assertEqual(len(names), 1)
1813 self.assertIn("spameggs42", names[0])
1815 def test_create_pathlike_name(self):
1816 with tarfile.open(pathlib.Path(tmpname), self.mode) as tobj:
1817 self.assertIsInstance(tobj.name, str)
1818 self.assertEqual(tobj.name, os.path.abspath(tmpname))
1819 tobj.add(pathlib.Path(self.file_path))
1821 self.assertEqual(len(names), 1)
1822 self.assertIn('spameggs42', names[0])
1824 with self.taropen(tmpname) as tobj:
1826 self.assertEqual(len(names), 1)
1827 self.assertIn('spameggs42', names[0])
1829 def test_create_taropen_pathlike_name(self):
1830 with self.taropen(pathlib.Path(tmpname), "x") as tobj:
1831 self.assertIsInstance(tobj.name, str)
1832 self.assertEqual(tobj.name, os.path.abspath(tmpname))
1833 tobj.add(pathlib.Path(self.file_path))
1835 self.assertEqual(len(names), 1)
1836 self.assertIn('spameggs42', names[0])
1838 with self.taropen(tmpname) as tobj:
1840 self.assertEqual(len(names), 1)
1841 self.assertIn('spameggs42', names[0])
1846 def test_create_with_compresslevel(self):
1847 with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
1848 tobj.add(self.file_path)
1855 def test_create_with_compresslevel(self):
1856 with tarfile.open(tmpname, self.mode, compresslevel=1) as tobj:
1857 tobj.add(self.file_path)
1866 def test_create_with_preset(self):
1867 with tarfile.open(tmpname, self.mode, preset=1) as tobj:
1868 tobj.add(self.file_path)
1883 def setUp(self):
1884 self.foo = os.path.join(TEMPDIR, "foo")
1885 self.bar = os.path.join(TEMPDIR, "bar")
1887 with open(self.foo, "wb") as fobj:
1891 os.link(self.foo, self.bar)
1893 self.skipTest('os.link(): %s' % e)
1895 self.tar = tarfile.open(tmpname, "w")
1896 self.tar.add(self.foo)
1898 def tearDown(self):
1899 self.tar.close()
1900 os_helper.unlink(self.foo)
1901 os_helper.unlink(self.bar)
1903 def test_add_twice(self):
1906 tarinfo = self.tar.gettarinfo(self.foo)
1907 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
1910 def test_add_hardlink(self):
1911 tarinfo = self.tar.gettarinfo(self.bar)
1912 self.assertEqual(tarinfo.type, tarfile.LNKTYPE,
1915 def test_dereference_hardlink(self):
1916 self.tar.dereference = True
1917 tarinfo = self.tar.gettarinfo(self.bar)
1918 self.assertEqual(tarinfo.type, tarfile.REGTYPE,
1924 def _test(self, name, link=None):
1941 self.assertEqual(link, l, "PAX longlink creation failed")
1944 self.assertEqual(name, n, "PAX longname creation failed")
1948 def test_pax_global_header(self):
1966 self.assertEqual(tar.pax_headers, pax_headers)
1967 self.assertEqual(tar.getmembers()[0].pax_headers, pax_headers)
1970 self.assertIsNot(type(key), bytes)
1971 self.assertIsNot(type(val), bytes)
1976 self.fail("unable to convert pax header field")
1980 def test_pax_extended_header(self):
1999 self.assertEqual(t.pax_headers, pax_headers)
2000 self.assertEqual(t.name, "foo")
2001 self.assertEqual(t.uid, 123)
2005 def test_create_pax_header(self):
2020 self.assertEqual(info['name'], "foo")
2022 self.assertIsInstance(info['mtime'], int)
2023 self.assertEqual(info['mtime'], 1000)
2024 self.assertEqual(info['size'], 100)
2025 self.assertEqual(info['uid'], 123)
2026 self.assertEqual(info['gid'], 124)
2027 self.assertEqual(header,
2045 self.assertEqual(info['name'], "foo\u3374")
2046 self.assertEqual(info['mtime'], 0)
2047 self.assertEqual(info['size'], 0)
2048 self.assertEqual(info['uid'], 0)
2049 self.assertEqual(info['gid'], 0)
2050 self.assertEqual(header,
2063 def test_iso8859_1_filename(self):
2064 self._test_unicode_filename("iso8859-1")
2066 def test_utf7_filename(self):
2067 self._test_unicode_filename("utf7")
2069 def test_utf8_filename(self):
2070 self._test_unicode_filename("utf-8")
2072 def _test_unicode_filename(self, encoding):
2073 tar = tarfile.open(tmpname, "w", format=self.format,
2083 self.assertEqual(tar.getmembers()[0].name, name)
2087 def test_unicode_filename_error(self):
2088 tar = tarfile.open(tmpname, "w", format=self.format,
2094 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
2098 self.assertRaises(UnicodeError, tar.addfile, tarinfo)
2102 def test_unicode_argument(self):
2107 self.assertIs(type(t.name), str)
2108 self.assertIs(type(t.linkname), str)
2109 self.assertIs(type(t.uname), str)
2110 self.assertIs(type(t.gname), str)
2114 def test_uname_unicode(self):
2119 tar = tarfile.open(tmpname, mode="w", format=self.format,
2129 self.assertEqual(t.uname, "\xe4\xf6\xfc")
2130 self.assertEqual(t.gname, "\xe4\xf6\xfc")
2132 if self.format != tarfile.PAX_FORMAT:
2136 self.assertEqual(t.uname, "\udce4\udcf6\udcfc")
2137 self.assertEqual(t.gname, "\udce4\udcf6\udcfc")
2149 def test_unicode_name1(self):
2150 self._test_ustar_name("0123456789" * 10)
2151 self._test_ustar_name("0123456789" * 10 + "0", ValueError)
2152 self._test_ustar_name("0123456789" * 9 + "01234567\xff")
2153 self._test_ustar_name("0123456789" * 9 + "012345678\xff", ValueError)
2155 def test_unicode_name2(self):
2156 self._test_ustar_name("0123456789" * 9 + "012345\xff\xff")
2157 self._test_ustar_name("0123456789" * 9 + "0123456\xff\xff", ValueError)
2161 def test_unicode_longname1(self):
2162 self._test_ustar_name("0123456789" * 15 + "01234/" + "0123456789" * 10)
2163 self._test_ustar_name("0123456789" * 15 + "0123/4" + "0123456789" * 10, ValueError)
2164 self._test_ustar_name("0123456789" * 15 + "012\xff/" + "0123456789" * 10)
2165 self._test_ustar_name("0123456789" * 15 + "0123\xff/" + "0123456789" * 10, ValueError)
2167 def test_unicode_longname2(self):
2168 self._test_ustar_name("0123456789" * 15 + "01\xff/2" + "0123456789" * 10, ValueError)
2169 self._test_ustar_name("0123456789" * 15 + "01\xff\xff/" + "0123456789" * 10, ValueError)
2171 def test_unicode_longname3(self):
2172 self._test_ustar_name("0123456789" * 15 + "01\xff\xff/2" + "0123456789" * 10, ValueError)
2173 self._test_ustar_name("0123456789" * 15 + "01234/" + "0123456789" * 9 + "01234567\xff")
2174 self._test_ustar_name("0123456789" * 15 + "01234/" + "0123456789" * 9 + "012345678\xff", ValueError)
2176 def test_unicode_longname4(self):
2177 self._test_ustar_name("0123456789" * 15 + "01234/" + "0123456789" * 9 + "012345\xff\xff")
2178 self._test_ustar_name("0123456789" * 15 + "01234/" + "0123456789" * 9 + "0123456\xff\xff", ValueError)
2180 def _test_ustar_name(self, name, exc=None):
2181 with tarfile.open(tmpname, "w", format=self.format, encoding="utf-8") as tar:
2186 self.assertRaises(exc, tar.addfile, t)
2191 self.assertEqual(name, t.name)
2195 def test_unicode_link1(self):
2196 self._test_ustar_link("0123456789" * 10)
2197 self._test_ustar_link("0123456789" * 10 + "0", ValueError)
2198 self._test_ustar_link("0123456789" * 9 + "01234567\xff")
2199 self._test_ustar_link("0123456789" * 9 + "012345678\xff", ValueError)
2201 def test_unicode_link2(self):
2202 self._test_ustar_link("0123456789" * 9 + "012345\xff\xff")
2203 self._test_ustar_link("0123456789" * 9 + "0123456\xff\xff", ValueError)
2205 def _test_ustar_link(self, name, exc=None):
2206 with tarfile.open(tmpname, "w", format=self.format, encoding="utf-8") as tar:
2212 self.assertRaises(exc, tar.addfile, t)
2217 self.assertEqual(name, t.linkname)
2225 def test_bad_pax_header(self):
2236 self.fail("unable to read bad GNU tar pax header")
2246 def test_binary_header(self):
2256 self.fail("unable to read POSIX.1-2008 binary header")
2262 def setUp(self):
2263 self.tarname = tmpname
2264 if os.path.exists(self.tarname):
2265 os_helper.unlink(self.tarname)
2267 def _create_testtar(self, mode="w:"):
2272 with tarfile.open(self.tarname, mode) as tar:
2275 def test_append_compressed(self):
2276 self._create_testtar("w:" + self.suffix)
2277 self.assertRaises(tarfile.ReadError, tarfile.open, tmpname, "a")
2282 def _add_testfile(self, fileobj=None):
2283 with tarfile.open(self.tarname, "a", fileobj=fileobj) as tar:
2286 def _test(self, names=["bar"], fileobj=None):
2287 with tarfile.open(self.tarname, fileobj=fileobj) as tar:
2288 self.assertEqual(tar.getnames(), names)
2290 def test_non_existing(self):
2291 self._add_testfile()
2292 self._test()
2294 def test_empty(self):
2295 tarfile.open(self.tarname, "w:").close()
2296 self._add_testfile()
2297 self._test()
2299 def test_empty_fileobj(self):
2301 self._add_testfile(fobj)
2303 self._test(fileobj=fobj)
2305 def test_fileobj(self):
2306 self._create_testtar()
2307 with open(self.tarname, "rb") as fobj:
2310 self._add_testfile(fobj)
2312 self._test(names=["foo", "bar"], fileobj=fobj)
2314 def test_existing(self):
2315 self._create_testtar()
2316 self._add_testfile()
2317 self._test(names=["foo", "bar"])
2321 def _test_error(self, data):
2322 with open(self.tarname, "wb") as fobj:
2324 self.assertRaises(tarfile.ReadError, self._add_testfile)
2326 def test_null(self):
2327 self._test_error(b"")
2329 def test_incomplete(self):
2330 self._test_error(b"\0" * 13)
2332 def test_premature_eof(self):
2334 self._test_error(data)
2336 def test_trailing_garbage(self):
2338 self._test_error(data + b"\0" * 13)
2340 def test_invalid(self):
2341 self._test_error(b"a" * 512)
2355 def test_ustar_limits(self):
2362 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
2370 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
2374 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
2379 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
2384 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.USTAR_FORMAT)
2386 def test_gnu_limits(self):
2397 self.assertRaises(ValueError, tarinfo.tobuf, tarfile.GNU_FORMAT)
2399 def test_pax_limits(self):
2414 def test_char_fields(self):
2415 self.assertEqual(tarfile.stn("foo", 8, "ascii", "strict"),
2417 self.assertEqual(tarfile.stn("foobar", 3, "ascii", "strict"),
2419 self.assertEqual(tarfile.nts(b"foo\0\0\0\0\0", "ascii", "strict"),
2421 self.assertEqual(tarfile.nts(b"foo\0bar\0", "ascii", "strict"),
2424 def test_read_number_fields(self):
2427 self.assertEqual(tarfile.nti(b"0000001\x00"), 1)
2428 self.assertEqual(tarfile.nti(b"7777777\x00"), 0o7777777)
2429 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\x00\x20\x00\x00"),
2431 self.assertEqual(tarfile.nti(b"\x80\x00\x00\x00\xff\xff\xff\xff"),
2433 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\xff"),
2435 self.assertEqual(tarfile.nti(b"\xff\xff\xff\xff\xff\xff\xff\x9c"),
2437 self.assertEqual(tarfile.nti(b"\xff\x00\x00\x00\x00\x00\x00\x00"),
2441 self.assertEqual(tarfile.nti(b"\0"), 0)
2442 self.assertEqual(tarfile.nti(b" \0"), 0)
2444 def test_write_number_fields(self):
2445 self.assertEqual(tarfile.itn(1), b"0000001\x00")
2446 self.assertEqual(tarfile.itn(0o7777777), b"7777777\x00")
2447 self.assertEqual(tarfile.itn(0o10000000, format=tarfile.GNU_FORMAT),
2449 self.assertEqual(tarfile.itn(0xffffffff, format=tarfile.GNU_FORMAT),
2451 self.assertEqual(tarfile.itn(-1, format=tarfile.GNU_FORMAT),
2453 self.assertEqual(tarfile.itn(-100, format=tarfile.GNU_FORMAT),
2455 self.assertEqual(tarfile.itn(-0x100000000000000,
2461 self.assertEqual(tarfile.itn(-100.0, format=tarfile.GNU_FORMAT),
2463 self.assertEqual(tarfile.itn(8 ** 12 + 0.0, format=tarfile.GNU_FORMAT),
2465 self.assertEqual(tarfile.nti(tarfile.itn(-0.1, format=tarfile.GNU_FORMAT)), 0)
2467 def test_number_field_limits(self):
2468 with self.assertRaises(ValueError):
2470 with self.assertRaises(ValueError):
2472 with self.assertRaises(ValueError):
2474 with self.assertRaises(ValueError):
2477 def test__all__(self):
2495 support.check__all__(self, tarfile, not_exported=not_exported)
2497 def test_useful_error_message_when_modules_missing(self):
2499 with self.assertRaises(tarfile.ReadError) as excinfo:
2504 self.assertIn(
2512 def tarfilecmd(self, *args, **kwargs):
2517 def tarfilecmd_failure(self, *args):
2520 def make_simple_tarfile(self, tar_name):
2524 self.addCleanup(os_helper.unlink, tar_name)
2529 def make_evil_tarfile(self, tar_name):
2531 self.addCleanup(os_helper.unlink, tar_name)
2538 def test_bad_use(self):
2539 rc, out, err = self.tarfilecmd_failure()
2540 self.assertEqual(out, b'')
2541 self.assertIn(b'usage', err.lower())
2542 self.assertIn(b'error', err.lower())
2543 self.assertIn(b'required', err.lower())
2544 rc, out, err = self.tarfilecmd_failure('-l', '')
2545 self.assertEqual(out, b'')
2546 self.assertNotEqual(err.strip(), b'')
2548 def test_test_command(self):
2551 out = self.tarfilecmd(opt, tar_name)
2552 self.assertEqual(out, b'')
2554 def test_test_command_verbose(self):
2557 out = self.tarfilecmd(opt, '-t', tar_name,
2559 self.assertIn(b'is a tar archive.\n', out)
2561 def test_test_command_invalid_file(self):
2563 rc, out, err = self.tarfilecmd_failure('-t', zipname)
2564 self.assertIn(b' is not a tar archive.', err)
2565 self.assertEqual(out, b'')
2566 self.assertEqual(rc, 1)
2569 with self.subTest(tar_name=tar_name):
2575 rc, out, err = self.tarfilecmd_failure('-t', tmpname)
2576 self.assertEqual(out, b'')
2577 self.assertEqual(rc, 1)
2581 def test_list_command(self):
2588 out = self.tarfilecmd(opt, tar_name,
2590 self.assertEqual(out, expected)
2592 def test_list_command_verbose(self):
2599 out = self.tarfilecmd(opt, '-l', tar_name,
2601 self.assertEqual(out, expected)
2603 def test_list_command_invalid_file(self):
2605 rc, out, err = self.tarfilecmd_failure('-l', zipname)
2606 self.assertIn(b' is not a tar archive.', err)
2607 self.assertEqual(out, b'')
2608 self.assertEqual(rc, 1)
2610 def test_create_command(self):
2616 out = self.tarfilecmd(opt, tmpname, *files)
2617 self.assertEqual(out, b'')
2623 def test_create_command_verbose(self):
2629 out = self.tarfilecmd(opt, '-c', tmpname, *files,
2631 self.assertIn(b' file created.', out)
2637 def test_create_command_dotless_filename(self):
2640 out = self.tarfilecmd('-c', dotlessname, *files)
2641 self.assertEqual(out, b'')
2647 def test_create_command_dot_started_filename(self):
2651 out = self.tarfilecmd('-c', tar_name, *files)
2652 self.assertEqual(out, b'')
2658 def test_create_command_compressed(self):
2667 out = self.tarfilecmd('-c', tar_name, *files)
2673 def test_extract_command(self):
2674 self.make_simple_tarfile(tmpname)
2678 out = self.tarfilecmd(opt, tmpname)
2679 self.assertEqual(out, b'')
2683 def test_extract_command_verbose(self):
2684 self.make_simple_tarfile(tmpname)
2688 out = self.tarfilecmd(opt, '-e', tmpname,
2690 self.assertIn(b' file is extracted.', out)
2694 def test_extract_command_filter(self):
2695 self.make_evil_tarfile(tmpname)
2702 self.tarfilecmd_failure('-e', tmpname,
2705 out = self.tarfilecmd('-e', tmpname,
2709 self.assertIn(b' file is extracted.', out)
2713 def test_extract_command_different_directory(self):
2714 self.make_simple_tarfile(tmpname)
2717 out = self.tarfilecmd('-e', tmpname, 'spamdir')
2718 self.assertEqual(out, b'')
2722 def test_extract_command_invalid_file(self):
2725 rc, out, err = self.tarfilecmd_failure('-e', zipname)
2726 self.assertIn(b' is not a tar archive.', err)
2727 self.assertEqual(out, b'')
2728 self.assertEqual(rc, 1)
2733 def test_basic(self):
2735 self.assertFalse(tar.closed, "closed inside runtime context")
2736 self.assertTrue(tar.closed, "context manager failed")
2738 def test_closed(self):
2743 with self.assertRaises(OSError):
2747 def test_exception(self):
2749 with self.assertRaises(Exception) as exc:
2752 self.assertIsInstance(exc.exception, OSError,
2754 self.assertTrue(tar.closed, "context manager failed")
2756 def test_no_eof(self):
2764 self.assertEqual(os.path.getsize(tmpname), 0,
2766 self.assertTrue(tar.closed, "context manager failed")
2768 def test_eof(self):
2773 self.assertNotEqual(os.path.getsize(tmpname), 0,
2776 def test_fileobj(self):
2785 self.assertFalse(fobj.closed, "external file object was closed")
2786 self.assertTrue(tar.closed, "context manager failed")
2795 def _test_link_extraction(self, name):
2796 self.tar.extract(name, TEMPDIR, filter='fully_trusted')
2799 self.assertEqual(sha256sum(data), sha256_regtype)
2804 def test_hardlink_extraction1(self):
2805 self._test_link_extraction("ustar/lnktype")
2809 def test_hardlink_extraction2(self):
2810 self._test_link_extraction("./ustar/linktest2/lnktype")
2814 def test_symlink_extraction1(self):
2815 self._test_link_extraction("ustar/symtype")
2819 def test_symlink_extraction2(self):
2820 self._test_link_extraction("./ustar/linktest2/symtype")
2827 def _test_partial_input(self, mode):
2830 def read(self, n):
2831 if self.hit_eof:
2834 self.hit_eof = self.tell() == len(self.getvalue())
2835 return super(MyBytesIO, self).read(n)
2836 def seek(self, *args):
2837 self.hit_eof = False
2838 return super(MyBytesIO, self).seek(*args)
2847 def test_partial_input(self):
2848 self._test_partial_input("r")
2850 def test_partial_input_bz2(self):
2851 self._test_partial_input("r:bz2")
2924 def test_extract_with_numeric_owner(self, mock_geteuid, mock_chmod,
2926 with self._setup_test(mock_geteuid) as (tarfl, filename_1, _,
2945 def test_extractall_with_numeric_owner(self, mock_geteuid, mock_chmod,
2947 with self._setup_test(mock_geteuid) as (tarfl, filename_1, dirname_1,
2972 def test_extract_without_numeric_owner(self, mock_geteuid, mock_chmod,
2974 with self._setup_test(mock_geteuid) as (tarfl, filename_1, _, _):
2984 def test_keyword_only(self, mock_geteuid):
2985 with self._setup_test(mock_geteuid) as (tarfl, filename_1, _, _):
2986 self.assertRaises(TypeError,
2991 def test_replace_name(self):
2992 member = self.tar.getmember('ustar/regtype')
2994 self.assertEqual(replaced.name, 'misc/other')
2995 self.assertEqual(member.name, 'ustar/regtype')
2996 self.assertEqual(self.tar.getmember('ustar/regtype').name,
2999 def test_replace_deep(self):
3000 member = self.tar.getmember('pax/regtype1')
3003 self.assertEqual(member.pax_headers['gname'], 'bar')
3004 self.assertEqual(
3005 self.tar.getmember('pax/regtype1').pax_headers['gname'], 'bar')
3007 def test_replace_shallow(self):
3008 member = self.tar.getmember('pax/regtype1')
3011 self.assertEqual(member.pax_headers['gname'], 'not-bar')
3012 self.assertEqual(
3013 self.tar.getmember('pax/regtype1').pax_headers['gname'], 'not-bar')
3015 def test_replace_all(self):
3016 member = self.tar.getmember('ustar/regtype')
3019 with self.subTest(attr_name=attr_name):
3021 self.assertEqual(getattr(replaced, attr_name), None)
3022 self.assertNotEqual(getattr(member, attr_name), None)
3024 def test_replace_internal(self):
3025 member = self.tar.getmember('ustar/regtype')
3026 with self.assertRaises(TypeError):
3054 def check_files_present(self, directory):
3058 self.assertEqual(self.control_paths, got_paths)
3061 def extract_with_none(self, *attr_names):
3063 self.tar.errorlevel = 0
3064 for member in self.tar.getmembers():
3068 self.tar.extractall(DIR, filter='fully_trusted')
3069 self.check_files_present(DIR)
3072 def test_extractall_none_mtime(self):
3076 with self.extract_with_none('mtime') as DIR:
3078 with self.subTest(path=path):
3086 self.assertGreaterEqual(path.stat().st_mtime, now)
3088 def test_extractall_none_mode(self):
3095 with self.extract_with_none('mode') as DIR:
3097 with self.subTest(path=path):
3099 self.assertEqual(path.stat().st_mode, dir_mode)
3101 self.assertEqual(path.stat().st_mode,
3104 def test_extractall_none_uid(self):
3105 with self.extract_with_none('uid'):
3108 def test_extractall_none_gid(self):
3109 with self.extract_with_none('gid'):
3112 def test_extractall_none_uname(self):
3113 with self.extract_with_none('uname'):
3116 def test_extractall_none_gname(self):
3117 with self.extract_with_none('gname'):
3120 def test_extractall_none_ownership(self):
3121 with self.extract_with_none('uid', 'gid', 'uname', 'gname'):
3139 def test_add(self):
3144 with self.subTest(tarformat=tarformat):
3159 with self.subTest(attr_name=attr_name):
3161 with self.assertRaisesRegex(ValueError,
3165 def test_list(self):
3173 with (self.subTest(attr_names=attr_names),
3187 self.assertEqual(got, b'????-??-??')
3189 self.assertEqual(got, b'??:??:??')
3192 self.assertEqual(got, b'??????????')
3197 self.assertEqual(got_group, exp_group)
3198 self.assertRegex(got_user, b'[0-9]+')
3203 self.assertEqual(got_user, exp_user)
3204 self.assertRegex(got_group, b'[0-9]+')
3209 self.assertEqual(got_group, exp_group)
3210 self.assertEqual(got_user, b'None')
3214 self.assertEqual(got_user, exp_user)
3215 self.assertEqual(got_group, b'None')
3220 self.assertEqual(got_group, exp_group)
3221 self.assertEqual(got_user, b'None')
3226 self.assertEqual(got_user, exp_user)
3227 self.assertEqual(got_group, b'None')
3230 self.assertEqual(expected, got)
3271 def __init__(self):
3272 self.bio = io.BytesIO()
3274 def __enter__(self):
3275 self.tar_w = tarfile.TarFile(mode='w', fileobj=self.bio)
3276 return self
3278 def __exit__(self, *exc):
3279 self.tar_w.close()
3280 self.contents = self.bio.getvalue()
3281 self.bio = None
3283 def add(self, name, *, type=None, symlink_to=None, hardlink_to=None,
3304 self.tar_w.addfile(tarinfo, fileobj)
3306 def open(self, **kwargs):
3308 bio = io.BytesIO(self.contents)
3335 def check_context(self, tar, filter):
3336 """Extracts `tar` to `self.destdir` and allows checking the result
3345 with os_helper.temp_dir(self.outerdir):
3347 tar.extractall(self.destdir, filter=filter)
3349 self.raised_exception = exc
3350 self.expected_paths = set()
3352 self.raised_exception = None
3353 self.expected_paths = set(self.outerdir.glob('**/*'))
3354 self.expected_paths.discard(self.destdir)
3359 if self.raised_exception:
3360 raise self.raised_exception
3361 self.assertEqual(self.expected_paths, set())
3363 def expect_file(self, name, type=None, symlink_to=None, mode=None):
3365 if self.raised_exception:
3366 raise self.raised_exception
3368 path = pathlib.Path(os.path.normpath(self.destdir / name))
3369 self.assertIn(path, self.expected_paths)
3370 self.expected_paths.remove(path)
3373 self.assertEqual(got, mode)
3377 got = (self.destdir / name).readlink()
3383 self.assertTrue(got.samefile(expected))
3385 self.assertTrue(path.is_file())
3387 self.assertTrue(path.is_dir())
3389 self.assertTrue(path.is_fifo())
3393 self.expected_paths.discard(parent)
3395 def expect_exception(self, exc_type, message_re='.'):
3396 with self.assertRaisesRegex(exc_type, message_re):
3397 if self.raised_exception is not None:
3398 raise self.raised_exception
3399 self.raised_exception = None
3401 def test_benign_file(self):
3405 with self.check_context(arc.open(), filter):
3406 self.expect_file('benign.txt')
3408 def test_absolute(self):
3412 arc.add(self.outerdir / 'escaped.evil')
3414 with self.check_context(arc.open(), 'fully_trusted'):
3415 self.expect_file('../escaped.evil')
3418 with self.check_context(arc.open(), filter):
3419 if str(self.outerdir).startswith('/'):
3422 outerdir_stripped = str(self.outerdir).lstrip('/')
3423 self.expect_file(f'{outerdir_stripped}/escaped.evil')
3429 self.expect_exception(
3434 def test_parent_symlink(self):
3443 with self.check_context(arc.open(), 'fully_trusted'):
3444 if self.raised_exception is not None:
3447 self.expect_exception(FileExistsError)
3452 self.expect_file('current', symlink_to='.')
3453 self.expect_file('parent', symlink_to='current/..')
3454 self.expect_file('../evil')
3456 with self.check_context(arc.open(), 'tar'):
3457 self.expect_exception(
3462 with self.check_context(arc.open(), 'data'):
3463 self.expect_exception(
3470 with self.check_context(arc.open(), 'fully_trusted'):
3471 self.expect_file('parent/evil')
3472 with self.check_context(arc.open(), 'tar'):
3473 self.expect_file('parent/evil')
3474 with self.check_context(arc.open(), 'data'):
3475 self.expect_file('parent/evil')
3478 def test_parent_symlink2(self):
3486 with self.check_context(arc.open(), 'fully_trusted'):
3488 self.expect_file('current', symlink_to='.')
3489 self.expect_file('parent', symlink_to='..')
3490 self.expect_file('../evil')
3492 self.expect_file('current/')
3493 self.expect_file('parent/evil')
3495 with self.check_context(arc.open(), 'tar'):
3497 self.expect_exception(
3503 self.expect_file('current/')
3504 self.expect_file('parent/evil')
3506 with self.check_context(arc.open(), 'data'):
3507 self.expect_exception(
3513 def test_absolute_symlink(self):
3517 arc.add('parent', symlink_to=self.outerdir)
3520 with self.check_context(arc.open(), 'fully_trusted'):
3522 self.expect_file('parent', symlink_to=self.outerdir)
3523 self.expect_file('../evil')
3525 self.expect_file('parent/evil')
3527 with self.check_context(arc.open(), 'tar'):
3529 self.expect_exception(
3535 self.expect_file('parent/evil')
3537 with self.check_context(arc.open(), 'data'):
3538 self.expect_exception(
3543 def test_sly_relative0(self):
3549 with self.check_context(arc.open(), filter='fully_trusted'):
3551 if isinstance(self.raised_exception, FileExistsError):
3559 self.expect_exception(FileExistsError)
3561 self.expect_file('../moo', symlink_to='..//tmp/moo')
3569 with self.check_context(arc.open(), filter):
3570 self.expect_exception(
3577 def test_sly_relative2(self):
3583 with self.check_context(arc.open(), 'fully_trusted'):
3584 self.expect_file('tmp', type=tarfile.DIRTYPE)
3586 self.expect_file('../moo', symlink_to='tmp/../../tmp/moo')
3589 with self.check_context(arc.open(), filter):
3590 self.expect_exception(
3596 def test_modes(self):
3621 with self.check_context(arc.open(), 'fully_trusted'):
3623 self.expect_file('all_bits', mode='?rwsrwsrwt')
3625 self.expect_file('all_bits', mode='?rwsrwsrwx')
3626 self.expect_file('perm_bits', mode='?rwxrwxrwx')
3627 self.expect_file('exec_group_other', mode='?rw-rwxrwx')
3628 self.expect_file('read_group_only', mode='?---r-----')
3629 self.expect_file('no_bits', mode='?---------')
3631 self.expect_file('dir/', mode='?---rwsrwt')
3633 self.expect_file('dir/', mode='?---rwsrwx')
3635 with self.check_context(arc.open(), 'tar'):
3636 self.expect_file('all_bits', mode='?rwxr-xr-x')
3637 self.expect_file('perm_bits', mode='?rwxr-xr-x')
3638 self.expect_file('exec_group_other', mode='?rw-r-xr-x')
3639 self.expect_file('read_group_only', mode='?---r-----')
3640 self.expect_file('no_bits', mode='?---------')
3641 self.expect_file('dir/', mode='?---r-xr-x')
3643 with self.check_context(arc.open(), 'data'):
3645 self.outerdir.stat().st_mode))
3646 self.expect_file('all_bits', mode='?rwxr-xr-x')
3647 self.expect_file('perm_bits', mode='?rwxr-xr-x')
3648 self.expect_file('exec_group_other', mode='?rw-r--r--')
3649 self.expect_file('read_group_only', mode='?rw-r-----')
3650 self.expect_file('no_bits', mode='?rw-------')
3651 self.expect_file('dir/', mode=normal_dir_mode)
3653 def test_pipe(self):
3659 with self.check_context(arc.open(), filter):
3661 self.expect_file('foo', type=tarfile.FIFOTYPE)
3666 with self.check_context(arc.open(), 'data'):
3667 self.expect_exception(
3671 def test_special_files(self):
3678 self.assertIs(trusted, tarinfo)
3680 self.assertEqual(tar.type, special_type)
3681 with self.assertRaises(tarfile.SpecialFileError) as cm:
3683 self.assertIsInstance(cm.exception.tarinfo, tarfile.TarInfo)
3684 self.assertEqual(cm.exception.tarinfo.name, 'foo')
3686 def test_fully_trusted_filter(self):
3691 self.assertIs(filtered, tarinfo)
3693 def test_tar_filter(self):
3700 self.assertIs(filtered.name, tarinfo.name)
3701 self.assertIs(filtered.type, tarinfo.type)
3703 def test_data_filter(self):
3712 self.assertIs(filtered.name, tarinfo.name)
3713 self.assertIs(filtered.type, tarinfo.type)
3715 def test_default_filter_warns_not(self):
3719 with warnings_helper.check_no_warnings(self):
3720 with self.check_context(arc.open(), None):
3721 self.expect_file('foo')
3723 def test_change_default_filter_on_instance(self):
3731 with self.check_context(tar, None):
3732 self.expect_file('ustar/regtype')
3734 def test_change_default_filter_on_class(self):
3743 with self.check_context(tar, None):
3744 self.expect_file('ustar/regtype')
3746 def test_change_default_filter_on_subclass(self):
3748 def extraction_filter(self, tarinfo, path):
3755 with self.check_context(tar, None):
3756 self.expect_file('ustar/regtype')
3758 def test_change_default_filter_to_string(self):
3761 with self.check_context(tar, None):
3762 self.expect_exception(TypeError)
3764 def test_custom_filter(self):
3766 self.assertIs(path, self.destdir)
3777 with self.check_context(arc.open(), custom_filter):
3778 self.expect_file('moved')
3779 self.expect_file('keep')
3781 def test_bad_filter_name(self):
3784 with self.check_context(arc.open(), 'bad filter name'):
3785 self.expect_exception(ValueError)
3787 def test_stateful_filter(self):
3792 def __enter__(self):
3793 self.num_files_processed = 0
3794 return self
3796 def __call__(self, tarinfo, path):
3801 self.num_files_processed += 1
3804 def __exit__(self, *exc_info):
3805 self.done = True
3812 with self.check_context(arc.open(), custom_filter):
3813 self.expect_file('good')
3814 self.assertEqual(custom_filter.num_files_processed, 2)
3815 self.assertEqual(custom_filter.done, True)
3817 def test_errorlevel(self):
3834 with self.check_context(arc.open(errorlevel=0), extracterror_filter):
3835 self.expect_file('file')
3837 with self.check_context(arc.open(errorlevel=0), filtererror_filter):
3838 self.expect_file('file')
3840 with self.check_context(arc.open(errorlevel=0), oserror_filter):
3841 self.expect_file('file')
3843 with self.check_context(arc.open(errorlevel=0), tarerror_filter):
3844 self.expect_exception(tarfile.TarError)
3846 with self.check_context(arc.open(errorlevel=0), valueerror_filter):
3847 self.expect_exception(ValueError)
3851 with self.check_context(arc.open(errorlevel=1), extracterror_filter):
3852 self.expect_file('file')
3854 with self.check_context(arc.open(errorlevel=1), filtererror_filter):
3855 self.expect_exception(tarfile.FilterError)
3857 with self.check_context(arc.open(errorlevel=1), oserror_filter):
3858 self.expect_exception(OSError)
3860 with self.check_context(arc.open(errorlevel=1), tarerror_filter):
3861 self.expect_exception(tarfile.TarError)
3863 with self.check_context(arc.open(errorlevel=1), valueerror_filter):
3864 self.expect_exception(ValueError)
3868 with self.check_context(arc.open(errorlevel=2), extracterror_filter):
3869 self.expect_exception(tarfile.ExtractError)
3871 with self.check_context(arc.open(errorlevel=2), filtererror_filter):
3872 self.expect_exception(tarfile.FilterError)
3874 with self.check_context(arc.open(errorlevel=2), oserror_filter):
3875 self.expect_exception(OSError)
3877 with self.check_context(arc.open(errorlevel=2), tarerror_filter):
3878 self.expect_exception(tarfile.TarError)
3880 with self.check_context(arc.open(errorlevel=2), valueerror_filter):
3881 self.expect_exception(ValueError)
3885 with self.check_context(arc.open(errorlevel='boo!'), filtererror_filter):
3886 self.expect_exception(TypeError) # errorlevel is not int