ZipInputStreamTest: Add test cases for #available.
Documents differences between M and N.
bug: 27476977
Change-Id: I08f77966bc94efe8faf8c87e87429cc4e6483a62
diff --git a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
index 1dc22ca..4af2561 100644
--- a/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/ZipInputStreamTest.java
@@ -16,30 +16,20 @@
package libcore.java.util.zip;
+import junit.framework.TestCase;
+import libcore.io.Streams;
+import tests.support.resource.Support_Resources;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.HashSet;
-import java.util.List;
import java.util.Random;
-import java.util.Set;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
-import junit.framework.TestCase;
-
-import tests.support.resource.Support_Resources;
-
public final class ZipInputStreamTest extends TestCase {
public void testShortMessage() throws IOException {
@@ -115,4 +105,51 @@
zi.close();
}
+
+ public void testAvailable() throws Exception {
+ // NOTE: We don't care about the contents of any of these entries as long as they're
+ // not empty.
+ ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(
+ zip(new String[] { "foo", "bar", "baz" }, new byte[] { 0, 0, 0, 1, 1, 1 })));
+
+ assertEquals(1, zis.available());
+ zis.getNextEntry();
+ assertEquals(1, zis.available());
+ zis.closeEntry();
+ // On Android M and below, this call would return "1". That seems a bit odd given that the
+ // contract for available states that we should return 1 if there are any bytes left to read
+ // from the "current" entry.
+ assertEquals(0, zis.available());
+
+ // There shouldn't be any bytes left to read if the entry is fully consumed...
+ zis.getNextEntry();
+ Streams.readFullyNoClose(zis);
+ assertEquals(0, zis.available());
+
+ // ... or if the entry is fully skipped over.
+ zis.getNextEntry();
+ zis.skip(Long.MAX_VALUE);
+ assertEquals(0, zis.available());
+
+ // There are no entries left in the file, so there whould be nothing left to read.
+ assertNull(zis.getNextEntry());
+ assertEquals(0, zis.available());
+
+ zis.close();
+ }
+
+ private static byte[] zip(String[] names, byte[] bytes) throws IOException {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ ZipOutputStream zippedOut = new ZipOutputStream(bytesOut);
+
+ for (String name : names) {
+ ZipEntry entry = new ZipEntry(name);
+ zippedOut.putNextEntry(entry);
+ zippedOut.write(bytes);
+ zippedOut.closeEntry();
+ }
+
+ zippedOut.close();
+ return bytesOut.toByteArray();
+ }
}