]> source.dussan.org Git - poi.git/commitdiff
60881 and 60891 -- on further look, no need to throw an exception for an encrypted...
authorTim Allison <tallison@apache.org>
Mon, 20 Mar 2017 20:47:15 +0000 (20:47 +0000)
committerTim Allison <tallison@apache.org>
Mon, 20 Mar 2017 20:47:15 +0000 (20:47 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1787846 13f79535-47bb-0310-9956-ffa450edef68

src/integrationtest/org/apache/poi/stress/XSSFBFileHandler.java
src/java/org/apache/poi/util/LittleEndianInputStream.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestSecureTempZip.java
test-data/spreadsheet/protected_passtika.xlsb [new file with mode: 0644]

index 02c5193d40b9f89e7943bdb3859147798267cd40..0d71de7e8af45838663a219a32d96a124a3990f3 100644 (file)
@@ -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
index 3109f88b2c55be761cf8a8003d5034a6e97b1c94..428b598d74b6568f1add33085380b7673765983a 100644 (file)
@@ -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);
index e6224adf4e4f5f3e8a9637711867ee9884a4b775..9fb149dcecdc24b2a88504102cf07415ef490031 100644 (file)
@@ -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 (file)
index 0000000..63405f1
Binary files /dev/null and b/test-data/spreadsheet/protected_passtika.xlsb differ