public inbox for mauve-patches@sourceware.org
 help / color / mirror / Atom feed
* FYI: Check for correct exceptions for TestOfCipherEngineInit
@ 2006-06-15 23:17 Vivek Lakshmanan
  0 siblings, 0 replies; only message in thread
From: Vivek Lakshmanan @ 2006-06-15 23:17 UTC (permalink / raw)
  To: mauve-patches

[-- Attachment #1: Type: text/plain, Size: 463 bytes --]

The following _already committed_ updates the testInitWithParameterSpec 
test with a check for the correct exception expected. Also provides more 
comments for the logic involved in this test case.
Thanks,
Vivek

2006-06-15  Vivek Lakshmanan  <vivekl@redhat.com>

    * gnu/testlet/gnu/javax/crypto/jce/TestOfCipherEngineInit.java
    (testInitWithParameterSpec): More comments and look for
    InvalidAlgorithmParameterException instead of InvalidKeyException.


[-- Attachment #2: mauve-TestOfCipherEngineInit.patch --]
[-- Type: text/x-patch, Size: 4665 bytes --]

Index: gnu/testlet/gnu/javax/crypto/jce/TestOfCipherEngineInit.java
===================================================================
RCS file: /cvs/mauve/mauve/gnu/testlet/gnu/javax/crypto/jce/TestOfCipherEngineInit.java,v
retrieving revision 1.2
diff -u -r1.2 TestOfCipherEngineInit.java
--- gnu/testlet/gnu/javax/crypto/jce/TestOfCipherEngineInit.java	15 Jun 2006 16:55:36 -0000	1.2
+++ gnu/testlet/gnu/javax/crypto/jce/TestOfCipherEngineInit.java	15 Jun 2006 23:02:05 -0000
@@ -28,6 +28,7 @@
 import gnu.testlet.TestHarness;
 import gnu.testlet.Testlet;
 
+import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.Key;
 import java.security.SecureRandom;
@@ -165,13 +166,15 @@
   // If param is null, and the cipher needs algorithm parameters to
   // function then init should create random/default parameters provided
   // the cipher is in ENCRYPT or WRAP mode, if in DECRYPT or UNWRAP mode
-  // the function must throw an InvalidKeyException.
+  // the function must throw an InvalidAlgorithmParameterException.
   // Extrapolation: If algorithm does not require additional params
   // then none should be created?
   private void testInitWithParameterSpec(TestHarness harness)
   {
     try
       {
+        // This cipher does not need extra algorithm parameters like
+        // an IV to be provided so it should not generate one.
         cipher = Cipher.getInstance("DESede/ECB/NoPadding");
         String input = "Does this work ?";
 
@@ -184,6 +187,7 @@
         byte[] ciphertext = cipher.doFinal(plaintext);
 
         iv = null;
+        // No need for an IV so none should be generated in decrypt mode either.
         cipher.init(Cipher.DECRYPT_MODE, key, (AlgorithmParameterSpec) null);
         iv = cipher.getIV();
         harness.check(
@@ -192,6 +196,7 @@
         byte[] plaintext2 = cipher.doFinal(ciphertext);
         String recovered = new String(plaintext2);
 
+        // Encryption and decryption should still work.
         harness.check(input.equals(recovered),
                       "Original and recovered texts MUST be equal");
       }
@@ -206,6 +211,8 @@
         cipher = Cipher.getInstance("DESede/CBC/NoPadding");
         String input = "Does this work ?";
 
+        // null param for CBC should result in random algorithm params being
+        // generated
         cipher.init(Cipher.ENCRYPT_MODE, key, (AlgorithmParameterSpec) null);
         iv = cipher.getIV();
         harness.check(
@@ -220,21 +227,25 @@
 
         AlgorithmParameterSpec backupAlg = cipher.getParameters().getParameterSpec(
                                                                                    BlockCipherParameterSpec.class);
-
         try
           {
+            // Should not be able to init a DECRYPT cipher for CBC without
+            // params.
             cipher.init(Cipher.DECRYPT_MODE, key, (AlgorithmParameterSpec) null);
             harness.fail("(Decrypting - NULL AlgorithmParameterSpec) init of CBC with NULL IV NOT possible");
           }
         catch (Exception e)
           {
+            // The exception must be InvalidAlgorithmParameterException
             String type = e.getClass().getName();
             harness.check(
-                          type.equals(InvalidKeyException.class.getName()),
+                          type.equals(InvalidAlgorithmParameterException.class.getName()),
                           "(Decrypting - NULL AlgorithmParameterSpec) CBC init with NULL IV MUST throw exception");
           }
         try
           {
+            // Now use a proper algorithm parameter and test init.
+            // This should pass!!
             cipher.init(Cipher.DECRYPT_MODE, key, backupAlg);
           }
         catch (Exception e)
@@ -245,9 +256,9 @@
         iv = cipher.getIV();
         harness.check(
                       iv != null,
-                      "(Decrypting - Valid AlgorithmParameterSpec) cipher.getIV() for CBC init with NULL params MUST NOT return null");
+                      "(Decrypting - Valid AlgorithmParameterSpec) cipher.getIV() for CBC init with valid params MUST NOT return null");
         harness.check(iv.length == 8,
-                      "(Decrypting - NULL AlgorithmParameterSpec) IV length for CBC should be 8");
+                      "(Decrypting - Valid AlgorithmParameterSpec) IV length for CBC should be 8");
         byte[] plaintext2 = cipher.doFinal(ciphertext);
         String recovered = new String(plaintext2);
 
@@ -264,7 +275,7 @@
   }
 
   
-  //TODO: Add tests for WRAP and UNWRAP too.
+  // TODO: Add tests for WRAP and UNWRAP too.
   
 
 }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2006-06-15 23:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-15 23:17 FYI: Check for correct exceptions for TestOfCipherEngineInit Vivek Lakshmanan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).