1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.car.admin; 17 18 import static android.car.test.mock.CarMockitoHelper.mockHandleRemoteExceptionFromCarServiceWithDefaultValue; 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.junit.Assert.assertThrows; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.ArgumentMatchers.notNull; 25 import static org.mockito.Mockito.doAnswer; 26 import static org.mockito.Mockito.doThrow; 27 28 import android.annotation.NonNull; 29 import android.annotation.UserIdInt; 30 import android.car.Car; 31 import android.car.test.mocks.AbstractExtendedMockitoTestCase; 32 import android.car.test.util.UserTestingHelper.UserInfoBuilder; 33 import android.car.user.UserCreationResult; 34 import android.car.user.UserRemovalResult; 35 import android.car.user.UserStartResult; 36 import android.car.user.UserStopResult; 37 import android.car.util.concurrent.AndroidFuture; 38 import android.content.pm.UserInfo; 39 import android.os.RemoteException; 40 import android.os.UserHandle; 41 42 import com.android.car.internal.ResultCallbackImpl; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.mockito.Mock; 47 48 public final class CarDevicePolicyManagerUnitTest extends AbstractExtendedMockitoTestCase { 49 50 @Mock 51 private Car mCar; 52 53 @Mock 54 private ICarDevicePolicyService mService; 55 56 private CarDevicePolicyManager mMgr; 57 CarDevicePolicyManagerUnitTest()58 public CarDevicePolicyManagerUnitTest() { 59 super(CarDevicePolicyManager.TAG); 60 } 61 62 @Before setFixtures()63 public void setFixtures() { 64 mMgr = new CarDevicePolicyManager(mCar, mService); 65 } 66 67 @Test testRemoveUser_success()68 public void testRemoveUser_success() throws Exception { 69 mockRemoveUser(/* userId= */ 100); 70 71 RemoveUserResult result = mMgr.removeUser(UserHandle.of(100)); 72 73 assertThat(result.isSuccess()).isTrue(); 74 assertThat(result.getStatus()).isEqualTo(RemoveUserResult.STATUS_SUCCESS); 75 } 76 77 @Test testRemoveUser_remoteException()78 public void testRemoveUser_remoteException() throws Exception { 79 doThrow(new RemoteException("D'OH!")).when(mService).removeUser(eq(100), notNull()); 80 mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar); 81 82 RemoveUserResult result = mMgr.removeUser(UserHandle.of(100)); 83 84 assertThat(result.isSuccess()).isFalse(); 85 assertThat(result.getStatus()).isEqualTo(RemoveUserResult.STATUS_FAILURE_GENERIC); 86 } 87 88 @Test testRemoveUser_securityException()89 public void testRemoveUser_securityException() throws Exception { 90 doThrow(new SecurityException("D'OH!")).when(mService).removeUser(eq(100), notNull()); 91 92 assertThrows(SecurityException.class, () -> mMgr.removeUser(UserHandle.of(100))); 93 } 94 95 @Test testRemoveUser_nullUser()96 public void testRemoveUser_nullUser() { 97 assertThrows(NullPointerException.class, () -> mMgr.removeUser(null)); 98 } 99 100 @Test testCreateUser_success()101 public void testCreateUser_success() throws Exception { 102 UserInfo user = new UserInfoBuilder(100).build(); 103 int status = UserCreationResult.STATUS_SUCCESSFUL; 104 mockCreateUser("TheDude", user, status); 105 106 CreateUserResult result = mMgr.createUser("TheDude", 100); 107 108 assertThat(result.isSuccess()).isTrue(); 109 assertThat(result.getStatus()).isEqualTo(CreateUserResult.STATUS_SUCCESS); 110 assertThat(result.getUserHandle().getIdentifier()).isEqualTo(100); 111 } 112 113 @Test testCreateUser_remoteException()114 public void testCreateUser_remoteException() throws Exception { 115 doThrow(new RemoteException("D'OH!")).when(mService).createUser(eq("TheDude"), eq(100), 116 notNull()); 117 mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar); 118 119 CreateUserResult result = mMgr.createUser("TheDude", 100); 120 121 assertThat(result.isSuccess()).isFalse(); 122 assertThat(result.getStatus()).isEqualTo(CreateUserResult.STATUS_FAILURE_GENERIC); 123 assertThat(result.getUserHandle()).isNull(); 124 } 125 126 @Test testCreateUser_securityException()127 public void testCreateUser_securityException() throws Exception { 128 doThrow(new SecurityException("D'OH!")).when(mService).createUser(eq("TheDude"), eq(100), 129 notNull()); 130 131 assertThrows(SecurityException.class, () -> mMgr.createUser("TheDude", 100)); 132 } 133 134 @Test testStartUserInBackground_success()135 public void testStartUserInBackground_success() throws Exception { 136 mockStartUserInBackground(100, UserStartResult.STATUS_SUCCESSFUL); 137 138 StartUserInBackgroundResult result = mMgr.startUserInBackground(UserHandle.of(100)); 139 140 assertThat(result.isSuccess()).isTrue(); 141 assertThat(result.getStatus()).isEqualTo(StartUserInBackgroundResult.STATUS_SUCCESS); 142 } 143 144 @Test testStartUserInBackground_remoteException()145 public void testStartUserInBackground_remoteException() throws Exception { 146 doThrow(new RemoteException("D'OH!")) 147 .when(mService).startUserInBackground(eq(100), notNull()); 148 mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar); 149 150 StartUserInBackgroundResult result = mMgr.startUserInBackground(UserHandle.of(100)); 151 152 assertThat(result.isSuccess()).isFalse(); 153 assertThat(result.getStatus()) 154 .isEqualTo(StartUserInBackgroundResult.STATUS_FAILURE_GENERIC); 155 } 156 157 @Test testStartUserInBackground_nullUser()158 public void testStartUserInBackground_nullUser() { 159 assertThrows(NullPointerException.class, () -> mMgr.startUserInBackground(null)); 160 } 161 162 @Test testStopUser_success()163 public void testStopUser_success() throws Exception { 164 mockStopUser(100, UserStopResult.STATUS_SUCCESSFUL); 165 166 StopUserResult result = mMgr.stopUser(UserHandle.of(100)); 167 168 assertThat(result.isSuccess()).isTrue(); 169 assertThat(result.getStatus()).isEqualTo(StopUserResult.STATUS_SUCCESS); 170 } 171 172 @Test testStopUser_remoteException()173 public void testStopUser_remoteException() throws Exception { 174 doThrow(new RemoteException("D'OH!")).when(mService).stopUser(eq(100), notNull()); 175 mockHandleRemoteExceptionFromCarServiceWithDefaultValue(mCar); 176 177 StopUserResult result = mMgr.stopUser(UserHandle.of(100)); 178 179 assertThat(result.isSuccess()).isFalse(); 180 assertThat(result.getStatus()).isEqualTo(StopUserResult.STATUS_FAILURE_GENERIC); 181 } 182 183 @Test testStopUser_nullUser()184 public void testStopUser_nullUser() { 185 assertThrows(NullPointerException.class, () -> mMgr.stopUser(null)); 186 } 187 mockRemoveUser(@serIdInt int userId)188 private void mockRemoveUser(@UserIdInt int userId) throws Exception { 189 doAnswer((invocation) -> { 190 @SuppressWarnings("unchecked") 191 ResultCallbackImpl<UserRemovalResult> resultResultCallbackImpl = 192 (ResultCallbackImpl<UserRemovalResult>) invocation.getArguments()[1]; 193 resultResultCallbackImpl.complete( 194 new UserRemovalResult(UserRemovalResult.STATUS_SUCCESSFUL)); 195 return null; 196 }).when(mService).removeUser(eq(userId), notNull()); 197 } 198 mockCreateUser(String name, @NonNull UserInfo user, int status)199 private void mockCreateUser(String name, @NonNull UserInfo user, int status) throws Exception { 200 doAnswer((invocation) -> { 201 @SuppressWarnings("unchecked") 202 ResultCallbackImpl<UserCreationResult> resultCallbackImpl = 203 (ResultCallbackImpl<UserCreationResult>) invocation.getArguments()[2]; 204 resultCallbackImpl.complete(new UserCreationResult(status, user.getUserHandle())); 205 return null; 206 }).when(mService).createUser(eq(name), eq(user.id), notNull()); 207 } 208 mockStartUserInBackground(@serIdInt int userId, int status)209 private void mockStartUserInBackground(@UserIdInt int userId, int status) throws Exception { 210 doAnswer((invocation) -> { 211 @SuppressWarnings("unchecked") 212 AndroidFuture<UserStartResult> future = 213 (AndroidFuture<UserStartResult>) invocation.getArguments()[1]; 214 future.complete(new UserStartResult(status)); 215 return null; 216 }).when(mService).startUserInBackground(eq(userId), notNull()); 217 } 218 mockStopUser(@serIdInt int userId, int status)219 private void mockStopUser(@UserIdInt int userId, int status) throws Exception { 220 doAnswer((invocation) -> { 221 @SuppressWarnings("unchecked") 222 AndroidFuture<UserStopResult> future = 223 (AndroidFuture<UserStopResult>) invocation.getArguments()[1]; 224 future.complete(new UserStopResult(status)); 225 return null; 226 }).when(mService).stopUser(eq(userId), notNull()); 227 } 228 } 229