summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ethan Lee <ethanalee@google.com> 2022-11-29 02:22:06 +0000
committer Ethan Lee <ethanalee@google.com> 2022-12-01 21:13:42 +0000
commiteb90e9679db760cdb7affe444631cbcad461fbd7 (patch)
treebe22f4ca56daf55838e51fef4e7885e3404f4623
parent8dadc7e871a6908b84c8d924f07b0f0e60fee529 (diff)
Fix UserController to not throw in stopUser
Return an USER_OP_* instead of throwing an IllegalException Test: atest UserControllerTest#stopUser_invalidUser UserControllerTest#stopUser_systemUser UserControllerTest#stopUser_currentUser Fixes: 259740023 Change-Id: I7e50412e028d225b7c97e113a3e4aac138da2314
-rw-r--r--services/core/java/com/android/server/am/UserController.java5
-rw-r--r--services/tests/servicestests/src/com/android/server/am/UserControllerTest.java33
2 files changed, 35 insertions, 3 deletions
diff --git a/services/core/java/com/android/server/am/UserController.java b/services/core/java/com/android/server/am/UserController.java
index 4d86140816ea..d0d0a6f2ff30 100644
--- a/services/core/java/com/android/server/am/UserController.java
+++ b/services/core/java/com/android/server/am/UserController.java
@@ -958,9 +958,8 @@ class UserController implements Handler.Callback {
int stopUser(final int userId, final boolean force, boolean allowDelayedLocking,
final IStopUserCallback stopUserCallback, KeyEvictedCallback keyEvictedCallback) {
checkCallingPermission(INTERACT_ACROSS_USERS_FULL, "stopUser");
- if (userId < 0 || userId == UserHandle.USER_SYSTEM) {
- throw new IllegalArgumentException("Can't stop system user " + userId);
- }
+ Preconditions.checkArgument(userId >= 0, "Invalid user id %d", userId);
+
enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, userId);
synchronized (mLock) {
return stopUsersLU(userId, force, allowDelayedLocking, stopUserCallback,
diff --git a/services/tests/servicestests/src/com/android/server/am/UserControllerTest.java b/services/tests/servicestests/src/com/android/server/am/UserControllerTest.java
index a49214f9b4f5..15b7cb576adc 100644
--- a/services/tests/servicestests/src/com/android/server/am/UserControllerTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/UserControllerTest.java
@@ -645,6 +645,39 @@ public class UserControllerTest {
/* keyEvictedCallback= */ mKeyEvictedCallback, /* expectLocking= */ true);
}
+ @Test
+ public void testStopUser_invalidUser() {
+ int userId = -1;
+
+ assertThrows(IllegalArgumentException.class,
+ () -> mUserController.stopUser(userId, /* force= */ true,
+ /* allowDelayedLocking= */ true, /* stopUserCallback= */ null,
+ /* keyEvictedCallback= */ null));
+ }
+
+ @Test
+ public void testStopUser_systemUser() {
+ int userId = UserHandle.USER_SYSTEM;
+
+ int r = mUserController.stopUser(userId, /* force= */ true,
+ /* allowDelayedLocking= */ true, /* stopUserCallback= */ null,
+ /* keyEvictedCallback= */ null);
+
+ assertThat(r).isEqualTo(ActivityManager.USER_OP_ERROR_IS_SYSTEM);
+ }
+
+ @Test
+ public void testStopUser_currentUser() {
+ setUpUser(TEST_USER_ID1, /* flags= */ 0);
+ mUserController.startUser(TEST_USER_ID1, /* foreground= */ true);
+
+ int r = mUserController.stopUser(TEST_USER_ID1, /* force= */ true,
+ /* allowDelayedLocking= */ true, /* stopUserCallback= */ null,
+ /* keyEvictedCallback= */ null);
+
+ assertThat(r).isEqualTo(ActivityManager.USER_OP_IS_CURRENT);
+ }
+
/**
* Test conditional delayed locking with mDelayUserDataLocking true.
*/