Late binding: add more Cipher tests

Any provider throwing an unchecked exception should not prevent the next
possibly working provider from having a chance.

Bug: 22573249
Change-Id: If3f508ed3e87de58b39ab380fb298a92fb1b593b
diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
index 5c46f03..5f05d0c 100644
--- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java
+++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java
@@ -1061,6 +1061,33 @@
         }
     }
 
+    public void testCipher_init_CallsInitIgnoresRuntimeException() throws Exception {
+        Provider mockProviderRejects = new MockProvider("MockProviderRejects") {
+            public void setup() {
+                put("Cipher.FOO",
+                        MockCipherSpi.MustInitWithAlgorithmParameters_ThrowsNull.class.getName());
+                put("Cipher.FOO SupportedKeyClasses", MockKey.class.getName());
+            }
+        };
+        Provider mockProviderAccepts = new MockProvider("MockProviderAccepts") {
+            public void setup() {
+                put("Cipher.FOO", MockCipherSpi.AllKeyTypes.class.getName());
+                put("Cipher.FOO SupportedKeyClasses", MockKey.class.getName());
+            }
+        };
+
+        Security.addProvider(mockProviderRejects);
+        Security.addProvider(mockProviderAccepts);
+        try {
+            Cipher c = Cipher.getInstance("FOO");
+            c.init(Cipher.ENCRYPT_MODE, new MockKey(), AlgorithmParameters.getInstance("AES"));
+            assertEquals(mockProviderAccepts, c.getProvider());
+        } finally {
+            Security.removeProvider(mockProviderRejects.getName());
+            Security.removeProvider(mockProviderAccepts.getName());
+        }
+    }
+
     public void testCipher_init_CallsInitWithMode() throws Exception {
         Provider mockProviderOnlyEncrypt = new MockProvider("MockProviderOnlyEncrypt") {
             public void setup() {
diff --git a/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java b/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java
index e398b33..c1b1bd2 100644
--- a/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java
+++ b/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java
@@ -81,6 +81,7 @@
         @Override
         protected void engineInit(int opmode, Key key, SecureRandom random)
                 throws InvalidKeyException {
+            throw new AssertionError("Must have AlgorithmParameterSpec");
         }
 
         @Override
@@ -96,6 +97,26 @@
         }
     }
 
+    public static class MustInitWithAlgorithmParameters_ThrowsNull extends MockCipherSpi {
+        @Override
+        protected void engineInit(int opmode, Key key, SecureRandom random)
+                throws InvalidKeyException {
+            throw new NullPointerException("expected rejection");
+        }
+
+        @Override
+        protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params,
+                SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
+            throw new NullPointerException("expected rejection");
+        }
+
+        @Override
+        protected void engineInit(int opmode, Key key, AlgorithmParameters params,
+                SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
+            throw new NullPointerException("expected rejection");
+        }
+    }
+
     public static class MustInitForEncryptModeOrRejects extends MockCipherSpi {
         @Override
         protected void engineInit(int opmode, Key key, SecureRandom random)