More missing core properties unit tests, covering read-write without checks, and encryption

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1662972 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-02-28 17:37:11 +00:00
parent 3ba7b59afc
commit 2ccd6db57e
2 changed files with 71 additions and 7 deletions

View File

@ -237,14 +237,9 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
*/
public void testNoCoreProperties_saveNew() throws Exception {
String sampleFileName = "OPCCompliance_NoCoreProperties.xlsx";
OPCPackage pkg = null;
try {
pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
} catch (Exception e) {
throw new RuntimeException(e);
}
OPCPackage pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
// Empty properties
// Verify it has empty properties
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
@ -262,6 +257,22 @@ public final class TestOPCComplianceCoreProperties extends TestCase {
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
// Open a new copy of it
pkg = OPCPackage.open(POIDataSamples.getOpenXML4JInstance().getFile(sampleFileName).getPath());
// Save and re-load, without having touched the properties yet
baos = new ByteArrayOutputStream();
pkg.save(baos);
bais = new ByteArrayInputStream(baos.toByteArray());
pkg = OPCPackage.open(bais);
// Check that this too added empty properties without error
assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
}
/**

View File

@ -18,6 +18,8 @@ package org.apache.poi.poifs.crypt;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
@ -32,6 +34,8 @@ import java.util.Iterator;
import javax.crypto.Cipher;
import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.ContentTypes;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.crypt.agile.AgileEncryptionHeader;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.DocumentNode;
@ -266,6 +270,55 @@ public class TestEncryptor {
assertArrayEquals(payloadExpected, payloadActual);
}
/**
* Ensure we can encrypt a package that is missing the Core
* Properties, eg one from dodgy versions of Jasper Reports
* See https://github.com/nestoru/xlsxenc/ and
* http://stackoverflow.com/questions/28593223
*/
@Test
public void encryptPackageWithoutCoreProperties() throws Exception {
// Open our file without core properties
File inp = POIDataSamples.getOpenXML4JInstance().getFile("OPCCompliance_NoCoreProperties.xlsx");
OPCPackage pkg = OPCPackage.open(inp.getPath());
// It doesn't have any core properties yet
assertEquals(0, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(pkg.getPackageProperties());
assertNotNull(pkg.getPackageProperties().getLanguageProperty());
assertNull(pkg.getPackageProperties().getLanguageProperty().getValue());
// Encrypt it
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
NPOIFSFileSystem fs = new NPOIFSFileSystem();
Encryptor enc = info.getEncryptor();
enc.confirmPassword("password");
OutputStream os = enc.getDataStream(fs);
pkg.save(os);
pkg.revert();
// Save the resulting OLE2 document, and re-open it
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fs.writeFilesystem(baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
NPOIFSFileSystem inpFS = new NPOIFSFileSystem(bais);
// Check we can decrypt it
info = new EncryptionInfo(inpFS);
Decryptor d = Decryptor.getInstance(info);
assertEquals(true, d.verifyPassword("password"));
OPCPackage inpPkg = OPCPackage.open(d.getDataStream(inpFS));
// Check it now has empty core properties
assertEquals(1, inpPkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
assertNotNull(inpPkg.getPackageProperties());
assertNotNull(inpPkg.getPackageProperties().getLanguageProperty());
assertNull(inpPkg.getPackageProperties().getLanguageProperty().getValue());
}
@Test
@Ignore
public void inPlaceRewrite() throws Exception {