]> source.dussan.org Git - poi.git/commitdiff
Unit test for bug #60128, showing that calling close on a broken package cleans up...
authorNick Burch <nick@apache.org>
Wed, 14 Sep 2016 12:35:34 +0000 (12:35 +0000)
committerNick Burch <nick@apache.org>
Wed, 14 Sep 2016 12:35:34 +0000 (12:35 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760693 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java
src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java
src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java

index 1d64ffe4eacb2052d45f0262434a969eb1f4736a..51ad32ce65fc91c1fa83008e9f81aaaa0ba7d820 100644 (file)
@@ -45,4 +45,9 @@ public interface ZipEntrySource {
         *  resources may be freed
         */
        public void close() throws IOException;
+       
+       /**
+        * Has close been called already?
+        */
+       public boolean isClosed();
 }
index f4117f44bfa9dae91c59a99f265848fcd94536c5..09317d361e200c5be954006ab6551e800197b685 100644 (file)
@@ -39,6 +39,9 @@ public class ZipFileZipEntrySource implements ZipEntrySource {
       }
       zipArchive = null;
    }
+   public boolean isClosed() {
+       return (zipArchive == null);
+   }
 
    public Enumeration<? extends ZipEntry> getEntries() {
       if (zipArchive == null)
index 36b69ac25b48d0749c3f69b73698fae7f5341a14..4c2b9df3e70884eff6d51c795d0603939ef69eb0 100644 (file)
@@ -76,6 +76,9 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource {
                // Free the memory
                zipEntries = null;
        }
+       public boolean isClosed() {
+           return (zipEntries == null);
+       }
        
        /**
         * Why oh why oh why are Iterator and Enumeration
index 7f174e07afcc17093e951daf34644a20f12c8dea..698f194c4cefc366a4371ca51ac0aaadd1d4ca80 100644 (file)
@@ -33,11 +33,14 @@ import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
 
+import org.apache.poi.POIDataSamples;
 import org.apache.poi.POITextExtractor;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.extractor.ExtractorFactory;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
+import org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException;
+import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
 import org.apache.poi.sl.usermodel.SlideShow;
 import org.apache.poi.sl.usermodel.SlideShowFactory;
 import org.apache.poi.ss.usermodel.Workbook;
@@ -194,6 +197,53 @@ public class TestZipPackage {
         } catch (Exception e) {
         }
         assertTrue("Can't delete tmp file", tmp.delete());
-
+    }
+    
+    /**
+     * If ZipPackage is passed an invalid file, a call to close
+     *  (eg from the OPCPackage open method) should tidy up the
+     *  stream / file the broken file is being read from.
+     * See bug #60128 for more
+     */
+    @Test
+    public void testTidyStreamOnInvalidFile() throws Exception {
+        // Spreadsheet has a good mix of alternate file types
+        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
+        
+        File[] notValidF = new File[] {
+                files.getFile("SampleSS.ods"), files.getFile("SampleSS.txt")
+        };
+        InputStream[] notValidS = new InputStream[] {
+                files.openResourceAsStream("SampleSS.ods"), files.openResourceAsStream("SampleSS.txt")
+        };
+
+        for (File notValid : notValidF) {
+            ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
+            assertNotNull(pkg.getZipArchive());
+            assertFalse(pkg.getZipArchive().isClosed());
+            try {
+                pkg.getParts();
+                fail("Shouldn't work");
+            } catch (ODFNotOfficeXmlFileException e) {
+            } catch (NotOfficeXmlFileException ne) {}
+            pkg.close();
+            
+            assertNotNull(pkg.getZipArchive());
+            assertTrue(pkg.getZipArchive().isClosed());
+        }
+        for (InputStream notValid : notValidS) {
+            ZipPackage pkg = new ZipPackage(notValid, PackageAccess.READ);
+            assertNotNull(pkg.getZipArchive());
+            assertFalse(pkg.getZipArchive().isClosed());
+            try {
+                pkg.getParts();
+                fail("Shouldn't work");
+            } catch (ODFNotOfficeXmlFileException e) {
+            } catch (NotOfficeXmlFileException ne) {}
+            pkg.close();
+            
+            assertNotNull(pkg.getZipArchive());
+            assertTrue(pkg.getZipArchive().isClosed());
+        }
     }
 }
index 4d4c5df345290d0adeb5ea6cf80f49a587fd1d10..868a382279e3be6c554ff75180b86a03b5c80814 100644 (file)
@@ -149,10 +149,12 @@ public class TestSecureTempZip {
     static class AesZipFileZipEntrySource implements ZipEntrySource {
         final ZipFile zipFile;
         final Cipher ci;
+        boolean closed;
 
         AesZipFileZipEntrySource(ZipFile zipFile, Cipher ci) {
             this.zipFile = zipFile;
             this.ci = ci;
+            this.closed = false;
         }
 
         /**
@@ -172,6 +174,12 @@ public class TestSecureTempZip {
         @Override
         public void close() throws IOException {
             zipFile.close();
+            closed = true;
+        }
+        
+        @Override
+        public boolean isClosed() {
+            return closed;
         }
     }
 }