summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tarfile.py
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2004-07-20 21:54:18 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2004-07-20 21:54:18 (GMT)
commit0662f8a5eaa19f7eed6978eed1af32563359fb93 (patch)
treeb1038a93a40ba0091f8014f6b626ce0881c019ff /Lib/test/test_tarfile.py
parent13e50fe45349c89325aeba460ef2d9e0efaca27c (diff)
downloadcpython-0662f8a5eaa19f7eed6978eed1af32563359fb93.zip
cpython-0662f8a5eaa19f7eed6978eed1af32563359fb93.tar.gz
cpython-0662f8a5eaa19f7eed6978eed1af32563359fb93.tar.bz2
SF #846659, fix bufsize violation and GNU longname/longlink extensions
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r--Lib/test/test_tarfile.py91
1 files changed, 90 insertions, 1 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 9d40ace..bd3fd8e 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -205,6 +205,94 @@ class WriteTest(BaseTest):
class WriteStreamTest(WriteTest):
sep = '|'
+class WriteGNULongTest(unittest.TestCase):
+ """This testcase checks for correct creation of GNU Longname
+ and Longlink extensions.
+
+ It creates a tarfile and adds empty members with either
+ long names, long linknames or both and compares the size
+ of the tarfile with the expected size.
+
+ It checks for SF bug #812325 in TarFile._create_gnulong().
+
+ While I was writing this testcase, I noticed a second bug
+ in the same method:
+ Long{names,links} weren't null-terminated which lead to
+ bad tarfiles when their length was a multiple of 512. This
+ is tested as well.
+ """
+
+ def setUp(self):
+ self.tar = tarfile.open(tmpname(), "w")
+ self.tar.posix = False
+
+ def tearDown(self):
+ self.tar.close()
+
+ def _length(self, s):
+ blocks, remainder = divmod(len(s) + 1, 512)
+ if remainder:
+ blocks += 1
+ return blocks * 512
+
+ def _calc_size(self, name, link=None):
+ # initial tar header
+ count = 512
+
+ if len(name) > tarfile.LENGTH_NAME:
+ # gnu longname extended header + longname
+ count += 512
+ count += self._length(name)
+
+ if link is not None and len(link) > tarfile.LENGTH_LINK:
+ # gnu longlink extended header + longlink
+ count += 512
+ count += self._length(link)
+
+ return count
+
+ def _test(self, name, link=None):
+ tarinfo = tarfile.TarInfo(name)
+ if link:
+ tarinfo.linkname = link
+ tarinfo.type = tarfile.LNKTYPE
+
+ self.tar.addfile(tarinfo)
+
+ v1 = self._calc_size(name, link)
+ v2 = self.tar.offset
+ self.assertEqual(v1, v2, "GNU longname/longlink creation failed")
+
+ def test_longname_1023(self):
+ self._test(("longnam/" * 127) + "longnam")
+
+ def test_longname_1024(self):
+ self._test(("longnam/" * 127) + "longname")
+
+ def test_longname_1025(self):
+ self._test(("longnam/" * 127) + "longname_")
+
+ def test_longlink_1023(self):
+ self._test("name", ("longlnk/" * 127) + "longlnk")
+
+ def test_longlink_1024(self):
+ self._test("name", ("longlnk/" * 127) + "longlink")
+
+ def test_longlink_1025(self):
+ self._test("name", ("longlnk/" * 127) + "longlink_")
+
+ def test_longnamelink_1023(self):
+ self._test(("longnam/" * 127) + "longnam",
+ ("longlnk/" * 127) + "longlnk")
+
+ def test_longnamelink_1024(self):
+ self._test(("longnam/" * 127) + "longname",
+ ("longlnk/" * 127) + "longlink")
+
+ def test_longnamelink_1025(self):
+ self._test(("longnam/" * 127) + "longname_",
+ ("longlnk/" * 127) + "longlink_")
+
# Gzip TestCases
class ReadTestGzip(ReadTest):
comp = "gz"
@@ -245,7 +333,8 @@ def test_main():
ReadTest,
ReadStreamTest,
WriteTest,
- WriteStreamTest
+ WriteStreamTest,
+ WriteGNULongTest,
]
if gzip: