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

Commit d5c28dd7 authored by Paul Hu's avatar Paul Hu Committed by Gerrit Code Review
Browse files

Merge "Add check to prevent resetting VPN always-on setting in safe mode" into main

parents ac2f2dc2 cabbb7da
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -1113,7 +1113,11 @@ public class Vpn {
            }
            // Remove always-on VPN if it's not supported.
            if (!isAlwaysOnPackageSupported(alwaysOnPackage)) {
                // Do not remove the always-on setting due to the restricted ability in safe mode.
                // The always-on VPN can then start after the device reboots to normal mode.
                if (!mContext.getPackageManager().isSafeMode()) {
                    setAlwaysOnPackage(null, false, null);
                }
                return false;
            }
            // Skip if the service is already established. This isn't bulletproof: it's not bound
+26 −0
Original line number Diff line number Diff line
@@ -3186,6 +3186,32 @@ public class VpnTest extends VpnTestBase {
        assertEquals(profile, ikev2VpnProfile.toVpnProfile());
    }

    @Test
    public void testStartAlwaysOnVpnOnSafeMode() throws Exception {
        final Vpn vpn = createVpn(PRIMARY_USER.id);
        setMockedUsers(PRIMARY_USER);

        // UID checks must return a different UID; otherwise it'll be treated as already prepared.
        final int uid = Process.myUid() + 1;
        when(mPackageManager.getPackageUidAsUser(eq(TEST_VPN_PKG), anyInt()))
                .thenReturn(uid);
        when(mVpnProfileStore.get(vpn.getProfileNameForPackage(TEST_VPN_PKG)))
                .thenReturn(mVpnProfile.encode());

        setAndVerifyAlwaysOnPackage(vpn, uid, false);
        assertTrue(vpn.startAlwaysOnVpn());
        assertEquals(TEST_VPN_PKG, vpn.getAlwaysOnPackage());

        // Simulate safe mode and restart the always-on VPN to verify the always-on package is not
        // reset.
        doReturn(null).when(mVpnProfileStore).get(vpn.getProfileNameForPackage(TEST_VPN_PKG));
        doReturn(null).when(mPackageManager).queryIntentServicesAsUser(
                any(), any(), eq(PRIMARY_USER.id));
        doReturn(true).when(mPackageManager).isSafeMode();
        assertFalse(vpn.startAlwaysOnVpn());
        assertEquals(TEST_VPN_PKG, vpn.getAlwaysOnPackage());
    }

    // Make it public and un-final so as to spy it
    public class TestDeps extends Vpn.Dependencies {
        TestDeps() {}