Unit test for bug #60128, showing that calling close on a broken package cleans up file or stream
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1760693 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ed7258906f
commit
6219cc6664
@ -45,4 +45,9 @@ public interface ZipEntrySource {
|
|||||||
* resources may be freed
|
* resources may be freed
|
||||||
*/
|
*/
|
||||||
public void close() throws IOException;
|
public void close() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Has close been called already?
|
||||||
|
*/
|
||||||
|
public boolean isClosed();
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,9 @@ public class ZipFileZipEntrySource implements ZipEntrySource {
|
|||||||
}
|
}
|
||||||
zipArchive = null;
|
zipArchive = null;
|
||||||
}
|
}
|
||||||
|
public boolean isClosed() {
|
||||||
|
return (zipArchive == null);
|
||||||
|
}
|
||||||
|
|
||||||
public Enumeration<? extends ZipEntry> getEntries() {
|
public Enumeration<? extends ZipEntry> getEntries() {
|
||||||
if (zipArchive == null)
|
if (zipArchive == null)
|
||||||
|
@ -76,6 +76,9 @@ public class ZipInputStreamZipEntrySource implements ZipEntrySource {
|
|||||||
// Free the memory
|
// Free the memory
|
||||||
zipEntries = null;
|
zipEntries = null;
|
||||||
}
|
}
|
||||||
|
public boolean isClosed() {
|
||||||
|
return (zipEntries == null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Why oh why oh why are Iterator and Enumeration
|
* Why oh why oh why are Iterator and Enumeration
|
||||||
|
@ -33,11 +33,14 @@ import java.io.OutputStreamWriter;
|
|||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
|
import org.apache.poi.POIDataSamples;
|
||||||
import org.apache.poi.POITextExtractor;
|
import org.apache.poi.POITextExtractor;
|
||||||
import org.apache.poi.POIXMLException;
|
import org.apache.poi.POIXMLException;
|
||||||
import org.apache.poi.extractor.ExtractorFactory;
|
import org.apache.poi.extractor.ExtractorFactory;
|
||||||
import org.apache.poi.hssf.HSSFTestDataSamples;
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
|
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.SlideShow;
|
||||||
import org.apache.poi.sl.usermodel.SlideShowFactory;
|
import org.apache.poi.sl.usermodel.SlideShowFactory;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
@ -194,6 +197,53 @@ public class TestZipPackage {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
assertTrue("Can't delete tmp file", tmp.delete());
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,10 +149,12 @@ public class TestSecureTempZip {
|
|||||||
static class AesZipFileZipEntrySource implements ZipEntrySource {
|
static class AesZipFileZipEntrySource implements ZipEntrySource {
|
||||||
final ZipFile zipFile;
|
final ZipFile zipFile;
|
||||||
final Cipher ci;
|
final Cipher ci;
|
||||||
|
boolean closed;
|
||||||
|
|
||||||
AesZipFileZipEntrySource(ZipFile zipFile, Cipher ci) {
|
AesZipFileZipEntrySource(ZipFile zipFile, Cipher ci) {
|
||||||
this.zipFile = zipFile;
|
this.zipFile = zipFile;
|
||||||
this.ci = ci;
|
this.ci = ci;
|
||||||
|
this.closed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -172,6 +174,12 @@ public class TestSecureTempZip {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
zipFile.close();
|
zipFile.close();
|
||||||
|
closed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isClosed() {
|
||||||
|
return closed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user