]> source.dussan.org Git - poi.git/commitdiff
More missing core properties unit tests, covering read-write without checks, and...
authorNick Burch <nick@apache.org>
Sat, 28 Feb 2015 17:37:11 +0000 (17:37 +0000)
committerNick Burch <nick@apache.org>
Sat, 28 Feb 2015 17:37:11 +0000 (17:37 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1662972 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java

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