]> source.dussan.org Git - poi.git/commitdiff
Another test for NPOIFS in-place writing support - probably the deletion of an entry...
authorAndreas Beeker <kiwiwings@apache.org>
Sat, 2 Aug 2014 00:51:56 +0000 (00:51 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Sat, 2 Aug 2014 00:51:56 +0000 (00:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1615273 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java
src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionInfoBuilder.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java

index 9e1f231f82e7099f201ef28a8e4deded4b194cd5..f2f30642197c3de4c3a8926d386c71a60233aff0 100644 (file)
@@ -69,7 +69,13 @@ public class DataSpaceMapUtils {
         LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0);\r
         out.write(bos);\r
         \r
-        return dir.createDocument(parts[parts.length-1], bos.getWriteIndex(), new POIFSWriterListener(){\r
+        String fileName = parts[parts.length-1];\r
+        \r
+        if (dir.hasEntry(fileName)) {\r
+            dir.getEntry(fileName).delete();\r
+        }\r
+        \r
+        return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){\r
             public void processPOIFSWriterEvent(POIFSWriterEvent event) {\r
                 try {\r
                     event.getStream().write(buf, 0, event.getLimit());\r
index ef19cb85dbb2d9513f06bcae71210e4d095a45e0..10ca07674f29dc4464242ffbb6200fd1f4c598d5 100644 (file)
@@ -48,6 +48,7 @@ public class AgileEncryptionInfoBuilder implements EncryptionInfoBuilder {
         if (info.getVersionMajor() == EncryptionMode.agile.versionMajor\r
             && info.getVersionMinor() == EncryptionMode.agile.versionMinor) {\r
             decryptor = new AgileDecryptor(this);\r
+            encryptor = new AgileEncryptor(this);\r
         }\r
     }\r
 \r
index b04da659dcfbeb44d135fef7c55136b26b8d1fe4..3584b8f9bddb6f3b93b1f543e80dbf711c3185ed 100644 (file)
@@ -41,7 +41,10 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
 import org.apache.poi.util.BoundedInputStream;\r
 import org.apache.poi.util.IOUtils;\r
+import org.apache.poi.util.TempFile;\r
+import org.apache.poi.xwpf.usermodel.XWPFDocument;\r
 import org.junit.Assume;\r
+import org.junit.Ignore;\r
 import org.junit.Test;\r
 \r
 public class TestEncryptor {\r
@@ -227,6 +230,38 @@ public class TestEncryptor {
         assertThat(payloadExpected, equalTo(payloadActual));\r
     }\r
     \r
+    @Test\r
+    @Ignore\r
+    public void testInPlaceRewrite() throws Exception {\r
+        File f = TempFile.createTempFile("protected_agile", ".docx");\r
+        // File f = new File("protected_agile.docx");\r
+        FileOutputStream fos = new FileOutputStream(f);\r
+        InputStream fis = POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx");\r
+        IOUtils.copy(fis, fos);\r
+        fis.close();\r
+        fos.close();\r
+        \r
+        NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);\r
+\r
+        // decrypt the protected file - in this case it was encrypted with the default password\r
+        EncryptionInfo encInfo = new EncryptionInfo(fs);\r
+        Decryptor d = encInfo.getDecryptor();\r
+        boolean b = d.verifyPassword(Decryptor.DEFAULT_PASSWORD);\r
+        assertTrue(b);\r
+\r
+        // do some strange things with it ;)\r
+        XWPFDocument docx = new XWPFDocument(d.getDataStream(fs));\r
+        docx.getParagraphArray(0).insertNewRun(0).setText("POI was here! All your base are belong to us!");\r
+        docx.getParagraphArray(0).insertNewRun(1).addBreak();\r
+\r
+        // and encrypt it again\r
+        Encryptor e = encInfo.getEncryptor();\r
+        e.confirmPassword("AYBABTU");\r
+        docx.write(e.getDataStream(fs));\r
+        \r
+        fs.close();\r
+    }\r
+    \r
     \r
     private void listEntry(DocumentNode de, String ext, String path) throws IOException {\r
         path += "\\" + de.getName().replace('\u0006', '_');\r