From: Tim Allison Date: Mon, 20 Mar 2017 20:47:15 +0000 (+0000) Subject: 60881 and 60891 -- on further look, no need to throw an exception for an encrypted... X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=69c3311a98bad230ab3173645fc41875c012972c;p=poi.git 60881 and 60891 -- on further look, no need to throw an exception for an encrypted xlsb. On the second, let's fix readFully to read fully. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1787846 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java b/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java index 02c5193d40..0d71de7e8a 100644 --- a/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java @@ -33,7 +33,7 @@ public class XSSFBFileHandler extends AbstractFileHandler { static { //add expected failures here: -// AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/Simple.xlsb"); + AbstractFileHandler.EXPECTED_EXTRACTOR_FAILURES.add("spreadsheet/protected_passtika.xlsb"); } @Override diff --git a/src/java/org/apache/poi/util/LittleEndianInputStream.java b/src/java/org/apache/poi/util/LittleEndianInputStream.java index 3109f88b2c..428b598d74 100644 --- a/src/java/org/apache/poi/util/LittleEndianInputStream.java +++ b/src/java/org/apache/poi/util/LittleEndianInputStream.java @@ -28,6 +28,9 @@ import java.io.InputStream; * by this class is consistent with that of the inner stream. */ public class LittleEndianInputStream extends FilterInputStream implements LittleEndianInput { + + private static final int EOF = -1; + public LittleEndianInputStream(InputStream is) { super(is); } @@ -128,12 +131,28 @@ public class LittleEndianInputStream extends FilterInputStream implements Little @Override public void readFully(byte[] buf, int off, int len) { try { - checkEOF(read(buf, off, len), len); + checkEOF(_read(buf, off, len), len); } catch (IOException e) { throw new RuntimeException(e); } } + //Makes repeated calls to super.read() until length is read or EOF is reached + private int _read(byte[] buffer, int offset, int length) throws IOException { + //lifted directly from org.apache.commons.io.IOUtils 2.4 + int remaining = length; + while (remaining > 0) { + int location = length - remaining; + int count = read(buffer, offset + location, remaining); + if (EOF == count) { // EOF + break; + } + remaining -= count; + } + + return length - remaining; + } + @Override public void readPlain(byte[] buf, int off, int len) { readFully(buf, off, len); diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java index e6224adf4e..9fb149dcec 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java @@ -32,6 +32,7 @@ import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.poifs.crypt.temp.AesZipFileZipEntrySource; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.extractor.XSSFBEventBasedExcelExtractor; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.XmlException; @@ -79,4 +80,40 @@ public class TestSecureTempZip { poifs.close(); fis.close(); } + + /** + * Test case for #59841 - this is an example on how to use encrypted temp files, + * which are streamed into POI opposed to having everything in memory + */ + @Test + public void protectedXLSBZip() throws IOException, GeneralSecurityException, XmlException, OpenXML4JException { + File tikaProt = XSSFTestDataSamples.getSampleFile("protected_passtika.xlsb"); + FileInputStream fis = new FileInputStream(tikaProt); + POIFSFileSystem poifs = new POIFSFileSystem(fis); + EncryptionInfo ei = new EncryptionInfo(poifs); + Decryptor dec = ei.getDecryptor(); + boolean passOk = dec.verifyPassword("tika"); + assertTrue(passOk); + + // extract encrypted ooxml file and write to custom encrypted zip file + InputStream is = dec.getDataStream(poifs); + + // provide ZipEntrySource to poi which decrypts on the fly + ZipEntrySource source = AesZipFileZipEntrySource.createZipEntrySource(is); + + // test the source + OPCPackage opc = OPCPackage.open(source); + String expected = "You can't see me"; + + XSSFBEventBasedExcelExtractor extractor = new XSSFBEventBasedExcelExtractor(opc); + extractor.setIncludeSheetNames(false); + String txt = extractor.getText(); + assertEquals(expected, txt.trim()); + + extractor.close(); + opc.close(); + poifs.close(); + fis.close(); + } + } diff --git a/test-data/spreadsheet/protected_passtika.xlsb b/test-data/spreadsheet/protected_passtika.xlsb new file mode 100644 index 0000000000..63405f1b10 Binary files /dev/null and b/test-data/spreadsheet/protected_passtika.xlsb differ