summaryrefslogtreecommitdiff
path: root/test-rules
diff options
context:
space:
mode:
authorPaul Duffin <paulduffin@google.com>2019-09-24 14:44:15 +0100
committerPaul Duffin <paulduffin@google.com>2019-09-25 11:05:52 +0100
commit01f6c021fa3cc3d8a7168ea38d27b3394770d0ef (patch)
treeba093434ce43d609a70f353a9c3b65b45a770c22 /test-rules
parent0d94b993ae698fdd463cc25fc4359e2222cf2cbc (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
Diffstat (limited to 'test-rules')
-rw-r--r--test-rules/src/main/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRule.java71
-rw-r--r--test-rules/src/test/java/libcore/junit/util/EnableDeprecatedBouncyCastleAlgorithmsRuleTest.java56
2 files changed, 127 insertions, 0 deletions
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>
+ * &#64;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());
+ }
+}