diff options
author | Elliott Hughes <enh@google.com> | 2013-07-11 12:53:01 -0700 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2013-07-11 12:53:01 -0700 |
commit | c1ee1ac7ff5dd966a90b1524232db6406bbd42fd (patch) | |
tree | e1c452f9329bb373ca280f064b87baa48519bdc1 | |
parent | 1cec2593103dc175a9ca267cb3f3a4760e2d36a4 (diff) | |
parent | 996637897c99d28a054bb0094717b36bbf3e8b59 (diff) | |
download | libcore-c1ee1ac7ff5dd966a90b1524232db6406bbd42fd.zip libcore-c1ee1ac7ff5dd966a90b1524232db6406bbd42fd.tar.gz libcore-c1ee1ac7ff5dd966a90b1524232db6406bbd42fd.tar.bz2 |
am 99663789: am b3e2cc46: am adf25974: am 15a93894: Tests for ZIP sign extension bugs.
* commit '996637897c99d28a054bb0094717b36bbf3e8b59':
Tests for ZIP sign extension bugs.
-rw-r--r-- | luni/src/test/java/libcore/java/util/zip/ZipEntryTest.java | 181 |
1 files changed, 137 insertions, 44 deletions
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipEntryTest.java b/luni/src/test/java/libcore/java/util/zip/ZipEntryTest.java index b18ebb4..0845b32 100644 --- a/luni/src/test/java/libcore/java/util/zip/ZipEntryTest.java +++ b/luni/src/test/java/libcore/java/util/zip/ZipEntryTest.java @@ -16,9 +16,11 @@ package libcore.java.util.zip; +import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.jar.JarEntry; @@ -28,52 +30,143 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipEntryTest extends junit.framework.TestCase { + private static File createTemporaryZipFile() throws IOException { + File result = File.createTempFile("ZipFileTest", "zip"); + result.deleteOnExit(); + return result; + } - /** - * http://code.google.com/p/android/issues/detail?id=4690 - */ - public void test_utf8FileNames() throws Exception { - // Create a zip file containing non-ASCII filenames. - File f = File.createTempFile("your", "mum"); - List<String> filenames = Arrays.asList("us-ascii", - "\u043c\u0430\u0440\u0442\u0430", // russian - "\u1f00\u03c0\u1f78", // greek - "\u30b3\u30f3\u30cb\u30c1\u30cf"); // japanese - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); - for (String filename : filenames) { - out.putNextEntry(new ZipEntry(filename)); - out.closeEntry(); // Empty files are fine. - } - out.close(); - // Read it back, and check we find all those names. - // This failed when we were mangling the encoding. - ZipFile zipFile = new ZipFile(f); - for (String filename : filenames) { - assertNotNull(filename, zipFile.getEntry(filename)); - } - // Check that ZipInputStream works too. - ZipInputStream in = new ZipInputStream(new FileInputStream(f)); - ZipEntry entry; - int entryCount = 0; - while ((entry = in.getNextEntry()) != null) { - assertTrue(entry.getName(), filenames.contains(entry.getName())); - ++entryCount; - } - assertEquals(filenames.size(), entryCount); - in.close(); + private static ZipOutputStream createZipOutputStream(File f) throws IOException { + return new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f))); + } + + private static String makeString(int count, String s) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < count; ++i) { + sb.append(s); + } + return sb.toString(); + } + + // http://code.google.com/p/android/issues/detail?id=4690 + public void test_utf8FileNames() throws Exception { + // Create a zip file containing non-ASCII filenames. + File f = File.createTempFile("your", "mum"); + List<String> filenames = Arrays.asList("us-ascii", + "\u043c\u0430\u0440\u0442\u0430", // russian + "\u1f00\u03c0\u1f78", // greek + "\u30b3\u30f3\u30cb\u30c1\u30cf"); // japanese + ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f)); + for (String filename : filenames) { + out.putNextEntry(new ZipEntry(filename)); + out.closeEntry(); // Empty files are fine. + } + out.close(); + // Read it back, and check we find all those names. + // This failed when we were mangling the encoding. + ZipFile zipFile = new ZipFile(f); + for (String filename : filenames) { + assertNotNull(filename, zipFile.getEntry(filename)); + } + // Check that ZipInputStream works too. + ZipInputStream in = new ZipInputStream(new FileInputStream(f)); + ZipEntry entry; + int entryCount = 0; + while ((entry = in.getNextEntry()) != null) { + assertTrue(entry.getName(), filenames.contains(entry.getName())); + ++entryCount; + } + assertEquals(filenames.size(), entryCount); + in.close(); + } + + // http://b/2099615 + public void testClone() { + byte[] extra = { 5, 7, 9 }; + JarEntry jarEntry = new JarEntry("foo"); + jarEntry.setExtra(extra); + assertSame("Expected no defensive copy of extra", extra, jarEntry.getExtra()); + + ZipEntry clone = (ZipEntry) jarEntry.clone(); + assertEquals(JarEntry.class, clone.getClass()); + assertNotSame(extra, clone.getExtra()); + } + + public void testTooLongName() throws Exception { + String tooLongName = makeString(65536, "z"); + try { + new ZipEntry(tooLongName); + fail(); + } catch (IllegalArgumentException expected) { + } + } + + public void testMaxLengthName() throws Exception { + String maxLengthName = makeString(65535, "z"); + + File f = createTemporaryZipFile(); + ZipOutputStream out = createZipOutputStream(f); + out.putNextEntry(new ZipEntry(maxLengthName)); + out.closeEntry(); + out.close(); + + // Read it back, and check that we see the entry. + ZipFile zipFile = new ZipFile(f); + assertNotNull(zipFile.getEntry(maxLengthName)); + zipFile.close(); + } + + public void testTooLongExtra() throws Exception { + byte[] tooLongExtra = new byte[65536]; + ZipEntry ze = new ZipEntry("x"); + try { + ze.setExtra(tooLongExtra); + fail(); + } catch (IllegalArgumentException expected) { } + } + + public void testMaxLengthExtra() throws Exception { + byte[] maxLengthExtra = new byte[65535]; - /** - * http://b/2099615 - */ - public void testClone() { - byte[] extra = { 5, 7, 9 }; - JarEntry jarEntry = new JarEntry("foo"); - jarEntry.setExtra(extra); - assertSame("Expected no defensive copy of extra", extra, jarEntry.getExtra()); - - ZipEntry clone = (ZipEntry) jarEntry.clone(); - assertEquals(JarEntry.class, clone.getClass()); - assertNotSame(extra, clone.getExtra()); + File f = createTemporaryZipFile(); + ZipOutputStream out = createZipOutputStream(f); + ZipEntry ze = new ZipEntry("x"); + ze.setExtra(maxLengthExtra); + out.putNextEntry(ze); + out.closeEntry(); + out.close(); + + // Read it back, and check that we see the entry. + ZipFile zipFile = new ZipFile(f); + assertEquals(maxLengthExtra.length, zipFile.getEntry("x").getExtra().length); + zipFile.close(); + } + + public void testTooLongComment() throws Exception { + String tooLongComment = makeString(65536, "z"); + ZipEntry ze = new ZipEntry("x"); + try { + ze.setComment(tooLongComment); + fail(); + } catch (IllegalArgumentException expected) { } + } + + public void testMaxLengthComment() throws Exception { + String maxLengthComment = makeString(65535, "z"); + + File f = createTemporaryZipFile(); + ZipOutputStream out = createZipOutputStream(f); + ZipEntry ze = new ZipEntry("x"); + ze.setComment(maxLengthComment); + out.putNextEntry(ze); + out.closeEntry(); + out.close(); + + // Read it back, and check that we see the entry. + ZipFile zipFile = new ZipFile(f); + assertEquals(maxLengthComment, zipFile.getEntry("x").getComment()); + zipFile.close(); + } } |