diff options
author | Kenny Root <kroot@google.com> | 2015-07-20 15:04:08 -0700 |
---|---|---|
committer | Kenny Root <kroot@google.com> | 2015-07-23 09:12:58 -0700 |
commit | 5423595a40397888d426112b1c6fe7b4fcf24e7a (patch) | |
tree | f976d8dce88d666e4d80656dd2a0f0179de5ea55 | |
parent | 264d2b8aeecc9d04a816e1471619f577c1e93628 (diff) | |
download | libcore-5423595a40397888d426112b1c6fe7b4fcf24e7a.zip libcore-5423595a40397888d426112b1c6fe7b4fcf24e7a.tar.gz libcore-5423595a40397888d426112b1c6fe7b4fcf24e7a.tar.bz2 |
Late binding: add tests for init-time rejection
Cipher should try to to initialize the CipherSpi it selects before
returning it as a successful match. These tests ensure that it is
correct.
(cherry picked from commit 85d1800ec694bb4c2f629073d79520c4a7ad0cc8)
Bug: 22573249
Change-Id: I12ed5021cf85fccb5d04a0904a302f6cd3569c3d
-rw-r--r-- | luni/src/test/java/libcore/javax/crypto/CipherTest.java | 88 | ||||
-rw-r--r-- | luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java | 66 |
2 files changed, 154 insertions, 0 deletions
diff --git a/luni/src/test/java/libcore/javax/crypto/CipherTest.java b/luni/src/test/java/libcore/javax/crypto/CipherTest.java index 3c7ff0e..3e81926 100644 --- a/luni/src/test/java/libcore/javax/crypto/CipherTest.java +++ b/luni/src/test/java/libcore/javax/crypto/CipherTest.java @@ -996,6 +996,94 @@ public final class CipherTest extends TestCase { } } + public void testCipher_init_CallsInitWithParams_AlgorithmParameterSpec() throws Exception { + Provider mockProviderRejects = new MockProvider("MockProviderRejects") { + public void setup() { + put("Cipher.FOO", + MockCipherSpi.MustInitWithAlgorithmParameterSpec_RejectsAll.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(), new IvParameterSpec(new byte[12])); + assertEquals(mockProviderAccepts, c.getProvider()); + } finally { + Security.removeProvider(mockProviderRejects.getName()); + Security.removeProvider(mockProviderAccepts.getName()); + } + } + + public void testCipher_init_CallsInitWithParams_AlgorithmParameters() throws Exception { + Provider mockProviderRejects = new MockProvider("MockProviderRejects") { + public void setup() { + put("Cipher.FOO", + MockCipherSpi.MustInitWithAlgorithmParameters_RejectsAll.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() { + put("Cipher.FOO", MockCipherSpi.MustInitForEncryptModeOrRejects.class.getName()); + put("Cipher.FOO SupportedKeyClasses", MockKey.class.getName()); + } + }; + Provider mockProviderAcceptsAll = new MockProvider("MockProviderAcceptsAll") { + public void setup() { + put("Cipher.FOO", MockCipherSpi.AllKeyTypes.class.getName()); + put("Cipher.FOO SupportedKeyClasses", MockKey.class.getName()); + } + }; + + Security.addProvider(mockProviderOnlyEncrypt); + Security.addProvider(mockProviderAcceptsAll); + try { + { + Cipher c = Cipher.getInstance("FOO"); + c.init(Cipher.DECRYPT_MODE, new MockKey(), AlgorithmParameters.getInstance("AES")); + assertEquals(mockProviderAcceptsAll, c.getProvider()); + } + + { + Cipher c = Cipher.getInstance("FOO"); + c.init(Cipher.ENCRYPT_MODE, new MockKey(), AlgorithmParameters.getInstance("AES")); + assertEquals(mockProviderOnlyEncrypt, c.getProvider()); + } + } finally { + Security.removeProvider(mockProviderOnlyEncrypt.getName()); + Security.removeProvider(mockProviderAcceptsAll.getName()); + } + } + public void test_getInstance() throws Exception { final ByteArrayOutputStream errBuffer = new ByteArrayOutputStream(); PrintStream out = new PrintStream(errBuffer); diff --git a/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java b/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java index 6742cf3..e398b33 100644 --- a/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java +++ b/luni/src/test/java/libcore/javax/crypto/MockCipherSpi.java @@ -25,6 +25,7 @@ import java.security.SecureRandom; import java.security.spec.AlgorithmParameterSpec; import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; import javax.crypto.CipherSpi; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; @@ -56,6 +57,71 @@ public class MockCipherSpi extends CipherSpi { public static class AllKeyTypes extends MockCipherSpi { } + public static class MustInitWithAlgorithmParameterSpec_RejectsAll extends MockCipherSpi { + @Override + protected void engineInit(int opmode, Key key, SecureRandom random) + throws InvalidKeyException { + throw new AssertionError("Must have AlgorithmParameterSpec"); + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + throw new InvalidAlgorithmParameterException("expected rejection"); + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameters params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + throw new AssertionError("Must have AlgorithmParameterSpec"); + } + } + + public static class MustInitWithAlgorithmParameters_RejectsAll extends MockCipherSpi { + @Override + protected void engineInit(int opmode, Key key, SecureRandom random) + throws InvalidKeyException { + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + throw new AssertionError("Must have AlgorithmParameterSpec"); + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameters params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + throw new InvalidAlgorithmParameterException("expected rejection"); + } + } + + public static class MustInitForEncryptModeOrRejects extends MockCipherSpi { + @Override + protected void engineInit(int opmode, Key key, SecureRandom random) + throws InvalidKeyException { + if (opmode != Cipher.ENCRYPT_MODE) { + throw new InvalidKeyException("expected rejection"); + } + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + if (opmode != Cipher.ENCRYPT_MODE) { + throw new InvalidKeyException("expected rejection"); + } + } + + @Override + protected void engineInit(int opmode, Key key, AlgorithmParameters params, + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { + if (opmode != Cipher.ENCRYPT_MODE) { + throw new InvalidKeyException("expected rejection"); + } + } + } + public void checkKeyType(Key key) throws InvalidKeyException { } |