From 0149ca4afd75fe2e213171f54333644b416a2f79 Mon Sep 17 00:00:00 2001
From: Parth Sane <parthsane@google.com>
Date: Thu, 23 May 2024 13:32:06 +0000
Subject: [PATCH] Add tests for ServiceManager Java Binder API

Bug: 333854840
Test: atest android.os.BinderTest
Change-Id: Iaf1fd1e5daea7103a873bc8f8aa0b78377e57714
---
 .../coretests/src/android/os/BinderTest.java  | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/core/tests/coretests/src/android/os/BinderTest.java b/core/tests/coretests/src/android/os/BinderTest.java
index 6c8b69fc9c5c..9767d677807d 100644
--- a/core/tests/coretests/src/android/os/BinderTest.java
+++ b/core/tests/coretests/src/android/os/BinderTest.java
@@ -20,6 +20,8 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.anyInt;
 import static org.testng.Assert.assertThrows;
 
 import android.platform.test.annotations.IgnoreUnderRavenwood;
@@ -27,6 +29,9 @@ import android.platform.test.ravenwood.RavenwoodRule;
 
 import androidx.test.filters.SmallTest;
 
+import com.android.internal.os.BinderInternal;
+
+
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -81,4 +86,27 @@ public class BinderTest {
         binder.setExtension(null);
         assertNull(binder.getExtension());
     }
+
+    @SmallTest
+    @Test(expected = java.lang.SecurityException.class)
+    public void testServiceManagerNativeSecurityException() throws RemoteException {
+        // Find the service manager
+        IServiceManager sServiceManager = ServiceManagerNative
+                .asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));
+
+        Binder binder = new Binder();
+        sServiceManager.addService("ValidName",  binder,
+                anyBoolean(), anyInt());
+    }
+
+    @SmallTest
+    @Test(expected = java.lang.NullPointerException.class)
+    public void testServiceManagerNativeNullptrException() throws RemoteException {
+        // Find the service manager
+        IServiceManager sServiceManager = ServiceManagerNative
+                .asInterface(Binder.allowBlocking(BinderInternal.getContextObject()));
+
+        sServiceManager.addService("ValidName",  null,
+                anyBoolean(), anyInt());
+    }
 }
-- 
GitLab