You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2016/09/14 12:35:35 UTC
svn commit: r1760693 - in /poi/trunk/src/ooxml:
java/org/apache/poi/openxml4j/util/ testcases/org/apache/poi/openxml4j/opc/
testcases/org/apache/poi/poifs/crypt/
Author: nick
Date: Wed Sep 14 12:35:34 2016
New Revision: 1760693
URL: http://svn.apache.org/viewvc?rev=1760693&view=rev
Log:
Unit test for bug #60128, showing that calling close on a broken package cleans up file or stream
Modified:
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java
poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java
poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java
Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java?rev=1760693&r1=1760692&r2=1760693&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipEntrySource.java Wed Sep 14 12:35:34 2016
@@ -45,4 +45,9 @@ public interface ZipEntrySource {
* resources may be freed
*/
public void close() throws IOException;
+
+ /**
+ * Has close been called already?
+ */
+ public boolean isClosed();
}
Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java?rev=1760693&r1=1760692&r2=1760693&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipFileZipEntrySource.java Wed Sep 14 12:35:34 2016
@@ -39,6 +39,9 @@ public class ZipFileZipEntrySource imple
}
zipArchive = null;
}
+ public boolean isClosed() {
+ return (zipArchive == null);
+ }
public Enumeration<? extends ZipEntry> getEntries() {
if (zipArchive == null)
Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java?rev=1760693&r1=1760692&r2=1760693&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java Wed Sep 14 12:35:34 2016
@@ -76,6 +76,9 @@ public class ZipInputStreamZipEntrySourc
// Free the memory
zipEntries = null;
}
+ public boolean isClosed() {
+ return (zipEntries == null);
+ }
/**
* Why oh why oh why are Iterator and Enumeration
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java?rev=1760693&r1=1760692&r2=1760693&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestZipPackage.java Wed Sep 14 12:35:34 2016
@@ -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());
+ }
}
}
Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java?rev=1760693&r1=1760692&r2=1760693&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java Wed Sep 14 12:35:34 2016
@@ -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;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org