aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelipe Leme <felipeal@google.com>2020-11-18 18:22:44 -0800
committerFelipe Leme <felipeal@google.com>2020-11-19 22:46:48 -0800
commitcceb2998620df607c36d2f3584f610e6a2af1a4c (patch)
tree96f0778e0baaa52d7bb17bf3f9c3194a96e31a78
parent2c240faa4aec2439117f30fa3d0437612796d106 (diff)
downloadCar-cceb2998620df607c36d2f3584f610e6a2af1a4c.tar.gz
Refactored InitialUserSetter to use createUserEvenWhenDisallowed().
Test: manual verification Test: atest InitialUserSetterTest CarUserServiceUnitTest Fixes: 172691669 Change-Id: I0358ad1e49c1e622af56d9d7d7cb29b60eb1d22f
-rw-r--r--car-internal-lib/src/com/android/car/internal/ICarServiceHelper.aidl5
-rw-r--r--service/src/com/android/car/user/CarUserService.java24
-rw-r--r--service/src/com/android/car/user/InitialUserSetter.java21
-rw-r--r--tests/carservice_unit_test/src/com/android/car/AbstractICarServiceHelperStub.java8
-rw-r--r--tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java42
-rw-r--r--tests/carservice_unit_test/src/com/android/car/user/InitialUserSetterTest.java17
6 files changed, 100 insertions, 17 deletions
diff --git a/car-internal-lib/src/com/android/car/internal/ICarServiceHelper.aidl b/car-internal-lib/src/com/android/car/internal/ICarServiceHelper.aidl
index 4d7785ff4b..22148eb44b 100644
--- a/car-internal-lib/src/com/android/car/internal/ICarServiceHelper.aidl
+++ b/car-internal-lib/src/com/android/car/internal/ICarServiceHelper.aidl
@@ -17,6 +17,7 @@
package com.android.car.internal;
import android.content.ComponentName;
+import android.content.pm.UserInfo;
import java.util.List;
@@ -50,4 +51,8 @@ interface ICarServiceHelper {
*/
void setSafetyMode(boolean safe);
+ /**
+ * Creates the given user, even when it's disallowed by DevicePolicyManager.
+ */
+ UserInfo createUserEvenWhenDisallowed(String name, String userType, int flags);
}
diff --git a/service/src/com/android/car/user/CarUserService.java b/service/src/com/android/car/user/CarUserService.java
index e63870f9e2..a49cbdffff 100644
--- a/service/src/com/android/car/user/CarUserService.java
+++ b/service/src/com/android/car/user/CarUserService.java
@@ -321,8 +321,8 @@ public final class CarUserService extends ICarUserService.Stub implements CarSer
mUserManager = userManager;
mLastPassengerId = UserHandle.USER_NULL;
mInitialUserSetter =
- initialUserSetter == null ? new InitialUserSetter(context, (u) -> setInitialUser(u))
- : initialUserSetter;
+ initialUserSetter == null ? new InitialUserSetter(context, this,
+ (u) -> setInitialUser(u)) : initialUserSetter;
mUserPreCreator =
userPreCreator == null ? new UserPreCreator(mUserManager) : userPreCreator;
Resources resources = context.getResources();
@@ -1244,6 +1244,26 @@ public final class CarUserService extends ICarUserService.Stub implements CarSer
}
}
+ /**
+ * Used to create the initial user, even when it's disallowed by {@code DevicePolicyManager}.
+ */
+ @Nullable
+ UserInfo createUserEvenWhenDisallowed(@Nullable String name, @NonNull String userType,
+ @UserInfoFlag int flags) {
+ if (mICarServiceHelper == null) {
+ Log.wtf(TAG, "createUserEvenWhenDisallowed(): mICarServiceHelper not set yet",
+ new Exception());
+ return null;
+ }
+ try {
+ return mICarServiceHelper.createUserEvenWhenDisallowed(name, userType, flags);
+ } catch (RemoteException e) {
+ Log.e(TAG, "createUserEvenWhenDisallowed(" + UserHelperLite.safeName(name) + ", "
+ + userType + ", " + UserInfo.flagsToString(flags) + ") failed", e);
+ return null;
+ }
+ }
+
@Override
public void createUser(@Nullable String name, @NonNull String userType, @UserInfoFlag int flags,
int timeoutMs, @NonNull AndroidFuture<UserCreationResult> receiver) {
diff --git a/service/src/com/android/car/user/InitialUserSetter.java b/service/src/com/android/car/user/InitialUserSetter.java
index c7375a9375..10a6b2eab3 100644
--- a/service/src/com/android/car/user/InitialUserSetter.java
+++ b/service/src/com/android/car/user/InitialUserSetter.java
@@ -117,6 +117,7 @@ final class InitialUserSetter {
// TODO(b/150413304): abstract AM / UM into interfaces, then provide local and remote
// implementation (where local is implemented by ActivityManagerInternal / UserManagerInternal)
private final UserManager mUm;
+ private final CarUserService mCarUserService;
private final LockPatternUtils mLockPatternUtils;
private final String mNewUserName;
@@ -124,22 +125,26 @@ final class InitialUserSetter {
private final Consumer<UserInfo> mListener;
- InitialUserSetter(@NonNull Context context, @NonNull Consumer<UserInfo> listener) {
- this(context, listener, /* newGuestName= */ null);
+ InitialUserSetter(@NonNull Context context, @NonNull CarUserService carUserService,
+ @NonNull Consumer<UserInfo> listener) {
+ this(context, carUserService, listener, /* newGuestName= */ null);
}
- InitialUserSetter(@NonNull Context context, @NonNull Consumer<UserInfo> listener,
- @Nullable String newGuestName) {
- this(context, UserManager.get(context), listener, new LockPatternUtils(context),
+ InitialUserSetter(@NonNull Context context, @NonNull CarUserService carUserService,
+ @NonNull Consumer<UserInfo> listener, @Nullable String newGuestName) {
+ this(context, UserManager.get(context), carUserService, listener,
+ new LockPatternUtils(context),
context.getString(com.android.internal.R.string.owner_name), newGuestName);
}
@VisibleForTesting
InitialUserSetter(@NonNull Context context, @NonNull UserManager um,
- @NonNull Consumer<UserInfo> listener, @NonNull LockPatternUtils lockPatternUtils,
- @Nullable String newUserName, @Nullable String newGuestName) {
+ @NonNull CarUserService carUserService, @NonNull Consumer<UserInfo> listener,
+ @NonNull LockPatternUtils lockPatternUtils, @Nullable String newUserName,
+ @Nullable String newGuestName) {
mContext = context;
mUm = um;
+ mCarUserService = carUserService;
mListener = listener;
mLockPatternUtils = lockPatternUtils;
mNewUserName = newUserName;
@@ -585,7 +590,7 @@ final class InitialUserSetter {
+ type + ", flags=" + UserInfo.flagsToString(flags) + ")");
}
- UserInfo userInfo = mUm.createUser(name, type, flags);
+ UserInfo userInfo = mCarUserService.createUserEvenWhenDisallowed(name, type, flags);
if (userInfo == null) {
return new Pair<>(null, "createUser(name=" + UserHelperLite.safeName(name) + ", flags="
+ userFlagsToString(halFlags) + "): failed to create user");
diff --git a/tests/carservice_unit_test/src/com/android/car/AbstractICarServiceHelperStub.java b/tests/carservice_unit_test/src/com/android/car/AbstractICarServiceHelperStub.java
index 69f3dc7f7a..7304e23655 100644
--- a/tests/carservice_unit_test/src/com/android/car/AbstractICarServiceHelperStub.java
+++ b/tests/carservice_unit_test/src/com/android/car/AbstractICarServiceHelperStub.java
@@ -17,6 +17,7 @@ package com.android.car;
import android.annotation.UserIdInt;
import android.content.ComponentName;
+import android.content.pm.UserInfo;
import android.os.RemoteException;
import android.util.Log;
@@ -65,4 +66,11 @@ abstract class AbstractICarServiceHelperStub extends ICarServiceHelper.Stub {
public void setSafetyMode(boolean safe) {
Log.d(TAG, "setSafetyMode(safe=" + safe + ")");
}
+
+ @Override
+ public UserInfo createUserEvenWhenDisallowed(String name, String userType, int flags) {
+ Log.d(TAG, "createUserEvenWhenDisallowed(name=" + name + ", userType=" + userType
+ + ", flags=" + flags + ")");
+ return null;
+ }
}
diff --git a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
index 944672ae19..b22bcb4b64 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/CarUserServiceTest.java
@@ -1727,6 +1727,48 @@ public final class CarUserServiceTest extends AbstractExtendedMockitoTestCase {
}
@Test
+ @ExpectWtf
+ public void testCreateUserEvenWhenDisallowed_noHelper() throws Exception {
+ UserInfo userInfo = mCarUserService.createUserEvenWhenDisallowed("name",
+ UserManager.USER_TYPE_FULL_SECONDARY, UserInfo.FLAG_ADMIN);
+
+ assertThat(userInfo).isNull();
+ }
+
+ @Test
+ public void testCreateUserEvenWhenDisallowed_remoteException() throws Exception {
+ mCarUserService.setCarServiceHelper(mICarServiceHelper);
+ when(mICarServiceHelper.createUserEvenWhenDisallowed(any(), any(), anyInt()))
+ .thenThrow(new RemoteException("D'OH!"));
+
+ UserInfo userInfo = mCarUserService.createUserEvenWhenDisallowed("name",
+ UserManager.USER_TYPE_FULL_SECONDARY, UserInfo.FLAG_ADMIN);
+
+ assertThat(userInfo).isNull();
+ }
+
+ @Test
+ public void testCreateUserEvenWhenDisallowed_success() throws Exception {
+ UserInfo user = new UserInfoBuilder(100)
+ .setName("name")
+ .setType(UserManager.USER_TYPE_FULL_SECONDARY)
+ .setFlags(UserInfo.FLAG_ADMIN)
+ .build();
+ mCarUserService.setCarServiceHelper(mICarServiceHelper);
+ when(mICarServiceHelper.createUserEvenWhenDisallowed("name",
+ UserManager.USER_TYPE_FULL_SECONDARY, UserInfo.FLAG_ADMIN)).thenReturn(user);
+
+ UserInfo actualUser = mCarUserService.createUserEvenWhenDisallowed("name",
+ UserManager.USER_TYPE_FULL_SECONDARY, UserInfo.FLAG_ADMIN);
+
+ assertThat(actualUser).isNotNull();
+ assertThat(actualUser.id).isEqualTo(100);
+ assertThat(actualUser.name).isEqualTo("name");
+ assertThat(actualUser.userType).isEqualTo(UserManager.USER_TYPE_FULL_SECONDARY);
+ assertThat(actualUser.flags).isEqualTo(UserInfo.FLAG_ADMIN);
+ }
+
+ @Test
public void testIsHalSupported() throws Exception {
when(mUserHal.isSupported()).thenReturn(true);
assertThat(mCarUserService.isUserHalSupported()).isTrue();
diff --git a/tests/carservice_unit_test/src/com/android/car/user/InitialUserSetterTest.java b/tests/carservice_unit_test/src/com/android/car/user/InitialUserSetterTest.java
index e42d3aa78a..3a7430b49d 100644
--- a/tests/carservice_unit_test/src/com/android/car/user/InitialUserSetterTest.java
+++ b/tests/carservice_unit_test/src/com/android/car/user/InitialUserSetterTest.java
@@ -91,6 +91,9 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
private UserManager mUm;
@Mock
+ private CarUserService mCarUserService;
+
+ @Mock
private LockPatternUtils mLockPatternUtils;
// Spy used in tests that need to verify the default behavior as fallback
@@ -108,7 +111,7 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
@Before
public void setFixtures() {
- mSetter = spy(new InitialUserSetter(mContext, mUm, mListener,
+ mSetter = spy(new InitialUserSetter(mContext, mUm, mCarUserService, mListener,
mLockPatternUtils, OWNER_NAME, GUEST_NAME));
doReturn(mIActivityManager).when(() -> ActivityManager.getService());
@@ -434,7 +437,7 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
@Test
public void testCreateUser_fail_systemUser() throws Exception {
- // No need to set mUm.createUser() expectation - it shouldn't be called
+ // No need to mock createUser() expectation - it shouldn't be called
mSetter.set(new Builder(InitialUserSetter.TYPE_CREATE)
.setNewUserName("TheDude")
@@ -448,7 +451,7 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
@Test
public void testCreateUser_fail_guestAdmin() throws Exception {
- // No need to set mUm.createUser() expectation - it shouldn't be called
+ // No need to set createUser() expectation - it shouldn't be called
mSetter.set(new Builder(InitialUserSetter.TYPE_CREATE)
.setNewUserName("TheDude")
@@ -461,7 +464,7 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
@Test
public void testCreateUser_fail_ephemeralAdmin() throws Exception {
- // No need to set mUm.createUser() expectation - it shouldn't be called
+ // No need to set createUser() expectation - it shouldn't be called
mSetter.set(new Builder(InitialUserSetter.TYPE_CREATE)
.setNewUserName("TheDude")
@@ -474,7 +477,7 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
@Test
public void testCreateUser_fail_createFail() throws Exception {
- // No need to set mUm.createUser() expectation - it will return false by default
+ // No need to set createUser() expectation - it will return false by default
mSetter.set(new Builder(InitialUserSetter.TYPE_CREATE)
.setNewUserName("TheDude")
@@ -1079,14 +1082,14 @@ public final class InitialUserSetterTest extends AbstractExtendedMockitoTestCase
private UserInfo expectCreateUserOfType(@NonNull String type, @UserIdInt int userId,
@Nullable String name, @UserInfoFlag int flags) {
UserInfo userInfo = new UserInfo(userId, name, flags);
- when(mUm.createUser(name, type, flags)).thenReturn(userInfo);
+ when(mCarUserService.createUserEvenWhenDisallowed(name, type, flags)).thenReturn(userInfo);
// Once user is created, it should exist...
when(mUm.getUserInfo(userId)).thenReturn(userInfo);
return userInfo;
}
private void expectCreateUserThrowsException(@NonNull String name, @UserIdInt int userId) {
- when(mUm.createUser(eq(name), anyString(), eq(userId)))
+ when(mCarUserService.createUserEvenWhenDisallowed(eq(name), anyString(), eq(userId)))
.thenThrow(new RuntimeException("Cannot create user. D'OH!"));
}