Add API coverage for java.security methods
Bug: 182166453
Test: atest CtsLibcoreTestCases:libcore.java.security
Change-Id: Ic528c887c6ef85dcd3b814b38a8cf756a924675a
diff --git a/expectations/knownfailures.txt b/expectations/knownfailures.txt
index 3ef0559..10a325e 100644
--- a/expectations/knownfailures.txt
+++ b/expectations/knownfailures.txt
@@ -102,7 +102,7 @@
{
description: "Android doesn't allow null parent.",
result: EXEC_FAILED,
- name: "tests.java.security.SecureClassLoaderTest#testSecureClassLoaderClassLoader"
+ name: "tests.java.security.SecureClassLoaderTest#testNullParent"
},
{
description: "not supported",
diff --git a/luni/src/test/java/libcore/java/security/AccessControlExceptionTest.java b/luni/src/test/java/libcore/java/security/AccessControlExceptionTest.java
new file mode 100644
index 0000000..e12f8fa
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/AccessControlExceptionTest.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.AccessControlException;
+import java.security.AllPermission;
+import java.security.Permission;
+
+@RunWith(JUnit4.class)
+public class AccessControlExceptionTest {
+
+ @Test
+ public void testConstructor() {
+ String msg = "test message";
+ Permission permission = new AllPermission();
+
+ try {
+ throw new AccessControlException(msg);
+ } catch (AccessControlException e) {
+ assertEquals(msg, e.getMessage());
+ }
+
+ try {
+ throw new AccessControlException(msg, permission);
+ } catch (AccessControlException e) {
+ assertEquals(msg, e.getMessage());
+ assertSame(permission, e.getPermission());
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/AccessControllerTest.java b/luni/src/test/java/libcore/java/security/AccessControllerTest.java
index 5746abd..678be72 100644
--- a/luni/src/test/java/libcore/java/security/AccessControllerTest.java
+++ b/luni/src/test/java/libcore/java/security/AccessControllerTest.java
@@ -16,25 +16,36 @@
package libcore.java.security;
+import static org.junit.Assert.assertEquals;
+
import java.security.AccessControlContext;
import java.security.AccessController;
import java.security.DomainCombiner;
import java.security.Permission;
import java.security.Permissions;
import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+
import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
/**
* Android doesn't fully support access controller. This tests that actions are
* passed through without permission enforcement.
*/
-public final class AccessControllerTest extends TestCase {
+@RunWith(JUnit4.class)
+public final class AccessControllerTest {
- public void testDoPrivilegedWithCombiner() {
- final Permission permission = new RuntimePermission("do stuff");
+ @Test
+ public void testDoPrivilegedWithCombiner() throws Exception {
final DomainCombiner union = new DomainCombiner() {
@Override
public ProtectionDomain[] combine(ProtectionDomain[] a, ProtectionDomain[] b) {
@@ -46,28 +57,61 @@
AccessControlContext accessControlContext = new AccessControlContext(
new AccessControlContext(new ProtectionDomain[] { protectionDomain }), union);
+ assertActionRun(action -> AccessController.doPrivileged(
+ (PrivilegedAction<Void>) action::get));
+
+ assertActionRun(action -> AccessController.doPrivileged(
+ (PrivilegedAction<Void>) action::get, accessControlContext));
+
+ assertActionRun(action -> {
+ try {
+ AccessController.doPrivileged((PrivilegedExceptionAction<Void>) action::get);
+ } catch (PrivilegedActionException e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ assertActionRun(action -> {
+ try {
+ AccessController.doPrivileged(
+ (PrivilegedExceptionAction<Void>) action::get, accessControlContext);
+ } catch (PrivilegedActionException e) {
+ throw new RuntimeException(e);
+ }
+ });
+
+ assertActionRun(action -> AccessController.doPrivilegedWithCombiner(
+ (PrivilegedAction<Void>) action::get));
+
+ assertActionRun(action -> {
+ try {
+ AccessController.doPrivilegedWithCombiner(
+ (PrivilegedExceptionAction<Void>) action::get);
+ } catch (PrivilegedActionException e) {
+ throw new RuntimeException(e);
+ }
+ });
+ }
+
+ private static void assertActionRun(Consumer<Supplier<Void>> runner) {
+ final Permission permission = new RuntimePermission("do stuff");
+
final AtomicInteger actionCount = new AtomicInteger();
- AccessController.doPrivileged(new PrivilegedAction<Void>() {
- @Override
- public Void run() {
+ runner.accept(() -> {
+ assertEquals(null, AccessController.getContext().getDomainCombiner());
+ AccessController.getContext().checkPermission(permission);
+
+ // Calling doPrivileged again would have exercised the combiner
+ runner.accept(() -> {
+ actionCount.incrementAndGet();
assertEquals(null, AccessController.getContext().getDomainCombiner());
AccessController.getContext().checkPermission(permission);
-
- // Calling doPrivileged again would have exercised the combiner
- AccessController.doPrivileged(new PrivilegedAction<Void>() {
- @Override
- public Void run() {
- actionCount.incrementAndGet();
- assertEquals(null, AccessController.getContext().getDomainCombiner());
- AccessController.getContext().checkPermission(permission);
- return null;
- }
- });
-
return null;
- }
- }, accessControlContext);
+ });
+
+ return null;
+ });
assertEquals(1, actionCount.get());
}
diff --git a/luni/src/test/java/libcore/java/security/AllPermissionTest.java b/luni/src/test/java/libcore/java/security/AllPermissionTest.java
new file mode 100644
index 0000000..0d4f4b2
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/AllPermissionTest.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.AllPermission;
+
+@RunWith(JUnit4.class)
+public class AllPermissionTest {
+
+ @Test
+ public void testGetAction() {
+ String action = "test action";
+ AllPermission permission = new AllPermission("Test permission", action);
+ // Action value is discarded and returns null here.
+ assertNull(permission.getActions());
+ }
+
+ @Test
+ public void testImplies() {
+ AllPermission permission = new AllPermission();
+ AllPermission permission2 = new AllPermission();
+ assertTrue(permission.implies(null));
+ assertTrue(permission.implies(permission));
+ assertTrue(permission.implies(permission2));
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/AuthProviderTest.java b/luni/src/test/java/libcore/java/security/AuthProviderTest.java
new file mode 100644
index 0000000..ecc7da8
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/AuthProviderTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.AuthProvider;
+
+import javax.security.auth.Subject;
+import javax.security.auth.callback.CallbackHandler;
+
+@RunWith(JUnit4.class)
+public class AuthProviderTest {
+
+ private static class TestAuthProvider extends AuthProvider {
+ final String name;
+
+ protected TestAuthProvider(String name, double version, String info) {
+ super(name, version, info);
+ this.name = name;
+ }
+
+ @Override
+ public void login(Subject subject, CallbackHandler handler) {
+ }
+
+ @Override
+ public void logout() {
+ }
+
+ @Override
+ public void setCallbackHandler(CallbackHandler handler) {
+ }
+ }
+
+ @Test
+ public void testConstructor() {
+ TestAuthProvider provider = new TestAuthProvider("test", 1.0d, "info");
+ assertEquals("test", provider.name);
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/BasicPermissionTest.java b/luni/src/test/java/libcore/java/security/BasicPermissionTest.java
new file mode 100644
index 0000000..e45c3ff
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/BasicPermissionTest.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.AllPermission;
+import java.security.BasicPermission;
+
+@RunWith(JUnit4.class)
+public class BasicPermissionTest {
+
+ private static class TestPermission extends BasicPermission {
+ public TestPermission(String name) {
+ super(name);
+ }
+ }
+
+ @Test
+ public void testImplies() {
+ BasicPermission permission = new TestPermission("name");
+ AllPermission permission2 = new AllPermission();
+ assertTrue(permission.implies(null));
+ assertTrue(permission.implies(permission));
+ assertTrue(permission.implies(permission2));
+ }
+
+ @Test
+ public void testCheckGuard_doesntThrow() {
+ BasicPermission permission = new TestPermission("name");
+ permission.checkGuard(null);
+ permission.checkGuard(new Object());
+ permission.checkGuard(permission);
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/CodeSourceTest.java b/luni/src/test/java/libcore/java/security/CodeSourceTest.java
new file mode 100644
index 0000000..01481f8
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/CodeSourceTest.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.CodeSigner;
+import java.security.CodeSource;
+import java.security.KeyStore;
+import java.security.Timestamp;
+import java.security.cert.Certificate;
+import java.security.cert.X509Certificate;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.List;
+
+import sun.security.provider.certpath.X509CertPath;
+
+@RunWith(JUnit4.class)
+public class CodeSourceTest {
+
+
+ private static final String PATH = "file://invalid_cert_path";
+
+ private CodeSource codeSource;
+
+ @Before
+ public void setUp() throws Exception {
+ CodeSigner codeSigner = createCodeSigner();
+ codeSource = new CodeSource(new URL(PATH), new CodeSigner[] { codeSigner });
+ }
+
+ private static CodeSigner createCodeSigner() throws Exception {
+ KeyStore keyStore = KeyStore.getInstance("AndroidCAStore");
+ keyStore.load(null);
+ // Get a X509Certificate from the keyStore
+ X509Certificate cert = null;
+ for (Enumeration<String> aliases = keyStore.aliases(); aliases.hasMoreElements(); ) {
+ String alias = aliases.nextElement();
+ Certificate certificate = keyStore.getCertificate(alias);
+ assertTrue(certificate instanceof X509Certificate);
+ cert = (X509Certificate) certificate;
+ break;
+ }
+
+ assertNotNull(cert);
+ X509CertPath certPath = new X509CertPath(List.of(cert));
+ return new CodeSigner(certPath, new Timestamp(new Date(), certPath));
+ }
+
+ @Test
+ public void testGetCerificates() {
+ assertNull(codeSource.getCertificates());
+ }
+
+ @Test
+ public void testGetCodeSigners() {
+ assertNull(codeSource.getCodeSigners());
+ }
+
+ @Test
+ public void testGetLocation() throws MalformedURLException {
+ assertEquals(new URL(PATH), codeSource.getLocation());
+ }
+
+ @Test
+ public void testImplies() {
+ assertTrue(codeSource.implies(null));
+ assertTrue(codeSource.implies(codeSource));
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/PermissionCollectionTest.java b/luni/src/test/java/libcore/java/security/PermissionCollectionTest.java
new file mode 100644
index 0000000..1fb39e6
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/PermissionCollectionTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.util.Enumeration;
+
+@RunWith(JUnit4.class)
+public class PermissionCollectionTest {
+
+ private static class TestPermissionCollection extends PermissionCollection {
+
+ @Override
+ public void add(Permission permission) {
+ }
+
+ @Override
+ public boolean implies(Permission permission) {
+ return true;
+ }
+
+ @Override
+ public Enumeration<Permission> elements() {
+ return null;
+ }
+ }
+
+ @Test
+ public void testSetReadOnly() {
+ PermissionCollection permissionCollection = new TestPermissionCollection();
+ // isReadOnly() always return true
+ assertTrue(permissionCollection.isReadOnly());
+ permissionCollection.setReadOnly();
+ assertTrue(permissionCollection.isReadOnly());
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/PolicySpiTest.java b/luni/src/test/java/libcore/java/security/PolicySpiTest.java
new file mode 100644
index 0000000..52d5023
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/PolicySpiTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.CodeSource;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Policy;
+import java.security.PolicySpi;
+import java.security.ProtectionDomain;
+
+@RunWith(JUnit4.class)
+public class PolicySpiTest {
+
+ private static class TestPolicySpi extends PolicySpi {
+
+ @Override
+ protected boolean engineImplies(ProtectionDomain domain, Permission permission) {
+ return false;
+ }
+
+ @Override
+ public void engineRefresh() {
+ super.engineRefresh();
+ }
+
+ @Override
+ public PermissionCollection engineGetPermissions(CodeSource codesource) {
+ return super.engineGetPermissions(codesource);
+ }
+
+ @Override
+ public PermissionCollection engineGetPermissions(ProtectionDomain domain) {
+ return super.engineGetPermissions(domain);
+ }
+ }
+
+ private static final TestPolicySpi spi = new TestPolicySpi();
+
+ @Test
+ public void testEngineRefresh_doesntThrow() {
+ spi.engineRefresh();
+ }
+
+ @Test
+ public void testEngineGetPermissions() {
+ assertEquals(Policy.UNSUPPORTED_EMPTY_COLLECTION,
+ spi.engineGetPermissions((CodeSource) null));
+ assertEquals(Policy.UNSUPPORTED_EMPTY_COLLECTION,
+ spi.engineGetPermissions((ProtectionDomain) null));
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/PolicyTest.java b/luni/src/test/java/libcore/java/security/PolicyTest.java
new file mode 100644
index 0000000..c25b3ce
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/PolicyTest.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.CodeSource;
+import java.security.Policy;
+import java.security.ProtectionDomain;
+import java.security.Provider;
+
+/**
+ * Test the stub implementation of {@link Policy}.
+ */
+@RunWith(JUnit4.class)
+public class PolicyTest {
+
+ private static class TestPolicy extends Policy {
+ }
+
+ private final Policy p = new TestPolicy();
+
+ @Test
+ public void testGetInstance_alwaysReturnNull() throws Exception {
+ assertNull(Policy.getInstance(null, null));
+ assertNull(Policy.getInstance(null, null, (String) null));
+ assertNull(Policy.getInstance(null, null, (Provider) null));
+
+ Policy.setPolicy(p);
+
+ // Still return null after setPolicy()
+ assertNull(Policy.getInstance(null, null));
+ assertNull(Policy.getInstance(null, null, (String) null));
+ assertNull(Policy.getInstance(null, null, (Provider) null));
+ }
+
+ @Test
+ public void testGetParameters() {
+ assertNull(p.getParameters());
+ }
+
+ @Test
+ public void testGetPermissions() {
+ assertNull(p.getPermissions((CodeSource) null));
+ assertNull(p.getPermissions((ProtectionDomain) null));
+ }
+
+ @Test
+ public void testGetProvider() {
+ assertNull(p.getProvider());
+ }
+
+ @Test
+ public void testGetType() {
+ assertNull(p.getType());
+ }
+
+ @Test
+ public void testImplies() {
+ assertTrue(p.implies(null, null));
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/ProtectionDomainTest.java b/luni/src/test/java/libcore/java/security/ProtectionDomainTest.java
new file mode 100644
index 0000000..8360a52
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/ProtectionDomainTest.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.net.URL;
+import java.security.CodeSigner;
+import java.security.CodeSource;
+import java.security.KeyStore;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Principal;
+import java.security.ProtectionDomain;
+import java.security.Timestamp;
+import java.security.cert.Certificate;
+import java.security.cert.X509Certificate;
+import java.util.Date;
+import java.util.Enumeration;
+import java.util.List;
+
+import sun.security.provider.certpath.X509CertPath;
+
+@RunWith(JUnit4.class)
+public class ProtectionDomainTest {
+
+ private ProtectionDomain domain;
+
+ @Before
+ public void setUp() throws Exception {
+ String path = "file://invalid_path";
+ CodeSigner codeSigner = createCodeSigner();
+ CodeSource codeSource = new CodeSource(new URL(path), new CodeSigner[] { codeSigner });
+ domain = new ProtectionDomain(codeSource , new TestPermissionCollection(),
+ ProtectionDomainTest.class.getClassLoader(), new Principal[0]);
+ }
+
+ @Test
+ public void testGetClassLoader() {
+ // Return null even thought it's set in the constructor
+ assertNull(domain.getClassLoader());
+ }
+
+ @Test
+ public void testGetCodeSource() {
+ // Return null even thought it's set in the constructor
+ assertNull(domain.getCodeSource());
+ }
+
+ @Test
+ public void testGetPermissions() {
+ // Return null even thought it's set in the constructor
+ assertNull(domain.getPermissions());
+ }
+
+ @Test
+ public void testGetPrincipals() {
+ // Return null even thought it's set in the constructor
+ assertNull(domain.getPrincipals());
+ }
+
+ @Test
+ public void testImplies() {
+ assertTrue(domain.implies(null));
+ }
+
+ private static CodeSigner createCodeSigner() throws Exception {
+ KeyStore keyStore = KeyStore.getInstance("AndroidCAStore");
+ keyStore.load(null);
+ // Get a X509Certificate from the keyStore
+ X509Certificate cert = null;
+ for (Enumeration<String> aliases = keyStore.aliases(); aliases.hasMoreElements(); ) {
+ String alias = aliases.nextElement();
+ Certificate certificate = keyStore.getCertificate(alias);
+ assertTrue(certificate instanceof X509Certificate);
+ cert = (X509Certificate) certificate;
+ break;
+ }
+
+ assertNotNull(cert);
+ X509CertPath certPath = new X509CertPath(List.of(cert));
+ return new CodeSigner(certPath, new Timestamp(new Date(), certPath));
+ }
+
+ private static class TestPermissionCollection extends PermissionCollection {
+
+ @Override
+ public void add(Permission permission) {
+ }
+
+ @Override
+ public boolean implies(Permission permission) {
+ return true;
+ }
+
+ @Override
+ public Enumeration<Permission> elements() {
+ return null;
+ }
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/ProviderTest.java b/luni/src/test/java/libcore/java/security/ProviderTest.java
index 2a1f59a..50eebc3 100644
--- a/luni/src/test/java/libcore/java/security/ProviderTest.java
+++ b/luni/src/test/java/libcore/java/security/ProviderTest.java
@@ -36,6 +36,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
+import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -1070,6 +1071,21 @@
assertEquals("default", p.getOrDefault("thisIsNotInTheProvider", "default"));
}
+ public void test_elements() {
+ Provider p = new MockProvider("MockProvider");
+ p.put("class1.algorithm1", "impl1");
+ Enumeration<Object> elements = p.elements();
+ boolean isImpl1Found = false;
+ while (elements.hasMoreElements()) {
+ if ("impl1".equals(elements.nextElement())) {
+ isImpl1Found = true;
+ break;
+ }
+ }
+
+ assertTrue("impl1 is not found.", isImpl1Found);
+ }
+
private static class Pair<A, B> {
private final A first;
private final B second;
diff --git a/luni/src/test/java/libcore/java/security/SecurityPermissionTest.java b/luni/src/test/java/libcore/java/security/SecurityPermissionTest.java
new file mode 100644
index 0000000..20fa1c7
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/SecurityPermissionTest.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.SecurityPermission;
+
+@RunWith(JUnit4.class)
+public class SecurityPermissionTest {
+
+ @Test
+ public void testConstructor() {
+ SecurityPermission permission = new SecurityPermission("name", "action");
+
+ // The name and actions are discarded from the constructor.
+ assertEquals("", permission.getName());
+ assertEquals("", permission.getActions());
+ }
+}
diff --git a/luni/src/test/java/libcore/java/security/UnresolvedPermissionTest.java b/luni/src/test/java/libcore/java/security/UnresolvedPermissionTest.java
new file mode 100644
index 0000000..4e8b2de
--- /dev/null
+++ b/luni/src/test/java/libcore/java/security/UnresolvedPermissionTest.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2021 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.java.security;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.security.UnresolvedPermission;
+import java.security.cert.Certificate;
+
+@RunWith(JUnit4.class)
+public class UnresolvedPermissionTest {
+
+ private final UnresolvedPermission p = new UnresolvedPermission("type", "name", "action",
+ new Certificate[0]);
+
+ @Test
+ public void testGetName() {
+ assertEquals("", p.getName());
+ }
+
+ @Test
+ public void testGetActions() {
+ assertNull(p.getActions());
+ }
+
+ @Test
+ public void testUnresolvedType() {
+ assertNull(p.getUnresolvedType());
+ }
+
+ @Test
+ public void testGetUnresolvedCertssss() {
+ assertNull(p.getUnresolvedCerts());
+ }
+
+ @Test
+ public void testGetUnresolvedName() {
+ assertNull(p.getUnresolvedName());
+ }
+
+ @Test
+ public void testGetUnresolvedActions() {
+ assertNull(p.getUnresolvedActions());
+ }
+
+ @Test
+ public void testImplies() {
+ assertFalse(p.implies(null));
+ assertFalse(p.implies(p));
+ }
+}
diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java
index 1362fc6..2cbf1ce 100644
--- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java
+++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/Identity2Test.java
@@ -253,6 +253,9 @@
java.security.Certificate[] certs = sub.certificates();
assertEquals("Certificate not contained in the identity",
certs[0], certImpl);
+
+ sub.removeCertificate(certs[0]);
+ assertEquals(0, sub.certificates().length);
}
/**
@@ -333,7 +336,7 @@
* java.security.Identity#toString()
*/
public void test_toString() throws Exception {
- IdentitySubclass sub = new IdentitySubclass("test", null);
+ Identity sub = new IdentitySubclass("test", null);
assertNotNull(sub.toString());
assertTrue("The String returned is not valid", sub.toString().length() > 0);
// Regression for HARMONY-1566
@@ -344,7 +347,7 @@
* java.security.Identity#toString(boolean)
*/
public void test_toStringZ() throws Exception {
- IdentitySubclass sub = new IdentitySubclass("test", null);
+ Identity sub = new IdentitySubclass("test", null);
assertNotNull(sub.toString(true));
assertTrue("The String returned is not valid", sub.toString(true).length() > 0);
}
diff --git a/luni/src/test/java/tests/java/security/SecureClassLoaderTest.java b/luni/src/test/java/tests/java/security/SecureClassLoaderTest.java
index 9a0761b..cb38d8d 100644
--- a/luni/src/test/java/tests/java/security/SecureClassLoaderTest.java
+++ b/luni/src/test/java/tests/java/security/SecureClassLoaderTest.java
@@ -165,13 +165,25 @@
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x00,
(byte) 0x21, };
- @KnownFailure("Android doesn't allow null parent.")
public void testSecureClassLoaderClassLoader() throws Exception {
URL[] urls = new URL[] { new URL("http://localhost") };
URLClassLoader ucl = URLClassLoader.newInstance(urls);
- new MyClassLoader(ucl);
+ MyClassLoader classLoader = new MyClassLoader(ucl);
try {
+ classLoader.tryDefineClass("ClassA", new byte[0], 0, 0, null);
+ fail("SecureClassloader.defineClass doesn't throw");
+ } catch (UnsupportedOperationException e) {}
+
+ try {
+ classLoader.tryDefineClass("ClassB", ByteBuffer.wrap(new byte[0]), null);
+ fail("SecureClassloader.defineClass doesn't throw");
+ } catch (UnsupportedOperationException e) {}
+ }
+
+ @KnownFailure("Android doesn't allow null parent.")
+ public void testNullParent() {
+ try {
new MyClassLoader(null);
} catch (Exception e) {
fail("unexpected exception: " + e);
@@ -233,5 +245,14 @@
return defineClass(name, b, off, len, cs);
}
+ public Class<?> tryDefineClass(String name, byte[] bytes, int off, int len,
+ CodeSource codeSource) {
+ return this.defineClass(name, bytes, off, len, codeSource);
+ }
+
+ public Class<?> tryDefineClass(String name, ByteBuffer byteBuffer, CodeSource codeSource) {
+ return this.defineClass(name, byteBuffer, codeSource);
+ }
+
}
}