From bacc106cb7cb5c7c3b841271cdc5f2f7a9f7b823 Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Sat, 2 Aug 2014 00:51:56 +0000 Subject: [PATCH] Another test for NPOIFS in-place writing support - probably the deletion of an entry causes the problem git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1615273 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/crypt/DataSpaceMapUtils.java | 8 ++++- .../agile/AgileEncryptionInfoBuilder.java | 1 + .../apache/poi/poifs/crypt/TestEncryptor.java | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java index 9e1f231f82..f2f3064219 100644 --- a/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java +++ b/src/java/org/apache/poi/poifs/crypt/DataSpaceMapUtils.java @@ -69,7 +69,13 @@ public class DataSpaceMapUtils { LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0); out.write(bos); - return dir.createDocument(parts[parts.length-1], bos.getWriteIndex(), new POIFSWriterListener(){ + String fileName = parts[parts.length-1]; + + if (dir.hasEntry(fileName)) { + dir.getEntry(fileName).delete(); + } + + return dir.createDocument(fileName, bos.getWriteIndex(), new POIFSWriterListener(){ public void processPOIFSWriterEvent(POIFSWriterEvent event) { try { event.getStream().write(buf, 0, event.getLimit()); diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionInfoBuilder.java b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionInfoBuilder.java index ef19cb85db..10ca07674f 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionInfoBuilder.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionInfoBuilder.java @@ -48,6 +48,7 @@ public class AgileEncryptionInfoBuilder implements EncryptionInfoBuilder { if (info.getVersionMajor() == EncryptionMode.agile.versionMajor && info.getVersionMinor() == EncryptionMode.agile.versionMinor) { decryptor = new AgileDecryptor(this); + encryptor = new AgileEncryptor(this); } } diff --git a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java index b04da659dc..3584b8f9bd 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java @@ -41,7 +41,10 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.BoundedInputStream; import org.apache.poi.util.IOUtils; +import org.apache.poi.util.TempFile; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.junit.Assume; +import org.junit.Ignore; import org.junit.Test; public class TestEncryptor { @@ -227,6 +230,38 @@ public class TestEncryptor { assertThat(payloadExpected, equalTo(payloadActual)); } + @Test + @Ignore + public void testInPlaceRewrite() throws Exception { + File f = TempFile.createTempFile("protected_agile", ".docx"); + // File f = new File("protected_agile.docx"); + FileOutputStream fos = new FileOutputStream(f); + InputStream fis = POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx"); + IOUtils.copy(fis, fos); + fis.close(); + fos.close(); + + NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false); + + // decrypt the protected file - in this case it was encrypted with the default password + EncryptionInfo encInfo = new EncryptionInfo(fs); + Decryptor d = encInfo.getDecryptor(); + boolean b = d.verifyPassword(Decryptor.DEFAULT_PASSWORD); + assertTrue(b); + + // do some strange things with it ;) + XWPFDocument docx = new XWPFDocument(d.getDataStream(fs)); + docx.getParagraphArray(0).insertNewRun(0).setText("POI was here! All your base are belong to us!"); + docx.getParagraphArray(0).insertNewRun(1).addBreak(); + + // and encrypt it again + Encryptor e = encInfo.getEncryptor(); + e.confirmPassword("AYBABTU"); + docx.write(e.getDataStream(fs)); + + fs.close(); + } + private void listEntry(DocumentNode de, String ext, String path) throws IOException { path += "\\" + de.getName().replace('\u0006', '_'); -- 2.39.5