diff options
Diffstat (limited to 'tests/java/src/android/aidl')
10 files changed, 375 insertions, 6 deletions
diff --git a/tests/java/src/android/aidl/permission/service/FakePermissionEnforcer.java b/tests/java/src/android/aidl/permission/service/FakePermissionEnforcer.java new file mode 100644 index 00000000..f7b694a5 --- /dev/null +++ b/tests/java/src/android/aidl/permission/service/FakePermissionEnforcer.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2022 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 android.aidl.permission.service; + +import static android.permission.PermissionManager.PERMISSION_GRANTED; +import static android.permission.PermissionManager.PERMISSION_HARD_DENIED; + +import android.annotation.NonNull; +import android.content.AttributionSource; +import java.util.List; + +/* Fake for android.os.PermissionEnforcer + */ +public class FakePermissionEnforcer extends android.os.PermissionEnforcer { + private List<String> mGranted; + + public void setGranted(List<String> granted) { mGranted = granted; } + + @Override + protected int checkPermission(@NonNull String permission, int pid, int uid) { + if (mGranted != null && mGranted.contains(permission)) { + return PERMISSION_GRANTED; + } + return PERMISSION_HARD_DENIED; + } + + @Override + protected int checkPermission(@NonNull String permission, @NonNull AttributionSource source) { + if (mGranted != null && mGranted.contains(permission)) { + return PERMISSION_GRANTED; + } + return PERMISSION_HARD_DENIED; + } +} diff --git a/tests/java/src/android/aidl/permission/service/PermissionTestService.java b/tests/java/src/android/aidl/permission/service/PermissionTestService.java new file mode 100644 index 00000000..b65b5dff --- /dev/null +++ b/tests/java/src/android/aidl/permission/service/PermissionTestService.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2022, 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 android.aidl.permission.service; + +import android.aidl.tests.permission.IProtected; +import android.annotation.EnforcePermission; +import android.os.Binder; +import android.os.ServiceManager; +import java.util.List; + +public class PermissionTestService extends IProtected.Stub { + private FakePermissionEnforcer mPermissionEnforcer; + + public static void main(String[] args) { + PermissionTestService myServer = new PermissionTestService(new FakePermissionEnforcer()); + ServiceManager.addService(IProtected.class.getName(), myServer); + + Binder.joinThreadPool(); + } + + public PermissionTestService(FakePermissionEnforcer permissionEnforcer) { + super(permissionEnforcer); + mPermissionEnforcer = permissionEnforcer; + } + + @Override + @EnforcePermission("READ_PHONE_STATE") + public void PermissionProtected() { + PermissionProtected_enforcePermission(); + } + + @Override + @EnforcePermission(allOf = {"INTERNET", "VIBRATE"}) + public void MultiplePermissionsAll() { + MultiplePermissionsAll_enforcePermission(); + } + + @Override + @EnforcePermission(anyOf = {"INTERNET", "VIBRATE"}) + public void MultiplePermissionsAny() { + MultiplePermissionsAny_enforcePermission(); + } + + @Override + @EnforcePermission("android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK") + public void NonManifestPermission() { + NonManifestPermission_enforcePermission(); + } + + @Override + public void SetGranted(List<String> permissions) { + mPermissionEnforcer.setGranted(permissions); + } +} diff --git a/tests/java/src/android/aidl/permission/tests/PermissionTests.java b/tests/java/src/android/aidl/permission/tests/PermissionTests.java new file mode 100644 index 00000000..91a9f420 --- /dev/null +++ b/tests/java/src/android/aidl/permission/tests/PermissionTests.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2022, 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 android.aidl.permission.tests; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import android.aidl.tests.permission.IProtected; +import android.os.IBinder; +import android.os.RemoteException; +import android.os.ServiceManager; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.internal.TextListener; +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public abstract class PermissionTests { + protected IProtected service; + + @Test + public void testProtected() throws Exception { + // Requires READ_PHONE_STATE. + service.SetGranted(List.of()); + assertThrows(SecurityException.class, () -> service.PermissionProtected()); + service.SetGranted(List.of("android.permission.READ_PHONE_STATE")); + service.PermissionProtected(); + } + + @Test + public void testMultiplePermissionsAll() throws Exception { + // Requires INTERNET and VIBRATE. + service.SetGranted(List.of()); + assertThrows(SecurityException.class, () -> service.MultiplePermissionsAll()); + service.SetGranted(List.of("android.permission.INTERNET")); + assertThrows(SecurityException.class, () -> service.MultiplePermissionsAll()); + service.SetGranted(List.of("android.permission.VIBRATE")); + assertThrows(SecurityException.class, () -> service.MultiplePermissionsAll()); + service.SetGranted(List.of("android.permission.INTERNET", "android.permission.VIBRATE")); + service.MultiplePermissionsAll(); + } + + @Test + public void testMultiplePermissionsAny() throws Exception { + // Requires INTERNET or VIBRATE. + service.SetGranted(List.of()); + assertThrows(SecurityException.class, () -> service.MultiplePermissionsAny()); + service.SetGranted(List.of("android.permission.INTERNET")); + service.MultiplePermissionsAny(); + service.SetGranted(List.of("android.permission.VIBRATE")); + service.MultiplePermissionsAny(); + } + + @Test + public void testNonManifestPermission() throws Exception { + // Requires android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK + service.SetGranted(List.of()); + assertThrows(SecurityException.class, () -> service.NonManifestPermission()); + service.SetGranted(List.of("android.permission.MAINLINE_NETWORK_STACK")); + service.NonManifestPermission(); + } + + public static void main(String[] args) { + JUnitCore junit = new JUnitCore(); + junit.addListener(new TextListener(System.out)); + Result result = junit.run(PermissionTestsRemote.class, PermissionTestsLocal.class); + System.out.println(result.wasSuccessful() ? "TEST SUCCESS" : "TEST FAILURE"); + } +} diff --git a/tests/java/src/android/aidl/permission/tests/PermissionTestsLocal.java b/tests/java/src/android/aidl/permission/tests/PermissionTestsLocal.java new file mode 100644 index 00000000..3e3146e3 --- /dev/null +++ b/tests/java/src/android/aidl/permission/tests/PermissionTestsLocal.java @@ -0,0 +1,38 @@ + +/* + * Copyright (C) 2022, 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 android.aidl.permission.tests; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import android.aidl.permission.service.FakePermissionEnforcer; +import android.aidl.permission.service.PermissionTestService; +import android.aidl.tests.permission.IProtected; +import android.os.RemoteException; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class PermissionTestsLocal extends PermissionTests { + @Before + public void setUp() throws RemoteException { + service = new PermissionTestService(new FakePermissionEnforcer()); + assertNotNull(service); + } +} diff --git a/tests/java/src/android/aidl/permission/tests/PermissionTestsRemote.java b/tests/java/src/android/aidl/permission/tests/PermissionTestsRemote.java new file mode 100644 index 00000000..5602fe83 --- /dev/null +++ b/tests/java/src/android/aidl/permission/tests/PermissionTestsRemote.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022, 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 android.aidl.permission.tests; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; + +import android.aidl.tests.permission.IProtected; +import android.os.IBinder; +import android.os.RemoteException; +import android.os.ServiceManager; +import org.junit.Before; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class PermissionTestsRemote extends PermissionTests { + @Before + public void setUp() throws RemoteException { + IBinder binder = ServiceManager.waitForService(IProtected.class.getName()); + assertNotNull(binder); + service = IProtected.Stub.asInterface(binder); + assertNotNull(service); + } +} diff --git a/tests/java/src/android/aidl/sdkversion/ITestService.aidl b/tests/java/src/android/aidl/sdkversion/ITestService.aidl index afefddef..b3302ada 100644 --- a/tests/java/src/android/aidl/sdkversion/ITestService.aidl +++ b/tests/java/src/android/aidl/sdkversion/ITestService.aidl @@ -19,10 +19,15 @@ interface ITestService { // Since sdk = 29, we have write/readBoolean boolean RepeatBoolean(boolean token); - // Since sdk = 23, we have write/readTypedObject @JavaDerive(toString=true, equals=true) parcelable TypedObject { boolean b; } + + // Since sdk = 23, we have write/readTypedObject @nullable TypedObject RepeatTypedObject(in @nullable TypedObject token); + + // Since sdk = 33, we have write/readTypedList (with parcelableFlags) + @nullable List<TypedObject> RepeatTypedList( + in @nullable List<TypedObject> tokens); } diff --git a/tests/java/src/android/aidl/sdkversion/service/AidlJavaVersionTestService.java b/tests/java/src/android/aidl/sdkversion/service/AidlJavaVersionTestService.java index ca108c87..91d1024f 100644 --- a/tests/java/src/android/aidl/sdkversion/service/AidlJavaVersionTestService.java +++ b/tests/java/src/android/aidl/sdkversion/service/AidlJavaVersionTestService.java @@ -20,6 +20,7 @@ import android.aidl.sdkversion.ITestService; import android.aidl.sdkversion.ITestService.TypedObject; import android.os.Binder; import android.os.ServiceManager; +import java.util.List; public class AidlJavaVersionTestService { static class TestServiceServer extends ITestService.Stub { @@ -32,6 +33,11 @@ public class AidlJavaVersionTestService { public TypedObject RepeatTypedObject(TypedObject token) { return token; } + + @Override + public List<TypedObject> RepeatTypedList(List<TypedObject> token) { + return token; + } } public static void main(String[] args) { diff --git a/tests/java/src/android/aidl/sdkversion/tests/AidlJavaVersionTests.java b/tests/java/src/android/aidl/sdkversion/tests/AidlJavaVersionTests.java index 7239ad7c..27b4208f 100644 --- a/tests/java/src/android/aidl/sdkversion/tests/AidlJavaVersionTests.java +++ b/tests/java/src/android/aidl/sdkversion/tests/AidlJavaVersionTests.java @@ -27,6 +27,8 @@ import android.aidl.sdkversion.ITestService.TypedObject; import android.os.IBinder; import android.os.RemoteException; import android.os.ServiceManager; +import java.util.ArrayList; +import java.util.List; import org.junit.Before; import org.junit.Test; import org.junit.internal.TextListener; @@ -65,5 +67,8 @@ public class AidlJavaVersionTests { TypedObject token = new TypedObject(); assertEquals(mService.RepeatTypedObject(token), token); assertNull(mService.RepeatTypedObject(null)); + List<TypedObject> list = new ArrayList<>(); + list.add(token); + assertEquals(mService.RepeatTypedList(list), list); } } diff --git a/tests/java/src/android/aidl/service/TestServiceServer.java b/tests/java/src/android/aidl/service/TestServiceServer.java index 51a75205..7843d9ce 100644 --- a/tests/java/src/android/aidl/service/TestServiceServer.java +++ b/tests/java/src/android/aidl/service/TestServiceServer.java @@ -21,8 +21,10 @@ import android.aidl.fixedsizearray.FixedSizeArrayExample.IntParcelable; import android.aidl.tests.BackendType; import android.aidl.tests.BadParcelable; import android.aidl.tests.ByteEnum; +import android.aidl.tests.CircularParcelable; import android.aidl.tests.ConstantExpressionEnum; import android.aidl.tests.GenericStructuredParcelable; +import android.aidl.tests.ICircular; import android.aidl.tests.ICppJavaTests; import android.aidl.tests.INamedCallback; import android.aidl.tests.INewName; @@ -59,6 +61,13 @@ import java.util.List; public class TestServiceServer extends ITestService.Stub { public static void main(String[] args) { + // b/235006086: test with debug stack trace parceling feature + // which has been broken in the past. This does mean that we + // lose Java coverage for when this is false, but we do have + // other tests which cover this including CtsNdkBinderTestCases + // and other language-specific exception/Status unit tests. + Parcel.setStackTraceParceling(true); + TestServiceServer myServer = new TestServiceServer(); ServiceManager.addService(ITestService.class.getName(), myServer); @@ -94,11 +103,11 @@ public class TestServiceServer extends ITestService.Stub { } @Override public final int getInterfaceVersion() { - return IFooInterface.VERSION; + return super.VERSION; } @Override public final String getInterfaceHash() { - return IFooInterface.HASH; + return super.HASH; } } @@ -298,10 +307,27 @@ public class TestServiceServer extends ITestService.Stub { @Override public INamedCallback GetOtherTestService(String name) throws RemoteException { - if (!mNamedCallbacks.containsKey(name)) { - mNamedCallbacks.put(name, new MyNamedCallback(name)); + synchronized (mNamedCallbacks) { + if (!mNamedCallbacks.containsKey(name)) { + mNamedCallbacks.put(name, new MyNamedCallback(name)); + } + return mNamedCallbacks.get(name); + } + } + @Override + public boolean SetOtherTestService(String name, INamedCallback service) throws RemoteException { + synchronized (mNamedCallbacks) { + if (mNamedCallbacks.containsKey(name) && mNamedCallbacks.get(name) == service) { + return true; + } + try { + // This restricts the client to only setting services that it gets from this server. + mNamedCallbacks.put(name, (MyNamedCallback) service); + } catch (Exception e) { + Log.i("TestServiceServer", "Failed to cast service"); + } + return false; } - return mNamedCallbacks.get(name); } @Override public boolean VerifyName(INamedCallback service, String name) throws RemoteException { @@ -758,4 +784,21 @@ public class TestServiceServer extends ITestService.Stub { public byte getBackendType() throws RemoteException { return BackendType.JAVA; } + + private static class MyCircular extends ICircular.Stub { + private ITestService mSrv; + + MyCircular(ITestService srv) { mSrv = srv; } + + @Override + public ITestService GetTestService() { + return mSrv; + } + } + + @Override + public ICircular GetCircular(CircularParcelable cp) throws RemoteException { + cp.testService = this; + return new MyCircular(this); + } } diff --git a/tests/java/src/android/aidl/tests/TestServiceClient.java b/tests/java/src/android/aidl/tests/TestServiceClient.java index 098ad209..6f298864 100644 --- a/tests/java/src/android/aidl/tests/TestServiceClient.java +++ b/tests/java/src/android/aidl/tests/TestServiceClient.java @@ -134,6 +134,28 @@ public class TestServiceClient { } @Test + public void testConstFloatRepeat() throws RemoteException { + float query[] = {ITestService.FLOAT_TEST_CONSTANT, ITestService.FLOAT_TEST_CONSTANT2, + ITestService.FLOAT_TEST_CONSTANT3, ITestService.FLOAT_TEST_CONSTANT4, + ITestService.FLOAT_TEST_CONSTANT5, ITestService.FLOAT_TEST_CONSTANT6, + ITestService.FLOAT_TEST_CONSTANT7}; + for (int i = 0; i < query.length; i++) { + assertThat(service.RepeatFloat(query[i]), is(query[i])); + } + } + + @Test + public void testConstDoubleRepeat() throws RemoteException { + double query[] = {ITestService.DOUBLE_TEST_CONSTANT, ITestService.DOUBLE_TEST_CONSTANT2, + ITestService.DOUBLE_TEST_CONSTANT3, ITestService.DOUBLE_TEST_CONSTANT4, + ITestService.DOUBLE_TEST_CONSTANT5, ITestService.DOUBLE_TEST_CONSTANT6, + ITestService.DOUBLE_TEST_CONSTANT7}; + for (int i = 0; i < query.length; i++) { + assertThat(service.RepeatDouble(query[i]), is(query[i])); + } + } + + @Test public void testLongRepeat() throws RemoteException { long query = 1L << 60; assertThat(service.RepeatLong(query), is(query)); @@ -646,6 +668,14 @@ public class TestServiceClient { } } + @Test + public void testOutArrayWithNull() throws RemoteException { + // In Java out-array parameter can't be null. The proxy code always throws NPE. + assertThrows(NullPointerException.class, () -> service.ReverseUtf8CppString(null, null)); + assertThrows(NullPointerException.class, + () -> service.ReverseNullableUtf8CppString(null, null)); + } + private void shouldBeTheSame(StructuredParcelable a, StructuredParcelable b) { assertTrue(a.equals(b)); assertTrue(b.equals(a)); |