]> source.dussan.org Git - poi.git/commitdiff
Add more output in case of unknown cipher-ids to aid in debugging bugs like 57195
authorDominik Stadler <centic@apache.org>
Wed, 26 Nov 2014 18:39:44 +0000 (18:39 +0000)
committerDominik Stadler <centic@apache.org>
Wed, 26 Nov 2014 18:39:44 +0000 (18:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1641883 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/crypt/CipherAlgorithm.java
src/testcases/org/apache/poi/poifs/crypt/TestCipherAlgorithm.java [new file with mode: 0644]

index 68682f496ca29ee0a637923a1e2df49709e0456c..cee49d2bee5abeb8ce6cc10c9f943e28d8e64d37 100644 (file)
@@ -64,7 +64,7 @@ public enum CipherAlgorithm {
         for (CipherAlgorithm ca : CipherAlgorithm.values()) {\r
             if (ca.ecmaId == ecmaId) return ca;\r
         }\r
-        throw new EncryptedDocumentException("cipher algorithm not found");\r
+        throw new EncryptedDocumentException("cipher algorithm " + ecmaId + " not found");\r
     }\r
     \r
     public static CipherAlgorithm fromXmlId(String xmlId, int keySize) {\r
@@ -74,8 +74,6 @@ public enum CipherAlgorithm {
                 if (ks == keySize) return ca;\r
             }\r
         }\r
-        throw new EncryptedDocumentException("cipher algorithm not found");\r
+        throw new EncryptedDocumentException("cipher algorithm " + xmlId + "/" + keySize + " not found");\r
     }\r
-    \r
-\r
 }
\ No newline at end of file
diff --git a/src/testcases/org/apache/poi/poifs/crypt/TestCipherAlgorithm.java b/src/testcases/org/apache/poi/poifs/crypt/TestCipherAlgorithm.java
new file mode 100644 (file)
index 0000000..814620b
--- /dev/null
@@ -0,0 +1,42 @@
+package org.apache.poi.poifs.crypt;
+
+import static org.junit.Assert.*;
+
+import org.apache.poi.EncryptedDocumentException;
+import org.junit.Test;
+
+public class TestCipherAlgorithm {
+    @Test
+    public void test() {
+        assertEquals(128, CipherAlgorithm.aes128.defaultKeySize);
+        
+        for(CipherAlgorithm alg : CipherAlgorithm.values()) {
+            assertEquals(alg, CipherAlgorithm.valueOf(alg.toString()));
+        }
+
+        assertEquals(CipherAlgorithm.aes128, CipherAlgorithm.fromEcmaId(0x660E));
+        assertEquals(CipherAlgorithm.aes192, CipherAlgorithm.fromXmlId("AES", 192));
+        
+        try {
+            CipherAlgorithm.fromEcmaId(0);
+            fail("Should throw exception");
+        } catch (EncryptedDocumentException e) {
+            // expected
+        }
+
+        try {
+            CipherAlgorithm.fromXmlId("AES", 1);
+            fail("Should throw exception");
+        } catch (EncryptedDocumentException e) {
+            // expected
+        }
+
+        try {
+            CipherAlgorithm.fromXmlId("RC1", 0x40);
+            fail("Should throw exception");
+        } catch (EncryptedDocumentException e) {
+            // expected
+        }
+    }
+
+}