Skip to content

Commit 9281c2b

Browse files
committed
Add testResolveNonExistentServiceWithTargetuserThrows
1 parent bac0877 commit 9281c2b

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

binder/src/main/java/io/grpc/binder/internal/ServiceBinding.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ void unbindInternal(Status reason) {
252252
}
253253
}
254254

255+
// Sadly we must call this system API reflectively since it isn't part of the Android SDK.
255256
private static int getIdentifier(UserHandle userHandle) throws ReflectiveOperationException {
256-
return (int) userHandle.getClass().getDeclaredMethod("getIdentifier").invoke(userHandle);
257+
return (int) userHandle.getClass().getMethod("getIdentifier").invoke(userHandle);
257258
}
258259

260+
// Sadly we must call this system API reflectively since it isn't part of the Android SDK.
259261
private static ResolveInfo resolveServiceAsUser(
260262
PackageManager packageManager, Intent intent, int flags, UserHandle targetUserHandle) {
261263
try {
@@ -279,7 +281,7 @@ public ServiceInfo resolve() throws StatusException {
279281
: packageManager.resolveService(bindIntent, 0);
280282
if (resolveInfo == null) {
281283
throw Status.UNIMPLEMENTED // Same status code as when bindService() returns false.
282-
.withDescription("resolveService(" + bindIntent + ") returned null")
284+
.withDescription("resolveService(" + bindIntent + " / " + targetUserHandle + ") was null")
283285
.asException();
284286
}
285287
return resolveInfo.serviceInfo;

binder/src/test/java/io/grpc/binder/internal/ServiceBindingTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,19 @@ public void testResolveNonExistentServiceThrows() throws Exception {
312312
assertThat(statusException.getStatus().getDescription()).contains("does.not.exist");
313313
}
314314

315+
@Test
316+
@Config(sdk = 33)
317+
public void testResolveNonExistentServiceWithTargetUserThrows() throws Exception {
318+
ComponentName doesNotExistService = new ComponentName("does.not.exist", "NoService");
319+
binding = newBuilder()
320+
.setTargetUserHandle(generateUserHandle(/* userId = */ 12345))
321+
.setTargetComponent(doesNotExistService).build();
322+
StatusException statusException = assertThrows(StatusException.class, binding::resolve);
323+
assertThat(statusException.getStatus().getCode()).isEqualTo(Code.UNIMPLEMENTED);
324+
assertThat(statusException.getStatus().getDescription()).contains("does.not.exist");
325+
assertThat(statusException.getStatus().getDescription()).contains("12345");
326+
}
327+
315328
@Test
316329
@Config(sdk = 30)
317330
public void testBindWithDeviceAdmin() throws Exception {

0 commit comments

Comments
 (0)