Merge "Update VpnTest for new restricted API" am: 2954e8af0f am: 69dbb598f9
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1554177 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: I76d0e305bbace9c41a1ca44bad660391bbe79297
This commit is contained in:
@@ -203,6 +203,7 @@ public class Vpn {
|
|||||||
protected final NetworkCapabilities mNetworkCapabilities;
|
protected final NetworkCapabilities mNetworkCapabilities;
|
||||||
private final SystemServices mSystemServices;
|
private final SystemServices mSystemServices;
|
||||||
private final Ikev2SessionCreator mIkev2SessionCreator;
|
private final Ikev2SessionCreator mIkev2SessionCreator;
|
||||||
|
private final UserManager mUserManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether to keep the connection active after rebooting, or upgrading or reinstalling. This
|
* Whether to keep the connection active after rebooting, or upgrading or reinstalling. This
|
||||||
@@ -409,6 +410,7 @@ public class Vpn {
|
|||||||
mLooper = looper;
|
mLooper = looper;
|
||||||
mSystemServices = systemServices;
|
mSystemServices = systemServices;
|
||||||
mIkev2SessionCreator = ikev2SessionCreator;
|
mIkev2SessionCreator = ikev2SessionCreator;
|
||||||
|
mUserManager = mContext.getSystemService(UserManager.class);
|
||||||
|
|
||||||
mPackage = VpnConfig.LEGACY_VPN;
|
mPackage = VpnConfig.LEGACY_VPN;
|
||||||
mOwnerUID = getAppUid(mPackage, mUserId);
|
mOwnerUID = getAppUid(mPackage, mUserId);
|
||||||
@@ -1435,7 +1437,7 @@ public class Vpn {
|
|||||||
final long token = Binder.clearCallingIdentity();
|
final long token = Binder.clearCallingIdentity();
|
||||||
List<UserInfo> users;
|
List<UserInfo> users;
|
||||||
try {
|
try {
|
||||||
users = UserManager.get(mContext).getAliveUsers();
|
users = mUserManager.getAliveUsers();
|
||||||
} finally {
|
} finally {
|
||||||
Binder.restoreCallingIdentity(token);
|
Binder.restoreCallingIdentity(token);
|
||||||
}
|
}
|
||||||
@@ -1519,7 +1521,7 @@ public class Vpn {
|
|||||||
*/
|
*/
|
||||||
public void onUserAdded(int userId) {
|
public void onUserAdded(int userId) {
|
||||||
// If the user is restricted tie them to the parent user's VPN
|
// If the user is restricted tie them to the parent user's VPN
|
||||||
UserInfo user = UserManager.get(mContext).getUserInfo(userId);
|
UserInfo user = mUserManager.getUserInfo(userId);
|
||||||
if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
|
if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
|
||||||
synchronized(Vpn.this) {
|
synchronized(Vpn.this) {
|
||||||
final Set<UidRange> existingRanges = mNetworkCapabilities.getUids();
|
final Set<UidRange> existingRanges = mNetworkCapabilities.getUids();
|
||||||
@@ -1547,7 +1549,7 @@ public class Vpn {
|
|||||||
*/
|
*/
|
||||||
public void onUserRemoved(int userId) {
|
public void onUserRemoved(int userId) {
|
||||||
// clean up if restricted
|
// clean up if restricted
|
||||||
UserInfo user = UserManager.get(mContext).getUserInfo(userId);
|
UserInfo user = mUserManager.getUserInfo(userId);
|
||||||
if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
|
if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
|
||||||
synchronized(Vpn.this) {
|
synchronized(Vpn.this) {
|
||||||
final Set<UidRange> existingRanges = mNetworkCapabilities.getUids();
|
final Set<UidRange> existingRanges = mNetworkCapabilities.getUids();
|
||||||
@@ -1972,8 +1974,7 @@ public class Vpn {
|
|||||||
|
|
||||||
private void enforceNotRestrictedUser() {
|
private void enforceNotRestrictedUser() {
|
||||||
Binder.withCleanCallingIdentity(() -> {
|
Binder.withCleanCallingIdentity(() -> {
|
||||||
final UserManager mgr = UserManager.get(mContext);
|
final UserInfo user = mUserManager.getUserInfo(mUserId);
|
||||||
final UserInfo user = mgr.getUserInfo(mUserId);
|
|
||||||
|
|
||||||
if (user.isRestricted()) {
|
if (user.isRestricted()) {
|
||||||
throw new SecurityException("Restricted users cannot configure VPNs");
|
throw new SecurityException("Restricted users cannot configure VPNs");
|
||||||
@@ -2008,9 +2009,8 @@ public class Vpn {
|
|||||||
*/
|
*/
|
||||||
public void startLegacyVpnPrivileged(VpnProfile profile, KeyStore keyStore,
|
public void startLegacyVpnPrivileged(VpnProfile profile, KeyStore keyStore,
|
||||||
@Nullable Network underlying, @NonNull LinkProperties egress) {
|
@Nullable Network underlying, @NonNull LinkProperties egress) {
|
||||||
UserManager mgr = UserManager.get(mContext);
|
UserInfo user = mUserManager.getUserInfo(mUserId);
|
||||||
UserInfo user = mgr.getUserInfo(mUserId);
|
if (user.isRestricted() || mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN,
|
||||||
if (user.isRestricted() || mgr.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN,
|
|
||||||
new UserHandle(mUserId))) {
|
new UserHandle(mUserId))) {
|
||||||
throw new SecurityException("Restricted users cannot establish VPNs");
|
throw new SecurityException("Restricted users cannot establish VPNs");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -257,12 +257,14 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRestrictedProfilesAreAddedToVpn() {
|
public void testRestrictedProfilesAreAddedToVpn() {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB);
|
setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB);
|
||||||
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
|
|
||||||
null, null);
|
// Assume the user can have restricted profiles.
|
||||||
|
doReturn(true).when(mUserManager).canHaveRestrictedProfile();
|
||||||
|
final Set<UidRange> ranges =
|
||||||
|
vpn.createUserAndRestrictedProfilesRanges(primaryUser.id, null, null);
|
||||||
|
|
||||||
assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
|
assertEquals(new ArraySet<>(Arrays.asList(new UidRange[] {
|
||||||
PRI_USER_RANGE, UidRange.createForUser(restrictedProfileA.id)
|
PRI_USER_RANGE, UidRange.createForUser(restrictedProfileA.id)
|
||||||
@@ -271,7 +273,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testManagedProfilesAreNotAddedToVpn() {
|
public void testManagedProfilesAreNotAddedToVpn() {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
setMockedUsers(primaryUser, managedProfileA);
|
setMockedUsers(primaryUser, managedProfileA);
|
||||||
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
@@ -294,7 +295,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUidAllowAndDenylist() throws Exception {
|
public void testUidAllowAndDenylist() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRange user = PRI_USER_RANGE;
|
final UidRange user = PRI_USER_RANGE;
|
||||||
final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
|
final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
|
||||||
@@ -320,7 +320,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAlwaysAndOnGetLockDown() throws Exception {
|
public void testGetAlwaysAndOnGetLockDown() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
|
|
||||||
// Default state.
|
// Default state.
|
||||||
@@ -345,7 +344,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLockdownChangingPackage() throws Exception {
|
public void testLockdownChangingPackage() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRange user = PRI_USER_RANGE;
|
final UidRange user = PRI_USER_RANGE;
|
||||||
|
|
||||||
@@ -373,7 +371,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLockdownAllowlist() throws Exception {
|
public void testLockdownAllowlist() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRange user = PRI_USER_RANGE;
|
final UidRange user = PRI_USER_RANGE;
|
||||||
|
|
||||||
@@ -448,7 +445,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLockdownRuleRepeatability() throws Exception {
|
public void testLockdownRuleRepeatability() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRangeParcel[] primaryUserRangeParcel = new UidRangeParcel[] {
|
final UidRangeParcel[] primaryUserRangeParcel = new UidRangeParcel[] {
|
||||||
new UidRangeParcel(PRI_USER_RANGE.start, PRI_USER_RANGE.stop)};
|
new UidRangeParcel(PRI_USER_RANGE.start, PRI_USER_RANGE.stop)};
|
||||||
@@ -481,7 +477,6 @@ public class VpnTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLockdownRuleReversibility() throws Exception {
|
public void testLockdownRuleReversibility() throws Exception {
|
||||||
if (true) return; // TODO(b/175883995): Test disabled until updated for new UserManager API.
|
|
||||||
final Vpn vpn = createVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRangeParcel[] entireUser = {
|
final UidRangeParcel[] entireUser = {
|
||||||
new UidRangeParcel(PRI_USER_RANGE.start, PRI_USER_RANGE.stop)
|
new UidRangeParcel(PRI_USER_RANGE.start, PRI_USER_RANGE.stop)
|
||||||
@@ -1164,6 +1159,10 @@ public class VpnTest {
|
|||||||
doReturn(UserHandle.of(userId)).when(asUserContext).getUser();
|
doReturn(UserHandle.of(userId)).when(asUserContext).getUser();
|
||||||
when(mContext.createContextAsUser(eq(UserHandle.of(userId)), anyInt()))
|
when(mContext.createContextAsUser(eq(UserHandle.of(userId)), anyInt()))
|
||||||
.thenReturn(asUserContext);
|
.thenReturn(asUserContext);
|
||||||
|
when(asUserContext.getSystemServiceName(UserManager.class))
|
||||||
|
.thenReturn(Context.USER_SERVICE);
|
||||||
|
when(asUserContext.getSystemService(UserManager.class))
|
||||||
|
.thenReturn(mUserManager);
|
||||||
final TestLooper testLooper = new TestLooper();
|
final TestLooper testLooper = new TestLooper();
|
||||||
final Vpn vpn = new Vpn(testLooper.getLooper(), mContext, new TestDeps(), mNetService,
|
final Vpn vpn = new Vpn(testLooper.getLooper(), mContext, new TestDeps(), mNetService,
|
||||||
mNetd, userId, mKeyStore, mSystemServices, mIkev2SessionCreator);
|
mNetd, userId, mKeyStore, mSystemServices, mIkev2SessionCreator);
|
||||||
@@ -1199,11 +1198,6 @@ public class VpnTest {
|
|||||||
final int id = (int) invocation.getArguments()[0];
|
final int id = (int) invocation.getArguments()[0];
|
||||||
return userMap.get(id);
|
return userMap.get(id);
|
||||||
}).when(mUserManager).getUserInfo(anyInt());
|
}).when(mUserManager).getUserInfo(anyInt());
|
||||||
|
|
||||||
doAnswer(invocation -> {
|
|
||||||
final int id = (int) invocation.getArguments()[0];
|
|
||||||
return (userMap.get(id).flags & UserInfo.FLAG_ADMIN) != 0;
|
|
||||||
}).when(mUserManager).canHaveRestrictedProfile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user