You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2015/02/28 18:37:11 UTC

svn commit: r1662972 - in /poi/trunk/src/ooxml/testcases/org/apache/poi: openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java poifs/crypt/TestEncryptor.java

Author: nick
Date: Sat Feb 28 17:37:11 2015
New Revision: 1662972

URL: http://svn.apache.org/r1662972
Log:
More missing core properties unit tests, covering read-write without checks, and encryption

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

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java?rev=1662972&r1=1662971&r2=1662972&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/compliance/TestOPCComplianceCoreProperties.java Sat Feb 28 17:37:11 2015
@@ -237,14 +237,9 @@ public final class TestOPCComplianceCore
      */
     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());
@@ -261,6 +256,22 @@ public final class TestOPCComplianceCore
         assertEquals(1, pkg.getPartsByContentType(ContentTypes.CORE_PROPERTIES_PART).size());
         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());
     }
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java?rev=1662972&r1=1662971&r2=1662972&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/poifs/crypt/TestEncryptor.java Sat Feb 28 17:37:11 2015
@@ -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 {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org