]> source.dussan.org Git - poi.git/commitdiff
Patch from Andreas Beeker from bug #53475 - Switch XWPF test to JUnit 4, so that...
authorNick Burch <nick@apache.org>
Thu, 21 Nov 2013 11:18:23 +0000 (11:18 +0000)
committerNick Burch <nick@apache.org>
Thu, 21 Nov 2013 11:18:23 +0000 (11:18 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1544121 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/crypt/AgileDecryptor.java
src/ooxml/testcases/org/apache/poi/xwpf/AllXWPFTests.java
src/ooxml/testcases/org/apache/poi/xwpf/TestXWPFBugs.java

index 031aff5a24cb4fa30e6c7d249480bb6f2475b667..401049b0dd1bc308b6811225e3e3334b72c48b9d 100644 (file)
@@ -194,6 +194,7 @@ public class AgileDecryptor extends Decryptor {
 
     private Cipher getCipher(SecretKey key, byte[] vec)
         throws GeneralSecurityException {
+        
         String name = null;
         String chain = null;
 
@@ -209,6 +210,11 @@ public class AgileDecryptor extends Decryptor {
              throw new EncryptedDocumentException("Unsupported algorithm");
         }
 
+        // Ensure the JCE policies files allow for this sized key
+        if (Cipher.getMaxAllowedKeyLength(name) < _info.getHeader().getKeySize()) {
+            throw new EncryptedDocumentException("Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files");
+        }
+        
         switch (verifier.getCipherMode()) {
           case EncryptionHeader.MODE_CBC: 
               chain = "CBC"; 
@@ -219,7 +225,7 @@ public class AgileDecryptor extends Decryptor {
           default: 
               throw new EncryptedDocumentException("Unsupported chain mode");
         }
-
+        
         Cipher cipher = Cipher.getInstance(name + "/" + chain + "/NoPadding");
         IvParameterSpec iv = new IvParameterSpec(vec);
         cipher.init(Cipher.DECRYPT_MODE, key, iv);
index 12b768bba9b2765c763539cab265b04188b6fc79..20a4898e0d447ffe27bb3620ab62e3ddc3be3440 100644 (file)
@@ -17,9 +17,6 @@
 
 package org.apache.poi.xwpf;
 
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
 import org.apache.poi.xwpf.extractor.TestXWPFWordExtractor;
 import org.apache.poi.xwpf.model.TestXWPFHeaderFooterPolicy;
 import org.apache.poi.xwpf.usermodel.TestXWPFDocument;
@@ -31,30 +28,28 @@ import org.apache.poi.xwpf.usermodel.TestXWPFPictureData;
 import org.apache.poi.xwpf.usermodel.TestXWPFRun;
 import org.apache.poi.xwpf.usermodel.TestXWPFStyles;
 import org.apache.poi.xwpf.usermodel.TestXWPFTable;
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
 
 /**
  * Collects all tests for <tt>org.apache.poi.xwpf</tt> and sub-packages.
- *
- * @author Josh Micich
  */
+@RunWith(Suite.class)
+@Suite.SuiteClasses({
+    TestXWPFBugs.class,
+    TestXWPFDocument.class,
+    TestXWPFWordExtractor.class,
+    TestXWPFHeaderFooterPolicy.class,
+    TestXWPFHeader.class,
+    TestXWPFHeadings.class,
+    TestXWPFParagraph.class,
+    TestXWPFRun.class,
+    TestXWPFTable.class,
+    TestXWPFStyles.class,
+    TestXWPFPictureData.class,
+    TestXWPFNumbering.class,
+    TestAllExtendedProperties.class,
+    TestPackageCorePropertiesGetKeywords.class
+})
 public final class AllXWPFTests {
-
-       public static Test suite() {
-               TestSuite result = new TestSuite(AllXWPFTests.class.getName());
-               result.addTestSuite(TestXWPFBugs.class);
-               result.addTestSuite(TestXWPFDocument.class);
-               result.addTestSuite(TestXWPFWordExtractor.class);
-               result.addTestSuite(TestXWPFHeaderFooterPolicy.class);
-               result.addTestSuite(TestXWPFHeader.class);
-               result.addTestSuite(TestXWPFHeadings.class);
-               result.addTestSuite(TestXWPFParagraph.class);
-               result.addTestSuite(TestXWPFRun.class);
-               result.addTestSuite(TestXWPFTable.class);
-               result.addTestSuite(TestXWPFStyles.class);
-               result.addTestSuite(TestXWPFPictureData.class);
-               result.addTestSuite(TestXWPFNumbering.class);
-               result.addTestSuite(TestAllExtendedProperties.class);
-               result.addTestSuite(TestPackageCorePropertiesGetKeywords.class);
-               return result;
-       }
-}
+}
\ No newline at end of file
index ade755951ff1f11dba28ca281266de604644152b..a0a97cd1166e482fdceb0a4f858db122fdd062f7 100644 (file)
@@ -1,12 +1,15 @@
 package org.apache.poi.xwpf;\r
 \r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotNull;\r
+import static org.junit.Assert.assertTrue;\r
+\r
 import java.io.File;\r
 import java.io.InputStream;\r
 \r
-import junit.framework.TestCase;\r
+import javax.crypto.Cipher;\r
 \r
 import org.apache.poi.POIDataSamples;\r
-import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;\r
 import org.apache.poi.openxml4j.opc.OPCPackage;\r
 import org.apache.poi.poifs.crypt.Decryptor;\r
 import org.apache.poi.poifs.crypt.EncryptionHeader;\r
@@ -14,75 +17,72 @@ import org.apache.poi.poifs.crypt.EncryptionInfo;
 import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;\r
 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;\r
 import org.apache.poi.xwpf.usermodel.XWPFDocument;\r
+import org.junit.Assume;\r
+import org.junit.Test;\r
 \r
-public class TestXWPFBugs extends TestCase {\r
+public class TestXWPFBugs {\r
     /**\r
      * A word document that's encrypted with non-standard\r
      *  Encryption options, and no cspname section. See bug 53475\r
      */\r
-    public void test53475NoCSPName() throws Exception {\r
-        try {\r
-            Biff8EncryptionKey.setCurrentUserPassword("solrcell");\r
-            File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");\r
-            NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);\r
+    @Test\r
+    public void bug53475NoCSPName() throws Exception {\r
+        File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-solrcell.docx");\r
+        NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);\r
 \r
-            // Check the encryption details\r
-            EncryptionInfo info = new EncryptionInfo(filesystem);\r
-            assertEquals(128, info.getHeader().getKeySize());\r
-            assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm());\r
-            assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());\r
+        // Check the encryption details\r
+        EncryptionInfo info = new EncryptionInfo(filesystem);\r
+        assertEquals(128, info.getHeader().getKeySize());\r
+        assertEquals(EncryptionHeader.ALGORITHM_AES_128, info.getHeader().getAlgorithm());\r
+        assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());\r
 \r
-            // Check it can be decoded\r
-            Decryptor d = Decryptor.getInstance(info);         \r
-            assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));\r
+        // Check it can be decoded\r
+        Decryptor d = Decryptor.getInstance(info);             \r
+        assertTrue("Unable to process: document is encrypted", d.verifyPassword("solrcell"));\r
 \r
-            // Check we can read the word document in that\r
-            InputStream dataStream = d.getDataStream(filesystem);\r
-            OPCPackage opc = OPCPackage.open(dataStream);\r
-            XWPFDocument doc = new XWPFDocument(opc);\r
-            XWPFWordExtractor ex = new XWPFWordExtractor(doc);\r
-            String text = ex.getText();\r
-            assertNotNull(text);\r
-            assertEquals("This is password protected Word document.", text.trim());\r
-            ex.close();\r
-        } finally {\r
-            Biff8EncryptionKey.setCurrentUserPassword(null);\r
-        }\r
+        // Check we can read the word document in that\r
+        InputStream dataStream = d.getDataStream(filesystem);\r
+        OPCPackage opc = OPCPackage.open(dataStream);\r
+        XWPFDocument doc = new XWPFDocument(opc);\r
+        XWPFWordExtractor ex = new XWPFWordExtractor(doc);\r
+        String text = ex.getText();\r
+        assertNotNull(text);\r
+        assertEquals("This is password protected Word document.", text.trim());\r
+        ex.close();\r
     }\r
 \r
     /**\r
      * A word document with aes-256, i.e. aes is always 128 bit (= 128 bit block size),\r
      * but the key can be 128/192/256 bits\r
      */\r
-    public void test53475_aes256() throws Exception {\r
-        try {\r
-            Biff8EncryptionKey.setCurrentUserPassword("pass");\r
-            File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");\r
-            NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);\r
+    @Test\r
+    public void bug53475_aes256() throws Exception {\r
+        int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");\r
+        Assume.assumeTrue("Please install JCE Unlimited Strength Jurisdiction Policy files for AES 256", maxKeyLen == 2147483647);\r
+\r
+        File file = POIDataSamples.getDocumentInstance().getFile("bug53475-password-is-pass.docx");\r
+        NPOIFSFileSystem filesystem = new NPOIFSFileSystem(file, true);\r
 \r
-            // Check the encryption details\r
-            EncryptionInfo info = new EncryptionInfo(filesystem);\r
-            assertEquals(16, info.getHeader().getBlockSize());\r
-            assertEquals(256, info.getHeader().getKeySize());\r
-            assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm());\r
-            assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());\r
+        // Check the encryption details\r
+        EncryptionInfo info = new EncryptionInfo(filesystem);\r
+        assertEquals(16, info.getHeader().getBlockSize());\r
+        assertEquals(256, info.getHeader().getKeySize());\r
+        assertEquals(EncryptionHeader.ALGORITHM_AES_256, info.getHeader().getAlgorithm());\r
+        assertEquals(EncryptionHeader.HASH_SHA1, info.getHeader().getHashAlgorithm());\r
 \r
-            // Check it can be decoded\r
-            Decryptor d = Decryptor.getInstance(info);         \r
-            assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));\r
+        // Check it can be decoded\r
+        Decryptor d = Decryptor.getInstance(info);             \r
+        assertTrue("Unable to process: document is encrypted", d.verifyPassword("pass"));\r
 \r
-            // Check we can read the word document in that\r
-            InputStream dataStream = d.getDataStream(filesystem);\r
-            OPCPackage opc = OPCPackage.open(dataStream);\r
-            XWPFDocument doc = new XWPFDocument(opc);\r
-            XWPFWordExtractor ex = new XWPFWordExtractor(doc);\r
-            String text = ex.getText();\r
-            assertNotNull(text);\r
-            // I know ... a stupid typo, maybe next time ...\r
-            assertEquals("The is a password protected document.", text.trim());\r
-            ex.close();\r
-        } finally {\r
-            Biff8EncryptionKey.setCurrentUserPassword(null);\r
-        }\r
+        // Check we can read the word document in that\r
+        InputStream dataStream = d.getDataStream(filesystem);\r
+        OPCPackage opc = OPCPackage.open(dataStream);\r
+        XWPFDocument doc = new XWPFDocument(opc);\r
+        XWPFWordExtractor ex = new XWPFWordExtractor(doc);\r
+        String text = ex.getText();\r
+        assertNotNull(text);\r
+        // I know ... a stupid typo, maybe next time ...\r
+        assertEquals("The is a password protected document.", text.trim());\r
+        ex.close();\r
     }\r
 }\r