Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68a09e68 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Fix an NPE when a user is added and then removed quickly" into main am: 48fa5320

parents 5775a7ee 48fa5320
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -1960,6 +1960,10 @@ public class Vpn {
    public void onUserAdded(int userId) {
        // If the user is restricted tie them to the parent user's VPN
        UserInfo user = mUserManager.getUserInfo(userId);
        if (user == null) {
            Log.e(TAG, "Can not retrieve UserInfo for userId=" + userId);
            return;
        }
        if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
            synchronized(Vpn.this) {
                final Set<Range<Integer>> existingRanges = mNetworkCapabilities.getUids();
@@ -1989,6 +1993,14 @@ public class Vpn {
    public void onUserRemoved(int userId) {
        // clean up if restricted
        UserInfo user = mUserManager.getUserInfo(userId);
        // TODO: Retrieving UserInfo upon receiving the USER_REMOVED intent is not guaranteed.
        //  This could prevent the removal of associated ranges. To ensure proper range removal,
        //  store the user info when adding ranges. This allows using the user ID in the
        //  USER_REMOVED intent to handle the removal process.
        if (user == null) {
            Log.e(TAG, "Can not retrieve UserInfo for userId=" + userId);
            return;
        }
        if (user.isRestricted() && user.restrictedProfileParentId == mUserId) {
            synchronized(Vpn.this) {
                final Set<Range<Integer>> existingRanges = mNetworkCapabilities.getUids();
+24 −0
Original line number Diff line number Diff line
@@ -917,6 +917,30 @@ public class VpnTest extends VpnTestBase {
        RESTRICTED_PROFILE_A.partial = false;
    }

    @Test
    public void testOnUserAddedAndRemoved_nullUserInfo() throws Exception {
        final Vpn vpn = createVpn(PRIMARY_USER.id);
        final Set<Range<Integer>> initialRange = rangeSet(PRIMARY_USER_RANGE);
        // Note since mVpnProfile is a Ikev2VpnProfile, this starts an IkeV2VpnRunner.
        startLegacyVpn(vpn, mVpnProfile);
        // Set an initial Uid range and mock the network agent
        vpn.mNetworkCapabilities.setUids(initialRange);
        vpn.mNetworkAgent = mMockNetworkAgent;

        // Add the restricted user and then remove it immediately. So the getUserInfo() will return
        // null for the given restricted user id.
        setMockedUsers(PRIMARY_USER, RESTRICTED_PROFILE_A);
        doReturn(null).when(mUserManager).getUserInfo(RESTRICTED_PROFILE_A.id);
        vpn.onUserAdded(RESTRICTED_PROFILE_A.id);
        // Expect no range change to the NetworkCapabilities.
        assertEquals(initialRange, vpn.mNetworkCapabilities.getUids());

        // Remove the restricted user
        vpn.onUserRemoved(RESTRICTED_PROFILE_A.id);
        // Expect no range change to the NetworkCapabilities.
        assertEquals(initialRange, vpn.mNetworkCapabilities.getUids());
    }

    @Test
    public void testPrepare_throwSecurityExceptionWhenGivenPackageDoesNotBelongToTheCaller()
            throws Exception {