changeset 8328:24aa1fb94814

8133993: [TEST_BUG] Make CipherInputStreamExceptions compile on OpenJDK 7 Summary: OpenJDK 7 lacks GCM encryption so tests should be removed Reviewed-by: aph
author andrew
date Mon, 13 Jul 2015 18:51:44 +0100
parents 5c6bd5d2d602
children f26dc1cb7deb
files test/javax/crypto/Cipher/CipherInputStreamExceptions.java
diffstat 1 files changed, 2 insertions(+), 193 deletions(-) [+]
line wrap: on
line diff
--- a/test/javax/crypto/Cipher/CipherInputStreamExceptions.java	Sat Jul 11 16:20:24 2015 +0100
+++ b/test/javax/crypto/Cipher/CipherInputStreamExceptions.java	Mon Jul 13 18:51:44 2015 +0100
@@ -38,190 +38,18 @@
 import java.lang.RuntimeException;
 import java.lang.Throwable;
 import java.security.AlgorithmParameters;
-import javax.crypto.AEADBadTagException;
 import javax.crypto.Cipher;
 import javax.crypto.CipherInputStream;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
-import javax.crypto.spec.GCMParameterSpec;
 
 public class CipherInputStreamExceptions {
 
     static SecretKeySpec key = new SecretKeySpec(new byte[16], "AES");
-    static GCMParameterSpec gcmspec = new GCMParameterSpec(128, new byte[16]);
     static IvParameterSpec iv = new IvParameterSpec(new byte[16]);
     static boolean failure = false;
 
-    /* Full read stream, check that getMoreData() is throwing an exception
-     * This test
-     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
-     *   2) Changes the last byte to invalidate the authetication tag.
-     *   3) Fully reads CipherInputStream to decrypt the message and closes
-     */
-
-    static void gcm_AEADBadTag() throws Exception {
-        Cipher c;
-        byte[] read = new byte[200];
-
-        System.out.println("Running gcm_AEADBadTag");
-
-        // Encrypt 100 bytes with AES/GCM/PKCS5Padding
-        byte[] ct = encryptedText("GCM", 100);
-        // Corrupt the encrypted message
-        ct = corruptGCM(ct);
-        // Create stream for decryption
-        CipherInputStream in = getStream("GCM", ct);
-
-        try {
-            int size = in.read(read);
-            throw new RuntimeException("Fail: CipherInputStream.read() " +
-                    "returned " + size + " and didn't throw an exception.");
-        } catch (IOException e) {
-            Throwable ec = e.getCause();
-            if (ec instanceof AEADBadTagException) {
-                System.out.println("  Pass.");
-            } else {
-                System.out.println("  Fail: " + ec.getMessage());
-                throw new RuntimeException(ec);
-            }
-        } finally {
-            in.close();
-        }
-    }
-
-    /* Short read stream,
-     * This test
-     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
-     *   2) Reads 100 bytes from stream to decrypt the message and closes
-     *   3) Make sure no value is returned by read()
-     *   4) Make sure no exception is thrown
-     */
-
-    static void gcm_shortReadAEAD() throws Exception {
-        Cipher c;
-        byte[] read = new byte[100];
-
-        System.out.println("Running gcm_shortReadAEAD");
-
-        byte[] pt = new byte[600];
-        pt[0] = 1;
-        // Encrypt provided 600 bytes with AES/GCM/PKCS5Padding
-        byte[] ct = encryptedText("GCM", pt);
-        // Create stream for decryption
-        CipherInputStream in = getStream("GCM", ct);
-
-        int size = 0;
-        try {
-            size = in.read(read);
-            in.close();
-            if (read.length != 100) {
-                throw new RuntimeException("Fail: read size = " + read.length +
-                        "should be 100.");
-            }
-            if (read[0] != 1) {
-                throw new RuntimeException("Fail: The decrypted text does " +
-                        "not match the plaintext: '" + read[0] +"'");
-            }
-        } catch (IOException e) {
-            System.out.println("  Fail: " + e.getMessage());
-            throw new RuntimeException(e.getCause());
-        }
-        System.out.println("  Pass.");
-    }
-
-    /*
-     * Verify doFinal() exception is suppressed when input stream is not
-     * read before it is closed.
-     * This test:
-     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
-     *   2) Changes the last byte to invalidate the authetication tag.
-     *   3) Opens a CipherInputStream and the closes it. Never reads from it.
-     *
-     * There should be no exception thrown.
-     */
-    static void gcm_suppressUnreadCorrupt() throws Exception {
-        Cipher c;
-        byte[] read = new byte[200];
-
-        System.out.println("Running supressUnreadCorrupt test");
-
-        // Encrypt 100 bytes with AES/GCM/PKCS5Padding
-        byte[] ct = encryptedText("GCM", 100);
-        // Corrupt the encrypted message
-        ct = corruptGCM(ct);
-        // Create stream for decryption
-        CipherInputStream in = getStream("GCM", ct);
-
-        try {
-            in.close();
-            System.out.println("  Pass.");
-        } catch (IOException e) {
-            System.out.println("  Fail: " + e.getMessage());
-            throw new RuntimeException(e.getCause());
-        }
-    }
-
-    /*
-     * Verify noexception thrown when 1 byte is read from a GCM stream
-     * and then closed
-     * This test:
-     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
-     *   2) Read one byte from the stream, expect no exception thrown.
-     *   4) Close stream,expect no exception thrown.
-     */
-    static void gcm_oneReadByte() throws Exception {
-
-        System.out.println("Running gcm_oneReadByte test");
-
-        // Encrypt 100 bytes with AES/GCM/PKCS5Padding
-        byte[] ct = encryptedText("GCM", 100);
-        // Create stream for decryption
-        CipherInputStream in = getStream("GCM", ct);
-
-        try {
-            in.read();
-            System.out.println("  Pass.");
-        } catch (Exception e) {
-            System.out.println("  Fail: " + e.getMessage());
-            throw new RuntimeException(e.getCause());
-        }
-    }
-
-    /*
-     * Verify exception thrown when 1 byte is read from a corrupted GCM stream
-     * and then closed
-     * This test:
-     *   1) Encrypt 100 bytes with AES/GCM/PKCS5Padding
-     *   2) Changes the last byte to invalidate the authetication tag.
-     *   3) Read one byte from the stream, expect exception thrown.
-     *   4) Close stream,expect no exception thrown.
-     */
-    static void gcm_oneReadByteCorrupt() throws Exception {
-
-        System.out.println("Running gcm_oneReadByteCorrupt test");
-
-        // Encrypt 100 bytes with AES/GCM/PKCS5Padding
-        byte[] ct = encryptedText("GCM", 100);
-        // Corrupt the encrypted message
-        ct = corruptGCM(ct);
-        // Create stream for decryption
-        CipherInputStream in = getStream("GCM", ct);
-
-        try {
-            in.read();
-            System.out.println("  Fail. No exception thrown.");
-        } catch (IOException e) {
-            Throwable ec = e.getCause();
-            if (ec instanceof AEADBadTagException) {
-                System.out.println("  Pass.");
-            } else {
-                System.out.println("  Fail: " + ec.getMessage());
-                throw new RuntimeException(ec);
-            }
-        }
-    }
-
     /* Check that close() does not throw an exception with full message in
      * CipherInputStream's ibuffer.
      * This test:
@@ -356,10 +184,7 @@
     /* Generic method to create encrypted text */
     static byte[] encryptedText(String mode, byte[] pt) throws Exception{
         Cipher c;
-        if (mode.compareTo("GCM") == 0) {
-            c = Cipher.getInstance("AES/GCM/PKCS5Padding", "SunJCE");
-            c.init(Cipher.ENCRYPT_MODE, key, gcmspec);
-        } else if (mode.compareTo("CBC") == 0) {
+        if (mode.compareTo("CBC") == 0) {
             c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
             c.init(Cipher.ENCRYPT_MODE, key, iv);
         } else {
@@ -379,10 +204,7 @@
             throws Exception {
         Cipher c;
 
-        if (mode.compareTo("GCM") == 0) {
-            c = Cipher.getInstance("AES/GCM/PKCS5Padding", "SunJCE");
-            c.init(Cipher.DECRYPT_MODE, key, gcmspec);
-        } else if (mode.compareTo("CBC") == 0) {
+        if (mode.compareTo("CBC") == 0) {
             c = Cipher.getInstance("AES/CBC/PKCS5Padding", "SunJCE");
             c.init(Cipher.DECRYPT_MODE, key, iv);
         } else {
@@ -393,20 +215,7 @@
 
     }
 
-    /* Generic method for corrupting a GCM message.  Change the last
-     * byte on of the authentication tag
-     */
-    static byte[] corruptGCM(byte[] ct) {
-        ct[ct.length - 1] = (byte) (ct[ct.length - 1] + 1);
-        return ct;
-    }
-
     public static void main(String[] args) throws Exception {
-        gcm_AEADBadTag();
-        gcm_shortReadAEAD();
-        gcm_suppressUnreadCorrupt();
-        gcm_oneReadByte();
-        gcm_oneReadByteCorrupt();
         cbc_shortStream();
         cbc_shortRead400();
         cbc_shortRead600();