diff options
author | Nick Burch <nick@apache.org> | 2015-02-28 17:37:11 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2015-02-28 17:37:11 +0000 |
commit | 4327905c99187a506555593d9c625f92cc794ec9 (patch) | |
tree | 2f6d08db6a7fd5d6b9483891e0d6bf1e9ac74a59 | |
parent | e43ab85691f6ad606038ed9269c2064880b3be84 (diff) | |
download | poi-4327905c99187a506555593d9c625f92cc794ec9.tar.gz poi-4327905c99187a506555593d9c625f92cc794ec9.zip |
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
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java | 25 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java | 53 |
2 files changed, 71 insertions, 7 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java index cc6f7ca83d..b9517ba2ef 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java @@ -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()); } /** 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 37deba5e82..2cd9889bd2 100644 --- a/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java +++ b/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java @@ -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 {
|