diff options
author | Paul Duffin <paulduffin@google.com> | 2019-09-24 14:44:15 +0100 |
---|---|---|
committer | Paul Duffin <paulduffin@google.com> | 2019-09-25 11:05:52 +0100 |
commit | 01f6c021fa3cc3d8a7168ea38d27b3394770d0ef (patch) | |
tree | ba093434ce43d609a70f353a9c3b65b45a770c22 | |
parent | 0d94b993ae698fdd463cc25fc4359e2222cf2cbc (diff) |
Add TestRule to enable access to deprecated BC algorithms
Android restricts access to a number of deprecated BouncyCastle
algorithms to APKs that target SDK version <= 27. However, those
algorithms still need testing. Rather than set the target SDK version
to be <= 27 (which could have other side effects) tests use
Providers.setMaximumAllowableApiLevelForBcDeprecation(int) to raise
the level to make them accessible at the current target SDK version
and resets it the default value afterwards.
This change adds a JUnit test rule to implement that behavior and
uses it to replace duplicate setUp() and tearDown() code across a
number of tests. It also insulates the tests from having to access
the internal sun.security.jca.Providers and dalvik.system.VMRuntime
classes.
This is intended to be used by external/conscrypt so that the
conscrypt-tests module can stop depending on core-all-systems-module.
Bug: 141539296
Test: atest CtsLibcoreTestCases
Change-Id: If41b5c221c392e9b6d14d500537115d3380c4999
16 files changed, 244 insertions, 224 deletions
diff --git a/JavaLibrary.bp b/JavaLibrary.bp index d98541f3c3..37b74d9afc 100644 --- a/JavaLibrary.bp +++ b/JavaLibrary.bp @@ -536,6 +536,7 @@ java_system_modules { java_library_static { name: "core-test-rules", visibility: [ + "//external/conscrypt", "//frameworks/base/location/tests/locationtests", ], hostdex: true, diff --git a/luni/src/test/java/libcore/java/security/ProviderTest.java b/luni/src/test/java/libcore/java/security/ProviderTest.java index a9a5318b3f..946f602d45 100644 --- a/luni/src/test/java/libcore/java/security/ProviderTest.java +++ b/luni/src/test/java/libcore/java/security/ProviderTest.java @@ -55,29 +55,19 @@ import java.util.regex.Pattern; import javax.crypto.Cipher; import javax.crypto.EncryptedPrivateKeyInfo; import javax.crypto.NoSuchPaddingException; -import junit.framework.TestCase; import libcore.javax.crypto.MockKey; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class ProviderTest extends TestCase { +public class ProviderTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private static final boolean LOG_DEBUG = false; diff --git a/luni/src/test/java/libcore/java/security/SignatureTest.java b/luni/src/test/java/libcore/java/security/SignatureTest.java index 3750742c8b..9c014d2c3a 100644 --- a/luni/src/test/java/libcore/java/security/SignatureTest.java +++ b/luni/src/test/java/libcore/java/security/SignatureTest.java @@ -37,29 +37,19 @@ import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import junit.framework.TestCase; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; import libcore.util.HexEncoding; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class SignatureTest extends TestCase { +public class SignatureTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private static abstract class MockProvider extends Provider { public MockProvider(String name) { diff --git a/luni/src/test/java/libcore/java/security/cert/CertPathValidatorTest.java b/luni/src/test/java/libcore/java/security/cert/CertPathValidatorTest.java index 0f84e91827..fc09ae8312 100644 --- a/luni/src/test/java/libcore/java/security/cert/CertPathValidatorTest.java +++ b/luni/src/test/java/libcore/java/security/cert/CertPathValidatorTest.java @@ -32,8 +32,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; -import junit.framework.TestCase; import libcore.java.security.TestKeyStore; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; import org.bouncycastle.asn1.x509.CRLReason; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.cert.X509CertificateHolder; @@ -48,27 +49,16 @@ import org.bouncycastle.cert.ocsp.RevokedStatus; import org.bouncycastle.operator.DigestCalculatorProvider; import org.bouncycastle.operator.bc.BcDigestCalculatorProvider; import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class CertPathValidatorTest extends TestCase { +public class CertPathValidatorTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private OCSPResp generateOCSPResponse(X509Certificate serverCertJca, X509Certificate caCertJca, PrivateKey caKey, CertificateStatus status) throws Exception { diff --git a/luni/src/test/java/libcore/java/security/cert/PKIXParametersTest.java b/luni/src/test/java/libcore/java/security/cert/PKIXParametersTest.java index a26c631e76..1836aac2d1 100644 --- a/luni/src/test/java/libcore/java/security/cert/PKIXParametersTest.java +++ b/luni/src/test/java/libcore/java/security/cert/PKIXParametersTest.java @@ -16,32 +16,22 @@ package libcore.java.security.cert; -import junit.framework.TestCase; import java.security.InvalidAlgorithmParameterException; import java.security.KeyStore; import java.security.cert.PKIXParameters; import libcore.java.security.TestKeyStore; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class PKIXParametersTest extends TestCase { +public class PKIXParametersTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); public void testKeyStoreConstructor() throws Exception { TestKeyStore server = TestKeyStore.getServer(); diff --git a/luni/src/test/java/libcore/java/security/cert/PKIXRevocationCheckerTest.java b/luni/src/test/java/libcore/java/security/cert/PKIXRevocationCheckerTest.java index e2066ae72c..0e420cbd5d 100644 --- a/luni/src/test/java/libcore/java/security/cert/PKIXRevocationCheckerTest.java +++ b/luni/src/test/java/libcore/java/security/cert/PKIXRevocationCheckerTest.java @@ -14,13 +14,20 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; -import junit.framework.TestCase; import libcore.java.security.TestKeyStore; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; +public class PKIXRevocationCheckerTest extends TestCaseWithRules { + + // Allow access to deprecated BC algorithms in this test, so we can ensure they + // continue to work + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); -public class PKIXRevocationCheckerTest extends TestCase { PKIXRevocationChecker checker; PrivateKeyEntry entity; @@ -31,11 +38,6 @@ public class PKIXRevocationCheckerTest extends TestCase { protected void setUp() throws Exception { super.setUp(); - // Allow access to deprecated BC algorithms in this test, so we can ensure they - // continue to work - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX"); CertPathChecker rc = cpb.getRevocationChecker(); assertNotNull(rc); @@ -49,13 +51,6 @@ public class PKIXRevocationCheckerTest extends TestCase { issuer = intermediate.getPrivateKey("RSA", "RSA"); } - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } - public void test_Initializes() throws Exception { assertEquals(0, checker.getOcspResponses().size()); assertEquals(0, checker.getOcspExtensions().size()); diff --git a/luni/src/test/java/libcore/java/security/cert/X509CRLTest.java b/luni/src/test/java/libcore/java/security/cert/X509CRLTest.java index b11b40e44e..ca612be253 100644 --- a/luni/src/test/java/libcore/java/security/cert/X509CRLTest.java +++ b/luni/src/test/java/libcore/java/security/cert/X509CRLTest.java @@ -18,8 +18,10 @@ package libcore.java.security.cert; import static java.nio.charset.StandardCharsets.UTF_8; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; import sun.security.provider.X509Factory; import sun.security.x509.X509CRLImpl; import tests.support.resource.Support_Resources; @@ -49,27 +51,20 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import junit.framework.TestCase; import libcore.java.security.StandardNames; -public class X509CRLTest extends TestCase { +public class X509CRLTest extends TestCaseWithRules { + + // Allow access to deprecated BC algorithms in this test, so we can ensure they + // continue to work + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); @Override public void setUp() throws Exception { super.setUp(); mX509Providers = Security.getProviders("CertificateFactory.X509"); - - // Allow access to deprecated BC algorithms in this test, so we can ensure they - // continue to work - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); } private Provider[] mX509Providers; diff --git a/luni/src/test/java/libcore/java/security/cert/X509CertificateTest.java b/luni/src/test/java/libcore/java/security/cert/X509CertificateTest.java index 95a95c0f4b..d6942141d7 100644 --- a/luni/src/test/java/libcore/java/security/cert/X509CertificateTest.java +++ b/luni/src/test/java/libcore/java/security/cert/X509CertificateTest.java @@ -57,32 +57,26 @@ import java.util.List; import java.util.Locale; import java.util.Set; import javax.security.auth.x500.X500Principal; -import junit.framework.TestCase; import libcore.java.security.StandardNames; - -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; import tests.support.resource.Support_Resources; -public class X509CertificateTest extends TestCase { +public class X509CertificateTest extends TestCaseWithRules { + + // Allow access to deprecated BC algorithms in this test, so we can ensure they + // continue to work + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); @Override protected void setUp() throws Exception { super.setUp(); mX509Providers = Security.getProviders("CertificateFactory.X509"); - - // Allow access to deprecated BC algorithms in this test, so we can ensure they - // continue to work - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); } private Provider[] mX509Providers; diff --git a/luni/src/test/java/libcore/javax/crypto/MacTest.java b/luni/src/test/java/libcore/javax/crypto/MacTest.java index 3e49cd694e..bd2ad39649 100644 --- a/luni/src/test/java/libcore/javax/crypto/MacTest.java +++ b/luni/src/test/java/libcore/javax/crypto/MacTest.java @@ -26,28 +26,18 @@ import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; -import junit.framework.TestCase; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class MacTest extends TestCase { +public class MacTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private static abstract class MockProvider extends Provider { public MockProvider(String name) { diff --git a/luni/src/test/java/libcore/javax/security/auth/x500/X500PrincipalTest.java b/luni/src/test/java/libcore/javax/security/auth/x500/X500PrincipalTest.java index 1aa3550b75..226952f232 100644 --- a/luni/src/test/java/libcore/javax/security/auth/x500/X500PrincipalTest.java +++ b/luni/src/test/java/libcore/javax/security/auth/x500/X500PrincipalTest.java @@ -21,30 +21,20 @@ import java.security.cert.CertificateFactory; import java.security.cert.X509Certificate; import java.util.Arrays; import javax.security.auth.x500.X500Principal; -import junit.framework.TestCase; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; import libcore.libcore.util.SerializationTester; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class X500PrincipalTest extends TestCase { +public class X500PrincipalTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); public void testSerialization() { String expected = "aced0005737200266a617661782e73656375726974792e617574682e7" diff --git a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/MacTest.java b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/MacTest.java index 7d8aff0e11..751a99e6dc 100644 --- a/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/MacTest.java +++ b/luni/src/test/java/org/apache/harmony/crypto/tests/javax/crypto/MacTest.java @@ -39,39 +39,29 @@ import javax.crypto.SecretKey; import javax.crypto.ShortBufferException; import javax.crypto.spec.DHGenParameterSpec; import javax.crypto.spec.SecretKeySpec; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; import org.apache.harmony.crypto.tests.support.MyMacSpi; import org.apache.harmony.security.tests.support.SpiEngUtils; -import junit.framework.TestCase; import junit.framework.Test; import junit.framework.TestSuite; import libcore.java.security.StandardNames; import libcore.javax.crypto.MockKey; import libcore.javax.crypto.MockKey2; - -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; +import org.junit.Rule; +import org.junit.rules.TestRule; /** * Tests for Mac class constructors and methods * */ -public class MacTest extends TestCase { +public class MacTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); public static final String srvMac = "Mac"; diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/MessageDigest2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/MessageDigest2Test.java index 689efa51c4..5367dcd911 100644 --- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/MessageDigest2Test.java +++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/MessageDigest2Test.java @@ -33,9 +33,19 @@ import java.util.Map; import java.util.Map.Entry; import dalvik.system.VMRuntime; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; import sun.security.jca.Providers; -public class MessageDigest2Test extends junit.framework.TestCase { +public class MessageDigest2Test extends TestCaseWithRules { + + // Allow access to deprecated BC algorithms in this test, so we can ensure they + // continue to work + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private static final String MESSAGEDIGEST_ID = "MessageDigest."; @@ -434,18 +444,6 @@ public class MessageDigest2Test extends junit.framework.TestCase { for (Provider provider : providers) { digestAlgs.put(provider, getDigestAlgorithms(provider)); } - - // Allow access to deprecated BC algorithms in this test, so we can ensure they - // continue to work - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); } /* diff --git a/luni/src/test/java/tests/security/cert/CertificateFactory4Test.java b/luni/src/test/java/tests/security/cert/CertificateFactory4Test.java index 18fe0c4e6f..1b3f00c35d 100644 --- a/luni/src/test/java/tests/security/cert/CertificateFactory4Test.java +++ b/luni/src/test/java/tests/security/cert/CertificateFactory4Test.java @@ -17,10 +17,10 @@ package tests.security.cert; -import junit.framework.TestCase; - -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; import tests.support.resource.Support_Resources; import tests.support.Support_GetResource; @@ -40,23 +40,13 @@ import java.security.cert.CertificateFactory; import java.util.Collection; import java.util.List; -public class CertificateFactory4Test extends TestCase { +public class CertificateFactory4Test extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); private static final String BASE_URL = Support_GetResource .getResourceURL("/../internalres/"); diff --git a/luni/src/test/java/tests/targets/security/cert/CertificateTest.java b/luni/src/test/java/tests/targets/security/cert/CertificateTest.java index 80b34c017d..4a8e5ef7a4 100644 --- a/luni/src/test/java/tests/targets/security/cert/CertificateTest.java +++ b/luni/src/test/java/tests/targets/security/cert/CertificateTest.java @@ -32,29 +32,19 @@ import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Calendar; import java.util.List; -import junit.framework.TestCase; import libcore.java.security.StandardNames; +import libcore.junit.junit3.TestCaseWithRules; +import libcore.junit.util.EnableDeprecatedBouncyCastleAlgorithmsRule; +import org.junit.Rule; +import org.junit.rules.TestRule; -import dalvik.system.VMRuntime; -import sun.security.jca.Providers; - -public class CertificateTest extends TestCase { +public class CertificateTest extends TestCaseWithRules { // Allow access to deprecated BC algorithms in this test, so we can ensure they // continue to work - @Override - public void setUp() throws Exception { - super.setUp(); - Providers.setMaximumAllowableApiLevelForBcDeprecation( - VMRuntime.getRuntime().getTargetSdkVersion()); - } - - @Override - public void tearDown() throws Exception { - Providers.setMaximumAllowableApiLevelForBcDeprecation( - Providers.DEFAULT_MAXIMUM_ALLOWABLE_TARGET_API_LEVEL_FOR_BC_DEPRECATION); - super.tearDown(); - } + @Rule + public TestRule enableDeprecatedBCAlgorithmsRule = + EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); /* * Following certificate chain was taken from https://www.verisign.com and diff --git a/test-rules/src/main/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRule.java b/test-rules/src/main/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRule.java new file mode 100644 index 0000000000..bfd45ef07c --- /dev/null +++ b/test-rules/src/main/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRule.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package libcore.junit.util; + +import dalvik.system.VMRuntime; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.util.function.Supplier; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; +import sun.security.jca.Providers; + +/** + * Allows tests to temporarily enable deprecated BouncyCastle algorithms to verify that their + * behavior has not changed. + * + * <p>To use add the following to the test class. + * + * <pre> + * @Rule + * public TestRule enableDeprecatedBCAlgorithmsRule = + * EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance(); + * </pre> + * + * <p>It will give all test methods access to the deprecated algorithms. + */ +public class EnableDeprecatedBouncyCastleAlgorithmsRule implements TestRule { + + private static final TestRule INSTANCE = new EnableDeprecatedBouncyCastleAlgorithmsRule(); + + public static TestRule getInstance() { + return INSTANCE; + } + + private EnableDeprecatedBouncyCastleAlgorithmsRule() { + } + + @Override + public Statement apply(final Statement statement, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + int currentMaximum = + Providers.getMaximumAllowableApiLevelForBcDeprecation(); + try { + int newMaximum = VMRuntime.getRuntime().getTargetSdkVersion(); + Providers.setMaximumAllowableApiLevelForBcDeprecation(newMaximum); + statement.evaluate(); + } finally { + Providers.setMaximumAllowableApiLevelForBcDeprecation(currentMaximum); + } + } + }; + } +} diff --git a/test-rules/src/test/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRuleTest.java b/test-rules/src/test/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRuleTest.java new file mode 100644 index 0000000000..a08ed848c4 --- /dev/null +++ b/test-rules/src/test/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRuleTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package libcore.junit.util; + +import dalvik.system.VMRuntime; +import libcore.junit.util.SwitchTargetSdkVersionRule.TargetSdkVersion; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.junit.rules.TestRule; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import sun.security.jca.Providers; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link EnableDeprecatedBouncyCastleAlgorithmsRule}. + */ +@RunWith(JUnit4.class) +public class EnableDeprecatedBouncyCastleAlgorithmsRuleTest { + + /** + * Chain the rules together so that changes to the target sdk version will be visible in the + * bouncy castle rules. + */ + @Rule + public TestRule chain = RuleChain + .outerRule(SwitchTargetSdkVersionRule.getInstance()) + .around(EnableDeprecatedBouncyCastleAlgorithmsRule.getInstance()); + + @Test + @TargetSdkVersion(23) + public void testRunningAsIfTargetedAtSDKVersion23() { + assertEquals(23, Providers.getMaximumAllowableApiLevelForBcDeprecation()); + } + + @Test + public void testRunningAsIfTargetedAtCurrentSDKVersion() { + assertEquals(VMRuntime.getRuntime().getTargetSdkVersion(), + Providers.getMaximumAllowableApiLevelForBcDeprecation()); + } +} |