diff options
author | Gregory P. Smith <greg@krypto.org> | 2013-02-03 08:36:32 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2013-02-03 08:36:32 (GMT) |
commit | 09aa752067863ecf749010b315227f41e25571a5 (patch) | |
tree | 90f5dd649fbaa8429abc3bd4bb9303b05d7bcc84 /Lib/test/test_zipfile.py | |
parent | 6d29628d6bb083f63b94efeba6103522681bc872 (diff) | |
download | cpython-09aa752067863ecf749010b315227f41e25571a5.zip cpython-09aa752067863ecf749010b315227f41e25571a5.tar.gz cpython-09aa752067863ecf749010b315227f41e25571a5.tar.bz2 |
Refactor recently added bugfix into more testable code by using a
method for windows file name sanitization. Splits the unittest up
into several based on platform.
Diffstat (limited to 'Lib/test/test_zipfile.py')
-rw-r--r-- | Lib/test/test_zipfile.py | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index c1e20b2..5e837cd 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -538,8 +538,15 @@ class TestsWithSourceFile(unittest.TestCase): with open(filename, 'rb') as f: self.assertEqual(f.read(), content) - def test_extract_hackers_arcnames(self): - hacknames = [ + def test_sanitize_windows_name(self): + san = zipfile.ZipFile._sanitize_windows_name + # Passing pathsep in allows this test to work regardless of platform. + self.assertEqual(san(r',,?,C:,foo,bar/z', ','), r'_,C_,foo,bar/z') + self.assertEqual(san(r'a\b,c<d>e|f"g?h*i', ','), r'a\b,c_d_e_f_g_h_i') + self.assertEqual(san('../../foo../../ba..r', '/'), r'foo/ba..r') + + def test_extract_hackers_arcnames_common_cases(self): + common_hacknames = [ ('../foo/bar', 'foo/bar'), ('foo/../bar', 'foo/bar'), ('foo/../../bar', 'foo/bar'), @@ -549,8 +556,12 @@ class TestsWithSourceFile(unittest.TestCase): ('/foo/../bar', 'foo/bar'), ('/foo/../../bar', 'foo/bar'), ] - if os.path.sep == '\\': # Windows. - hacknames.extend([ + self._test_extract_hackers_arcnames(common_hacknames) + + @unittest.skipIf(os.path.sep != '\\', 'Requires \\ as path separator.') + def test_extract_hackers_arcnames_windows_only(self): + """Test combination of path fixing and windows name sanitization.""" + windows_hacknames = [ (r'..\foo\bar', 'foo/bar'), (r'..\/foo\/bar', 'foo/bar'), (r'foo/\..\/bar', 'foo/bar'), @@ -570,14 +581,19 @@ class TestsWithSourceFile(unittest.TestCase): (r'C:/../C:/foo/bar', 'C_/foo/bar'), (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'), ('../../foo../../ba..r', 'foo/ba..r'), - ]) - else: # Unix - hacknames.extend([ - ('//foo/bar', 'foo/bar'), - ('../../foo../../ba..r', 'foo../ba..r'), - (r'foo/..\bar', r'foo/..\bar'), - ]) + ] + self._test_extract_hackers_arcnames(windows_hacknames) + + @unittest.skipIf(os.path.sep != '/', r'Requires / as path separator.') + def test_extract_hackers_arcnames_posix_only(self): + posix_hacknames = [ + ('//foo/bar', 'foo/bar'), + ('../../foo../../ba..r', 'foo../ba..r'), + (r'foo/..\bar', r'foo/..\bar'), + ] + self._test_extract_hackers_arcnames(posix_hacknames) + def _test_extract_hackers_arcnames(self, hacknames): for arcname, fixedname in hacknames: content = b'foobar' + arcname.encode() with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp: @@ -594,7 +610,8 @@ class TestsWithSourceFile(unittest.TestCase): with zipfile.ZipFile(TESTFN2, 'r') as zipfp: writtenfile = zipfp.extract(arcname, targetpath) self.assertEqual(writtenfile, correctfile, - msg="extract %r" % arcname) + msg='extract %r: %r != %r' % + (arcname, writtenfile, correctfile)) self.check_file(correctfile, content) shutil.rmtree('target') |