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
This commit is contained in:
Andreas Beeker 2014-08-02 00:51:56 +00:00
parent ceb9427afb
commit bacc106cb7
3 changed files with 43 additions and 1 deletions

View File

@ -69,7 +69,13 @@ public class DataSpaceMapUtils {
LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0); LittleEndianByteArrayOutputStream bos = new LittleEndianByteArrayOutputStream(buf, 0);
out.write(bos); 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) { public void processPOIFSWriterEvent(POIFSWriterEvent event) {
try { try {
event.getStream().write(buf, 0, event.getLimit()); event.getStream().write(buf, 0, event.getLimit());

View File

@ -48,6 +48,7 @@ public class AgileEncryptionInfoBuilder implements EncryptionInfoBuilder {
if (info.getVersionMajor() == EncryptionMode.agile.versionMajor if (info.getVersionMajor() == EncryptionMode.agile.versionMajor
&& info.getVersionMinor() == EncryptionMode.agile.versionMinor) { && info.getVersionMinor() == EncryptionMode.agile.versionMinor) {
decryptor = new AgileDecryptor(this); decryptor = new AgileDecryptor(this);
encryptor = new AgileEncryptor(this);
} }
} }

View File

@ -41,7 +41,10 @@ import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.BoundedInputStream; import org.apache.poi.util.BoundedInputStream;
import org.apache.poi.util.IOUtils; 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.Assume;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class TestEncryptor { public class TestEncryptor {
@ -227,6 +230,38 @@ public class TestEncryptor {
assertThat(payloadExpected, equalTo(payloadActual)); 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 { private void listEntry(DocumentNode de, String ext, String path) throws IOException {
path += "\\" + de.getName().replace('\u0006', '_'); path += "\\" + de.getName().replace('\u0006', '_');