summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorPaulo Casanova <pasc@google.com>2016-12-27 16:16:59 +0000
committerPaulo Casanova <pasc@google.com>2016-12-28 23:42:51 +0000
commitba150accda722df57396f79c0fb70df9fe9673db (patch)
treea55c59da3d2f70ead9a00a6d1fbb693a3d56fe0e /src/test
parenta96ce3418bbd07c9df96e15698a05f26b3e7d9ce (diff)
downloadapkzlib-ba150accda722df57396f79c0fb70df9fe9673db.tar.gz
Allow disabling version to extract in zip.
Add an option to ZFile to ignore the "version to extract" in the central directory header and in the local header. This allows supporting some zip tools that write weird values in the field. Test: JUnit tests included Change-Id: If8f14737ddb50f30508937e13f376cb30bde71e2
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/android/apkzlib/zip/ZFileTest.java109
1 files changed, 107 insertions, 2 deletions
diff --git a/src/test/java/com/android/apkzlib/zip/ZFileTest.java b/src/test/java/com/android/apkzlib/zip/ZFileTest.java
index 742b9bd..265ef00 100644
--- a/src/test/java/com/android/apkzlib/zip/ZFileTest.java
+++ b/src/test/java/com/android/apkzlib/zip/ZFileTest.java
@@ -45,6 +45,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
+import java.util.Locale;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -1394,8 +1395,6 @@ public class ZFileTest {
/*
* We should be complaining about the CRC32 somewhere...
*/
- boolean foundCrc32Complain = false;
-
assertTrue(
Throwables.getCausalChain(e).stream()
.map(Throwable::getMessage)
@@ -1413,4 +1412,110 @@ public class ZFileTest {
*/
}
}
+
+ @Test
+ public void detectIncorrectVersionToExtractInCentralDirectory() throws Exception {
+ File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
+
+ /*
+ * Create a valid zip file.
+ */
+ try (ZFile zf = new ZFile(zipFile)) {
+ zf.add("foo", new ByteArrayInputStream(new byte[0]));
+ }
+
+ /*
+ * Change the "version to extract" in the central directory to 0x7777.
+ */
+ int versionToExtractOffset =
+ ZFileTestConstants.LOCAL_HEADER_SIZE
+ + 3
+ + CentralDirectory.F_VERSION_EXTRACT.offset();
+ byte[] allZipBytes = Files.toByteArray(zipFile);
+ allZipBytes[versionToExtractOffset] = 0x77;
+ allZipBytes[versionToExtractOffset + 1] = 0x77;
+ Files.write(allZipBytes, zipFile);
+
+ /*
+ * Opening the file should fail.
+ */
+ try {
+ new ZFile(zipFile);
+ fail();
+ } catch (IOException e) {
+ /*
+ * We should complain about the version to extract somewhere...
+ */
+ assertTrue(
+ Throwables.getCausalChain(e).stream()
+ .map(Throwable::getMessage)
+ .anyMatch(s -> s.toLowerCase(Locale.US).contains("version")));
+ assertTrue(
+ Throwables.getCausalChain(e).stream()
+ .map(Throwable::getMessage)
+ .anyMatch(s -> s.toLowerCase(Locale.US).contains("extract")));
+ }
+
+ /*
+ * Setting the ignore version extract validation should allow the zip to be opened.
+ */
+ ZFileOptions options = new ZFileOptions();
+ options.setSkipZipVersionToExtractValidation(true);
+ try (ZFile zf = new ZFile(zipFile, options)) {
+ /*
+ * Nothing to do.
+ */
+ }
+ }
+ @Test
+ public void detectIncorrectVersionToExtractInLocalHeader() throws Exception {
+ File zipFile = new File(mTemporaryFolder.getRoot(), "a.zip");
+
+ /*
+ * Create a valid zip file.
+ */
+ try (ZFile zf = new ZFile(zipFile)) {
+ zf.add("foo", new ByteArrayInputStream(new byte[0]));
+ }
+
+ /*
+ * Change the "version to extract" in the local header to 0x7777.
+ */
+ int versionToExtractOffset = StoredEntry.F_VERSION_EXTRACT.offset();
+ byte[] allZipBytes = Files.toByteArray(zipFile);
+ allZipBytes[versionToExtractOffset] = 0x77;
+ allZipBytes[versionToExtractOffset + 1] = 0x77;
+ Files.write(allZipBytes, zipFile);
+
+ /*
+ * Opening the file should fail.
+ */
+ try {
+ new ZFile(zipFile);
+ fail();
+ } catch (IOException e) {
+ /*
+ * We should complain about the version to extract somewhere...
+ */
+ assertTrue(
+ Throwables.getCausalChain(e).stream()
+ .map(Throwable::getMessage)
+ .anyMatch(s -> s.toLowerCase(Locale.US).contains("version")));
+ assertTrue(
+ Throwables.getCausalChain(e).stream()
+ .map(Throwable::getMessage)
+ .anyMatch(s -> s.toLowerCase(Locale.US).contains("extract")));
+ }
+
+ /*
+ * Setting the ignore version extract validation should allow the zip to be opened.
+ */
+ ZFileOptions options = new ZFileOptions();
+ options.setSkipZipVersionToExtractValidation(true);
+ try (ZFile zf = new ZFile(zipFile, options)) {
+ /*
+ * Nothing to do.
+ */
+ }
+ }
}