]> source.dussan.org Git - poi.git/commitdiff
record size of encrypted temp data
authorPJ Fanning <fanningpj@apache.org>
Sat, 16 Oct 2021 10:30:43 +0000 (10:30 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 16 Oct 2021 10:30:43 +0000 (10:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1894303 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/EncryptedTempFilePackagePart.java
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java
poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestEncryptedTempFilePackagePart.java
poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestTempFilePackagePart.java
poi/src/test/java9/module-info.class

index a9119eaf42552281f09fe1ddb513936dc2b1ca5a..a64326c32ab7e05254e2cc8a81c7918da08e7b6c 100644 (file)
@@ -98,12 +98,9 @@ public final class EncryptedTempFilePackagePart extends PackagePart {
         return tempFile.getOutputStream();
     }
 
-    /**
-     * @return EncryptedTempData.getSize() always returns -1
-     */
     @Override
     public long getSize() {
-        return -1;
+        return tempFile.getByteCount();
     }
 
     @Override
index 9f219f1a803629b13f16bff237cba1620c8e6f86..d0fe43a9c8779d523b909289248fba19a99ecbf1 100644 (file)
@@ -32,6 +32,7 @@ import javax.crypto.CipherInputStream;
 import javax.crypto.CipherOutputStream;
 import javax.crypto.spec.SecretKeySpec;
 
+import org.apache.commons.io.output.CountingOutputStream;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.poi.poifs.crypt.ChainingMode;
@@ -52,7 +53,8 @@ public class EncryptedTempData {
     private final SecretKeySpec skeySpec;
     private final byte[] ivBytes;
     private final File tempFile;
-    
+    private CountingOutputStream outputStream;
+
     public EncryptedTempData() throws IOException {
         SecureRandom sr = new SecureRandom();
         ivBytes = new byte[16];
@@ -72,7 +74,8 @@ public class EncryptedTempData {
      */
     public OutputStream getOutputStream() throws IOException {
         Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
-        return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc);
+        outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc));
+        return outputStream;
     }
 
     /**
@@ -86,6 +89,13 @@ public class EncryptedTempData {
         return new CipherInputStream(new FileInputStream(tempFile), ciDec);
     }
 
+    /**
+     * @return number of bytes stored in the temp data file (the number you should expect after you decrypt the data)
+     */
+    public long getByteCount() {
+        return outputStream == null ? 0 : outputStream.getByteCount();
+    }
+
     /**
      * Removes the temporarily backing file
      */
index 4f0524b2eae017cd94705e0c130cc66122bc9d67..1d9f3d411e421adbc72737762e2c1c13ff1d92e5 100644 (file)
@@ -33,15 +33,16 @@ public class TestEncryptedTempFilePackagePart {
     @Test
     void testRoundTrip() throws Exception {
         String text = UUID.randomUUID().toString();
+        byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
         String filepath =  OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
 
         try (OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ)) {
             PackagePartName name = new PackagePartName("/test.txt", true);
             EncryptedTempFilePackagePart part = new EncryptedTempFilePackagePart(p, name, "text/plain");
             try (OutputStream os = part.getOutputStream()) {
-                os.write(text.getBytes(StandardCharsets.UTF_8));
+                os.write(bytes);
             }
-            assertEquals(-1, part.getSize());
+            assertEquals(bytes.length, part.getSize());
             try (InputStream is = part.getInputStream()) {
                 assertEquals(text, new String(IOUtils.toByteArray(is), StandardCharsets.UTF_8));
             }
index c30dbcf18b5fd7f19f779f6200962a384e1b87f2..768054acec5e1841ee8c0d435cc85b7a303ccc8d 100644 (file)
@@ -40,7 +40,7 @@ public class TestTempFilePackagePart {
             PackagePartName name = new PackagePartName("/test.txt", true);
             TempFilePackagePart part = new TempFilePackagePart(p, name, "text/plain");
             try (OutputStream os = part.getOutputStream()) {
-                os.write(text.getBytes(StandardCharsets.UTF_8));
+                os.write(bytes);
             }
             assertEquals(bytes.length, part.getSize());
             try (InputStream is = part.getInputStream()) {
index 438e778ab515b0d480b1fb79ccaffeeafa4e55c6..028b943d0ae7577c19f08d4b20ce352134f4f685 100644 (file)
Binary files a/poi/src/test/java9/module-info.class and b/poi/src/test/java9/module-info.class differ