Adding a check if the user exist before making a state mutation

Bug: 186272717
Test: Unit Test
Change-Id: Ica3814be111708a9adf614eb4f17fe446918c0a4
This commit is contained in:
Jackson Michael
2022-10-18 13:24:47 +00:00
parent 0eee4af3a5
commit cc749f79e7
2 changed files with 10 additions and 0 deletions

View File

@@ -2633,6 +2633,9 @@ public class UserManagerService extends IUserManager.Stub {
/** @return a specific user restriction that's in effect currently. */
@Override
public boolean hasUserRestriction(String restrictionKey, @UserIdInt int userId) {
if (!userExists(userId)) {
return false;
}
checkManageOrInteractPermissionIfCallerInOtherProfileGroup(userId, "hasUserRestriction");
return mLocalService.hasUserRestriction(restrictionKey, userId);
}

View File

@@ -175,6 +175,13 @@ public class UserManagerServiceTest {
assertThat(mUserManagerService.hasUserRestriction(DISALLOW_USER_SWITCH, userId)).isFalse();
}
@Test
public void testHasUserRestriction_NonExistentUserReturnsFalse() {
int nonExistentUserId = UserHandle.USER_NULL;
assertThat(mUserManagerService.hasUserRestriction(DISALLOW_USER_SWITCH, nonExistentUserId))
.isFalse();
}
@Test
public void testSetUserRestrictionWithIncorrectID() throws Exception {
int incorrectId = 1;