diff options
Diffstat (limited to 'Lib/test/test_tarfile.py')
-rw-r--r-- | Lib/test/test_tarfile.py | 118 |
1 files changed, 107 insertions, 11 deletions
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py index 98349c4..8ee0f41 100644 --- a/Lib/test/test_tarfile.py +++ b/Lib/test/test_tarfile.py @@ -2,6 +2,7 @@ import sys import os import shutil import tempfile +import StringIO import unittest import tarfile @@ -25,7 +26,7 @@ def path(path): testtar = path("testtar.tar") tempdir = os.path.join(tempfile.gettempdir(), "testtar" + os.extsep + "dir") tempname = test_support.TESTFN -membercount = 10 +membercount = 12 def tarname(comp=""): if not comp: @@ -86,7 +87,9 @@ class ReadTest(BaseTest): if self.sep != "|": filename = "0-REGTYPE-TEXT" self.tar.extract(filename, dirname()) - lines1 = file(os.path.join(dirname(), filename), "rU").readlines() + f = open(os.path.join(dirname(), filename), "rU") + lines1 = f.readlines() + f.close() lines2 = self.tar.extractfile(filename).readlines() self.assert_(lines1 == lines2, "_FileObject.readline() does not work correctly") @@ -96,7 +99,9 @@ class ReadTest(BaseTest): if self.sep != "|": filename = "0-REGTYPE-TEXT" self.tar.extract(filename, dirname()) - lines1 = file(os.path.join(dirname(), filename), "rU").readlines() + f = open(os.path.join(dirname(), filename), "rU") + lines1 = f.readlines() + f.close() lines2 = [line for line in self.tar.extractfile(filename)] self.assert_(lines1 == lines2, "ExFileObject iteration does not work correctly") @@ -107,7 +112,9 @@ class ReadTest(BaseTest): if self.sep != "|": filename = "0-REGTYPE" self.tar.extract(filename, dirname()) - data = file(os.path.join(dirname(), filename), "rb").read() + f = open(os.path.join(dirname(), filename), "rb") + data = f.read() + f.close() tarinfo = self.tar.getmember(filename) fobj = self.tar.extractfile(tarinfo) @@ -155,7 +162,7 @@ class ReadTest(BaseTest): tarinfo = tarfile.TarInfo("directory/") tarinfo.type = tarfile.REGTYPE - fobj = file(filename, "w") + fobj = open(filename, "w") fobj.write(tarinfo.tobuf()) fobj.close() @@ -209,8 +216,21 @@ class ReadStreamTest(ReadTest): self.assert_(v2 is not None, "stream.extractfile() failed") self.assert_(v1.read() == v2.read(), "stream extraction failed") + tar.close() stream.close() +class ReadDetectTest(ReadTest): + + def setUp(self): + self.tar = tarfile.open(tarname(self.comp), self.mode) + +class ReadDetectFileobjTest(ReadTest): + + def setUp(self): + name = tarname(self.comp) + self.tar = tarfile.open(name, mode=self.mode, + fileobj=open(name, "rb")) + class ReadAsteriskTest(ReadTest): def setUp(self): @@ -254,7 +274,7 @@ class WriteTest(BaseTest): if not tarinfo.isreg(): continue f = self.src.extractfile(tarinfo) - if self.dst.posix and len(tarinfo.name) > tarfile.LENGTH_NAME: + if self.dst.posix and len(tarinfo.name) > tarfile.LENGTH_NAME and "/" not in tarinfo.name: self.assertRaises(ValueError, self.dst.addfile, tarinfo, f) else: @@ -273,15 +293,22 @@ class WriteSize0Test(BaseTest): def test_file(self): path = os.path.join(self.tmpdir, "file") - file(path, "w") + f = open(path, "w") + f.close() tarinfo = self.dst.gettarinfo(path) self.assertEqual(tarinfo.size, 0) - file(path, "w").write("aaa") + f = open(path, "w") + f.write("aaa") + f.close() tarinfo = self.dst.gettarinfo(path) self.assertEqual(tarinfo.size, 3) def test_directory(self): path = os.path.join(self.tmpdir, "directory") + if os.path.exists(path): + # This shouldn't be necessary, but is <wink> if a previous + # run was killed in mid-stream. + shutil.rmtree(path) os.mkdir(path) tarinfo = self.dst.gettarinfo(path) self.assertEqual(tarinfo.size, 0) @@ -385,6 +412,52 @@ class WriteGNULongTest(unittest.TestCase): self._test(("longnam/" * 127) + "longname_", ("longlnk/" * 127) + "longlink_") +class ReadGNULongTest(unittest.TestCase): + + def setUp(self): + self.tar = tarfile.open(tarname()) + + def tearDown(self): + self.tar.close() + + def test_1471427(self): + """Test reading of longname (bug #1471427). + """ + name = "test/" * 20 + "0-REGTYPE" + try: + tarinfo = self.tar.getmember(name) + except KeyError: + tarinfo = None + self.assert_(tarinfo is not None, "longname not found") + self.assert_(tarinfo.type != tarfile.DIRTYPE, "read longname as dirtype") + + def test_read_name(self): + name = ("0-LONGNAME-" * 10)[:101] + try: + tarinfo = self.tar.getmember(name) + except KeyError: + tarinfo = None + self.assert_(tarinfo is not None, "longname not found") + + def test_read_link(self): + link = ("1-LONGLINK-" * 10)[:101] + name = ("0-LONGNAME-" * 10)[:101] + try: + tarinfo = self.tar.getmember(link) + except KeyError: + tarinfo = None + self.assert_(tarinfo is not None, "longlink not found") + self.assert_(tarinfo.linkname == name, "linkname wrong") + + def test_truncated_longname(self): + f = open(tarname()) + fobj = StringIO.StringIO(f.read(1024)) + f.close() + tar = tarfile.open(name="foo.tar", fileobj=fobj) + self.assert_(len(tar.getmembers()) == 0, "") + tar.close() + + class ExtractHardlinkTest(BaseTest): def test_hardlink(self): @@ -420,7 +493,9 @@ class CreateHardlinkTest(BaseTest): if os.path.exists(self.bar): os.remove(self.bar) - file(self.foo, "w").write("foo") + f = open(self.foo, "w") + f.write("foo") + f.close() self.tar.add(self.foo) def test_add_twice(self): @@ -459,6 +534,10 @@ class WriteTestGzip(WriteTest): comp = "gz" class WriteStreamTestGzip(WriteStreamTest): comp = "gz" +class ReadDetectTestGzip(ReadDetectTest): + comp = "gz" +class ReadDetectFileobjTestGzip(ReadDetectFileobjTest): + comp = "gz" class ReadAsteriskTestGzip(ReadAsteriskTest): comp = "gz" class ReadStreamAsteriskTestGzip(ReadStreamAsteriskTest): @@ -482,6 +561,10 @@ if bz2: comp = "bz2" class WriteStreamTestBzip2(WriteStreamTestGzip): comp = "bz2" + class ReadDetectTestBzip2(ReadDetectTest): + comp = "bz2" + class ReadDetectFileobjTestBzip2(ReadDetectFileobjTest): + comp = "bz2" class ReadAsteriskTestBzip2(ReadAsteriskTest): comp = "bz2" class ReadStreamAsteriskTestBzip2(ReadStreamAsteriskTest): @@ -495,23 +578,34 @@ if not gzip: del WriteStreamTestGzip def test_main(): + # Create archive. + f = open(tarname(), "rb") + fguts = f.read() + f.close() if gzip: # create testtar.tar.gz - gzip.open(tarname("gz"), "wb").write(file(tarname(), "rb").read()) + tar = gzip.open(tarname("gz"), "wb") + tar.write(fguts) + tar.close() if bz2: # create testtar.tar.bz2 - bz2.BZ2File(tarname("bz2"), "wb").write(file(tarname(), "rb").read()) + tar = bz2.BZ2File(tarname("bz2"), "wb") + tar.write(fguts) + tar.close() tests = [ FileModeTest, ReadTest, ReadStreamTest, + ReadDetectTest, + ReadDetectFileobjTest, ReadAsteriskTest, ReadStreamAsteriskTest, WriteTest, WriteSize0Test, WriteStreamTest, WriteGNULongTest, + ReadGNULongTest, ] if hasattr(os, "link"): @@ -522,6 +616,7 @@ def test_main(): tests.extend([ ReadTestGzip, ReadStreamTestGzip, WriteTestGzip, WriteStreamTestGzip, + ReadDetectTestGzip, ReadDetectFileobjTestGzip, ReadAsteriskTestGzip, ReadStreamAsteriskTestGzip ]) @@ -529,6 +624,7 @@ def test_main(): tests.extend([ ReadTestBzip2, ReadStreamTestBzip2, WriteTestBzip2, WriteStreamTestBzip2, + ReadDetectTestBzip2, ReadDetectFileobjTestBzip2, ReadAsteriskTestBzip2, ReadStreamAsteriskTestBzip2 ]) try: |