harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r785124 - in /harmony/enhanced/classlib/trunk: modules/archive/src/main/java/java/util/zip/ modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ modules/archive/src/test/java/org/apache/harmony/archive/tests/java/ut...
Date Tue, 16 Jun 2009 09:27:47 GMT
Author: tellison
Date: Tue Jun 16 09:27:43 2009
New Revision: 785124

URL: http://svn.apache.org/viewvc?rev=785124&view=rev
Log:
Apply patch for HARMONY-6230 ([classlib][archive] ZipEntry close() leaves ZipInputStream in
an inconsistent state)

Added:
    harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_entry.jar
  (with props)
    harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_manifest.jar
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipInputStream.java
    harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
    harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
    harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipInputStream.java?rev=785124&r1=785123&r2=785124&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipInputStream.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/ZipInputStream.java
Tue Jun 16 09:27:43 2009
@@ -120,8 +120,24 @@
                 return;
             }
         }
+
+        /*
+         * The following code is careful to leave the ZipInputStream in a
+         * consistent state, even when close() results in an exception. It does
+         * so by:
+         *  - pushing bytes back into the source stream
+         *  - reading a data descriptor footer from the source stream
+         *  - resetting fields that manage the entry being closed
+         */
+
         // Ensure all entry bytes are read
-        skip(Long.MAX_VALUE);
+        Exception failure = null;
+        try {
+            skip(Long.MAX_VALUE);
+        } catch (Exception e) {
+            failure = e;
+        }
+
         int inB, out;
         if (currentEntry.compressionMethod == DEFLATED) {
             inB = inf.getTotalIn();
@@ -130,12 +146,38 @@
             inB = inRead;
             out = inRead;
         }
-        int diff = 0;
+        int diff = entryIn - inB;
         // Pushback any required bytes
-        if ((diff = entryIn - inB) != 0) {
+        if (diff != 0) {
             ((PushbackInputStream) in).unread(buf, len - diff, diff);
         }
 
+        try {
+            readAndVerifyDataDescriptor(inB, out);
+        } catch (Exception e) {
+            if (failure == null) { // otherwise we're already going to throw
+                failure = e;
+            }
+        }
+
+        inf.reset();
+        lastRead = inRead = entryIn = len = 0;
+        crc.reset();
+        currentEntry = null;
+
+        if (failure != null) {
+            if (failure instanceof IOException) {
+                throw (IOException) failure;
+            } else if (failure instanceof RuntimeException) {
+                throw (RuntimeException) failure;
+            }
+            AssertionError error = new AssertionError();
+            error.initCause(failure);
+            throw error;
+        }
+    }
+
+    private void readAndVerifyDataDescriptor(int inB, int out) throws IOException {
         if (hasDD) {
             in.read(hdrBuf, 0, EXTHDR);
             if (getLong(hdrBuf, 0) != EXTSIG) {
@@ -151,26 +193,19 @@
         if (currentEntry.compressedSize != inB || currentEntry.size != out) {
             throw new ZipException(Messages.getString("archive.21")); //$NON-NLS-1$
         }
-
-        inf.reset();
-        lastRead = inRead = entryIn = len = 0;
-        crc.reset();
-        currentEntry = null;
     }
 
     /**
-     * Reads the next entry from this {@code ZipInputStream}.
+     * Reads the next entry from this {@code ZipInputStream} or {@code null} if
+     * no more entries are present.
      * 
      * @return the next {@code ZipEntry} contained in the input stream.
      * @throws IOException
-     *             if the stream is not positioned at the beginning of an entry
-     *             or if an other {@code IOException} occurs.
+     *             if an {@code IOException} occurs.
      * @see ZipEntry
      */
     public ZipEntry getNextEntry() throws IOException {
-        if (currentEntry != null) {
-            closeEntry();
-        }
+        closeEntry();
         if (entriesEnd) {
             return null;
         }

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java?rev=785124&r1=785123&r2=785124&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
Tue Jun 16 09:27:43 2009
@@ -18,6 +18,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.File;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.HashSet;
@@ -26,6 +27,7 @@
 import java.util.jar.JarInputStream;
 import java.util.jar.Manifest;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
 
 import tests.support.resource.Support_Resources;
 
@@ -69,6 +71,61 @@
 		assertTrue("The jar input stream does not contain the correct entries",	hasCorrectEntry);
 	}
 
+    public void test_closeAfterException() throws Exception {
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "Broken_entry.jar");
+        InputStream is = Support_Resources.getStream("Broken_entry.jar");
+        JarInputStream jis = new JarInputStream(is, false);
+        jis.getNextEntry();
+        try {
+            jis.getNextEntry();
+            fail("ZipException expected");
+        } catch (ZipException ee) {
+            // expected
+        }
+        jis.close();
+        try {
+            jis.getNextEntry();
+            fail("IOException expected");
+        } catch (IOException ee) {
+            // expected
+        }
+    }
+
+    public void test_getNextJarEntry_Ex() throws Exception {
+        final Set<String> desired = new HashSet<String>(Arrays
+                .asList("foo/", "foo/bar/", "foo/bar/A.class", "Blah.txt"));
+        Set<String> actual = new HashSet<String>();
+        InputStream is = new URL(jarName).openConnection().getInputStream();
+        JarInputStream jis = new JarInputStream(is);
+        JarEntry je = jis.getNextJarEntry();
+        while (je != null) {
+            actual.add(je.toString());
+            je = jis.getNextJarEntry();
+        }
+        assertEquals(actual, desired);
+        jis.close();
+
+        try {
+            jis.getNextJarEntry();
+            fail("IOException expected");
+        } catch (IOException ee) {
+            // expected
+        }
+
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "Broken_entry.jar");
+        is = Support_Resources.getStream("Broken_entry.jar");
+        jis = new JarInputStream(is, false);
+        jis.getNextJarEntry();
+        try {
+            jis.getNextJarEntry();
+            fail("ZipException expected");
+        } catch (ZipException ee) {
+            // expected
+        }
+    }
+
 	/**
 	 * @tests java.util.jar.JarInputStream#getManifest()
 	 */
@@ -153,25 +210,17 @@
         InputStream is = new URL(modJarName).openConnection()
                 .getInputStream();
         JarInputStream jin = new JarInputStream(is, true);
-        ZipEntry zipEntry = null;
 
-        final int indexofDSA = 2;
-        final int totalEntries = 4;
-        int count = 0;
-        while (count == 0 || zipEntry != null) {
-            count++;
-            try {
-                zipEntry = jin.getNextEntry();
-                if (count == indexofDSA + 1) {
-                    fail("Should throw Security Exception");
-                }
-            } catch (SecurityException e) {
-                if (count != indexofDSA + 1) {
-                    throw e;
-                }
-            }
+        assertEquals("META-INF/TESTROOT.SF", jin.getNextEntry().getName());
+        assertEquals("META-INF/TESTROOT.DSA", jin.getNextEntry().getName());
+        try {
+            jin.getNextEntry();
+            fail();
+        } catch (SecurityException expected) {
         }
-        assertEquals(totalEntries + 2, count);
+        assertEquals("META-INF/", jin.getNextEntry().getName());
+        assertEquals("Test.class", jin.getNextEntry().getName());
+        assertNull(jin.getNextEntry());
         jin.close();
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java?rev=785124&r1=785123&r2=785124&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
Tue Jun 16 09:27:43 2009
@@ -185,6 +185,19 @@
                              "This is also text", new String(buf, 0, r));
 	}
 
+    public void test_getEntryLjava_lang_String_Ex() throws IOException {
+        java.util.zip.ZipEntry zentry = zfile.getEntry("File1.txt");
+        assertNotNull("Could not obtain ZipEntry", zentry);
+
+        zfile.close();
+        try {
+            zfile.getEntry("File2.txt");
+            fail("IllegalStateException expected");
+        } catch (IllegalStateException ee) {
+            // expected
+        }
+    }
+
 	/**
 	 * @tests java.util.zip.ZipFile#getInputStream(java.util.zip.ZipEntry)
 	 */

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java?rev=785124&r1=785123&r2=785124&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipInputStreamTest.java
Tue Jun 16 09:27:43 2009
@@ -18,9 +18,12 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
 import java.util.zip.ZipInputStream;
 import java.util.zip.ZipOutputStream;
 
@@ -110,6 +113,32 @@
 	    zis.closeEntry();
 	}
 
+    public void test_closeAfterException() throws Exception {
+        File resources = Support_Resources.createTempFolder();
+        Support_Resources.copyFile(resources, null, "Broken_manifest.jar");
+        FileInputStream fis = new FileInputStream(new File(resources,
+                "Broken_manifest.jar"));
+
+        ZipInputStream zis1 = new ZipInputStream(fis);
+
+        try {
+            for (int i = 0; i < 6; i++) {
+                zis1.getNextEntry();
+            }
+            fail("ZipException expected");
+        } catch (ZipException ee) {
+            // expected
+        }
+
+        zis1.close();
+        try {
+            zis1.getNextEntry();
+            fail("IOException expected");
+        } catch (IOException ee) {
+            // expected
+        }
+    }
+
 	/**
 	 * @tests java.util.zip.ZipInputStream#getNextEntry()
 	 */

Added: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_entry.jar
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_entry.jar?rev=785124&view=auto
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_entry.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_manifest.jar
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_manifest.jar?rev=785124&view=auto
==============================================================================
Binary file - no diff available.

Propchange: harmony/enhanced/classlib/trunk/support/src/test/java/tests/resources/Broken_manifest.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message