Class ShadowBinder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents the faked calling identity (UID, PID, UserHandle) of a remote Binder IPC caller. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static intprotected static intprotected static intprotected static UserHandleprotected voidlinkToDeath(IBinder.DeathRecipient deathRecipient, int flags) static voidreset()static ShadowBinder.CallingIdentityConfigures all theandroid.os.Binder#getCallingXXXmethods to reflect the specifiedShadowBinder.CallingIdentitywhen called from the current thread, for testing purposes.static voidsetCallingPid(int pid) ConfiguresBinder.getCallingPid()to return the specified Linux PID to subsequent callers on *any* thread, for testing purposes.static voidsetCallingUid(int uid) ConfiguresBinder.getCallingUid()to return the specified Linux UID to subsequent callers on *any* thread, for testing purposes.static voidsetCallingUserHandle(UserHandle userHandle) ConfiguresBinder.getCallingUserHandle()to return the specifiedUserHandleto subsequent callers on *any* thread, for testing purposes.protected booleanprotected booleanunlinkToDeath(IBinder.DeathRecipient deathRecipient, int flags)
-
Constructor Details
-
ShadowBinder
public ShadowBinder()
-
-
Method Details
-
transact
@Implementation protected boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException - Throws:
RemoteException
-
linkToDeath
-
unlinkToDeath
-
getCallingPid
-
getCallingUid
-
getCallingUidOrThrow
SeeBinder.getCallingUidOrThrow(). Whether or not this returns a value is controlled bysetCallingUid(int)(to set the value to be returned) or byreset()(to trigger the exception).- Returns:
- the value set by
setCallingUid(int) - Throws:
IllegalStateException- if no UID has been set
-
getCallingUserHandle
-
getDeathRecipients
-
setCallingPid
public static void setCallingPid(int pid) ConfiguresBinder.getCallingPid()to return the specified Linux PID to subsequent callers on *any* thread, for testing purposes.Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using
setCallingIdentityForCurrentThread(CallingIdentity)instead. -
setCallingUid
public static void setCallingUid(int uid) ConfiguresBinder.getCallingUid()to return the specified Linux UID to subsequent callers on *any* thread, for testing purposes.Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using
setCallingIdentityForCurrentThread(CallingIdentity)instead. -
setCallingUserHandle
ConfiguresBinder.getCallingUserHandle()to return the specifiedUserHandleto subsequent callers on *any* thread, for testing purposes.Warning: This is a global setting affecting all threads in your test process (very different than real Android). It can cause flakiness or unexpected behavior in multi-threaded tests. Prefer to configure your simulated Binder IPC callers one at a time using
setCallingIdentityForCurrentThread(CallingIdentity)instead. -
setCallingIdentityForCurrentThread
@Nullable public static ShadowBinder.CallingIdentity setCallingIdentityForCurrentThread(@Nullable ShadowBinder.CallingIdentity identity) Configures all theandroid.os.Binder#getCallingXXXmethods to reflect the specifiedShadowBinder.CallingIdentitywhen called from the current thread, for testing purposes.Use this method to prepare the current thread to deliver a Binder IPC transaction under a fake remote identity. You can configure this thread's apparent UID, PID and UserHandle. Call this method again (unconditionally, in a finally block, say) after the transaction completes to restore this thread's previous IPC identity (or clear it).
- Parameters:
identity- the identity to set, ornullto clear- Returns:
- this thread's previous fake identity, or
nullif none was set.
-
reset
-