Check current user owns a frp credential
In Android U+ tablet device, Android introduces headless mode which running on a main user instead of system user. Therefore, device throw a security error in HSUM build now. For now, we check whether current user owns a frp credential instead. This way also works on non HSUM build. Test: robo test + run FRP mode in Suw flow. Fix: 262438904 Change-Id: Ie4c7c470b13b9de8d532e61e9984521cebe7adff
This commit is contained in:
@@ -18,6 +18,7 @@ package com.android.settings;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
@@ -59,6 +60,9 @@ import android.widget.TextView;
|
||||
import androidx.core.graphics.drawable.IconCompat;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowLockPatternUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -67,6 +71,7 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowBinder;
|
||||
|
||||
import java.net.InetAddress;
|
||||
@@ -74,6 +79,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = ShadowLockPatternUtils.class)
|
||||
public class UtilsTest {
|
||||
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
@@ -88,7 +94,7 @@ public class UtilsTest {
|
||||
@Mock
|
||||
private DevicePolicyManager mDevicePolicyManager;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
private UserManager mMockUserManager;
|
||||
@Mock
|
||||
private PackageManager mPackageManager;
|
||||
@Mock
|
||||
@@ -96,18 +102,27 @@ public class UtilsTest {
|
||||
@Mock
|
||||
private ApplicationInfo mApplicationInfo;
|
||||
private Context mContext;
|
||||
private UserManager mUserManager;
|
||||
private static final int FLAG_SYSTEM = 0x00000000;
|
||||
private static final int FLAG_MAIN = 0x00004000;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
|
||||
when(mContext.getSystemService(WifiManager.class)).thenReturn(wifiManager);
|
||||
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(connectivityManager);
|
||||
when(mContext.getPackageManager()).thenReturn(mPackageManager);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ShadowLockPatternUtils.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWifiIpAddresses_succeeds() throws Exception {
|
||||
when(wifiManager.getCurrentNetwork()).thenReturn(network);
|
||||
@@ -173,8 +188,9 @@ public class UtilsTest {
|
||||
public void isProfileOrDeviceOwner_deviceOwnerApp_returnTrue() {
|
||||
when(mDevicePolicyManager.isDeviceOwnerAppOnAnyUser(PACKAGE_NAME)).thenReturn(true);
|
||||
|
||||
assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
|
||||
.isTrue();
|
||||
assertThat(
|
||||
Utils.isProfileOrDeviceOwner(mMockUserManager, mDevicePolicyManager, PACKAGE_NAME))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,12 +198,13 @@ public class UtilsTest {
|
||||
final List<UserInfo> userInfos = new ArrayList<>();
|
||||
userInfos.add(new UserInfo());
|
||||
|
||||
when(mUserManager.getUsers()).thenReturn(userInfos);
|
||||
when(mMockUserManager.getUsers()).thenReturn(userInfos);
|
||||
when(mDevicePolicyManager.getProfileOwnerAsUser(userInfos.get(0).id))
|
||||
.thenReturn(new ComponentName(PACKAGE_NAME, ""));
|
||||
|
||||
assertThat(Utils.isProfileOrDeviceOwner(mUserManager, mDevicePolicyManager, PACKAGE_NAME))
|
||||
.isTrue();
|
||||
assertThat(
|
||||
Utils.isProfileOrDeviceOwner(mMockUserManager, mDevicePolicyManager, PACKAGE_NAME))
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -339,4 +356,20 @@ public class UtilsTest {
|
||||
|
||||
assertThat(Utils.canCurrentUserDream(mockContext)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUserOwnsFrpCredential_userOwnsFrpCredential_returnUserId() {
|
||||
ShadowLockPatternUtils.setUserOwnsFrpCredential(true);
|
||||
|
||||
assertThat(Utils.checkUserOwnsFrpCredential(mContext, 123)).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkUserOwnsFrpCredential_userNotOwnsFrpCredential_returnUserId() {
|
||||
ShadowLockPatternUtils.setUserOwnsFrpCredential(false);
|
||||
|
||||
assertThrows(
|
||||
SecurityException.class,
|
||||
() -> Utils.checkUserOwnsFrpCredential(mContext, 123));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user