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

Commit bc8317ce authored by Ruslan Tkhakokhov's avatar Ruslan Tkhakokhov
Browse files

Update BackupEligibilityRules

1. Remove unused appIgnoresIncludeExcludeRules() and associated tests
2. Remove logic for enforcing full backup for apps with k/v agents and
   associated tests
3. Only ignore allowBackup for non-system apps in D2D and guard the
   behavior by target SDK

Bug: 183147249
Test: atest BackupEligibilityRulesTest
Change-Id: Id8b0862dae492de69c553858e27859970278d7db
parent ef9671de
Loading
Loading
Loading
Loading
+13 −17
Original line number Original line Diff line number Diff line
@@ -71,6 +71,14 @@ public class BackupEligibilityRules {
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S)
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S)
    static final long RESTRICT_ADB_BACKUP = 171032338L;
    static final long RESTRICT_ADB_BACKUP = 171032338L;


    /**
     * When  this change is enabled, {@code android:allowBackup}  is ignored for apps during D2D
     * (device-to-device) migrations.
     */
    @ChangeId
    @EnabledSince(targetSdkVersion = Build.VERSION_CODES.S)
    static final long IGNORE_ALLOW_BACKUP_IN_D2D = 183147249L;

    public static BackupEligibilityRules forBackup(PackageManager packageManager,
    public static BackupEligibilityRules forBackup(PackageManager packageManager,
            PackageManagerInternal packageManagerInternal,
            PackageManagerInternal packageManagerInternal,
            int userId) {
            int userId) {
@@ -148,13 +156,15 @@ public class BackupEligibilityRules {
    * @return boolean indicating whether backup is allowed.
    * @return boolean indicating whether backup is allowed.
    */
    */
    public boolean isAppBackupAllowed(ApplicationInfo app) {
    public boolean isAppBackupAllowed(ApplicationInfo app) {
        boolean isSystemApp = UserHandle.isCore(app.uid);
        boolean allowBackup = (app.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0;
        boolean allowBackup = (app.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0;
        switch (mOperationType) {
        switch (mOperationType) {
            case OperationType.MIGRATION:
            case OperationType.MIGRATION:
                // Backup / restore of all non-system apps is force allowed during
                // Backup / restore of all non-system apps is force allowed during
                // device-to-device migration.
                // device-to-device migration.
                return !isSystemApp || allowBackup;
                boolean isSystemApp = (app.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
                boolean ignoreAllowBackup = !isSystemApp && CompatChanges.isChangeEnabled(
                        IGNORE_ALLOW_BACKUP_IN_D2D, app.packageName, UserHandle.of(mUserId));
                return ignoreAllowBackup || allowBackup;
            case OperationType.ADB_BACKUP:
            case OperationType.ADB_BACKUP:
                String packageName = app.packageName;
                String packageName = app.packageName;
                if (packageName == null) {
                if (packageName == null) {
@@ -176,7 +186,7 @@ public class BackupEligibilityRules {


                boolean isPrivileged = (app.flags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
                boolean isPrivileged = (app.flags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
                boolean isDebuggable = (app.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                boolean isDebuggable = (app.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
                if (isSystemApp || isPrivileged) {
                if (UserHandle.isCore(app.uid) || isPrivileged) {
                    try {
                    try {
                        return mPackageManager.getProperty(PackageManager.PROPERTY_ALLOW_ADB_BACKUP,
                        return mPackageManager.getProperty(PackageManager.PROPERTY_ALLOW_ADB_BACKUP,
                                packageName).getBoolean();
                                packageName).getBoolean();
@@ -283,11 +293,6 @@ public class BackupEligibilityRules {
     */
     */
    @VisibleForTesting
    @VisibleForTesting
    public boolean appGetsFullBackup(PackageInfo pkg) {
    public boolean appGetsFullBackup(PackageInfo pkg) {
        if (forceFullBackup(pkg.applicationInfo.uid, mOperationType)) {
            // If this is a migration, all non-system packages get full backup.
            return true;
        }

        if (pkg.applicationInfo.backupAgentName != null) {
        if (pkg.applicationInfo.backupAgentName != null) {
            // If it has an agent, it gets full backups only if it says so
            // If it has an agent, it gets full backups only if it says so
            return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FULL_BACKUP_ONLY) != 0;
            return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_FULL_BACKUP_ONLY) != 0;
@@ -297,15 +302,6 @@ public class BackupEligibilityRules {
        return true;
        return true;
    }
    }


    public boolean appIgnoresIncludeExcludeRules(ApplicationInfo app) {
        return forceFullBackup(app.uid, mOperationType);
    }

    private boolean forceFullBackup(int appUid, @OperationType int operationType) {
        return operationType == OperationType.MIGRATION &&
                !UserHandle.isCore(appUid);
    }

    /**
    /**
     * Returns whether the app is only capable of doing key/value. We say it's not if it allows full
     * Returns whether the app is only capable of doing key/value. We say it's not if it allows full
     * backup, and it is otherwise.
     * backup, and it is otherwise.
+2 −75
Original line number Original line Diff line number Diff line
@@ -219,6 +219,7 @@ public class BackupEligibilityRulesTest {
    }
    }


    @Test
    @Test
    @EnableCompatChanges({BackupEligibilityRules.IGNORE_ALLOW_BACKUP_IN_D2D})
    public void appIsEligibleForBackup_backupNotAllowedAndInMigration_returnsTrue()
    public void appIsEligibleForBackup_backupNotAllowedAndInMigration_returnsTrue()
            throws Exception {
            throws Exception {
        ApplicationInfo applicationInfo = getApplicationInfo(Process.FIRST_APPLICATION_UID,
        ApplicationInfo applicationInfo = getApplicationInfo(Process.FIRST_APPLICATION_UID,
@@ -235,7 +236,7 @@ public class BackupEligibilityRulesTest {
    public void appIsEligibleForBackup_backupNotAllowedForSystemAppAndInMigration_returnsFalse()
    public void appIsEligibleForBackup_backupNotAllowedForSystemAppAndInMigration_returnsFalse()
            throws Exception {
            throws Exception {
        ApplicationInfo applicationInfo = getApplicationInfo(Process.SYSTEM_UID,
        ApplicationInfo applicationInfo = getApplicationInfo(Process.SYSTEM_UID,
                /* flags */ 0, CUSTOM_BACKUP_AGENT_NAME);
                ApplicationInfo.FLAG_SYSTEM, CUSTOM_BACKUP_AGENT_NAME);
        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
                OperationType.MIGRATION);
        boolean isEligible = eligibilityRules.appIsEligibleForBackup(applicationInfo);
        boolean isEligible = eligibilityRules.appIsEligibleForBackup(applicationInfo);
@@ -477,34 +478,6 @@ public class BackupEligibilityRulesTest {
        assertThat(result).isFalse();
        assertThat(result).isFalse();
    }
    }


    @Test
    public void appGetsFullBackup_withCustomBackupAgentAndWithoutFullBackupOnlyFlagAndInMigration_returnsTrue()
            throws Exception {
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.applicationInfo = getApplicationInfo(Process.FIRST_APPLICATION_UID,
                ~ApplicationInfo.FLAG_FULL_BACKUP_ONLY, CUSTOM_BACKUP_AGENT_NAME);

        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
        boolean result = eligibilityRules.appGetsFullBackup(packageInfo);

        assertThat(result).isTrue();
    }

    @Test
    public void appGetsFullBackup_systemAppWithCustomBackupAgentAndWithoutFullBackupOnlyFlagAndInMigration_returnsFalse()
            throws Exception {
        PackageInfo packageInfo = new PackageInfo();
        packageInfo.applicationInfo = getApplicationInfo(Process.SYSTEM_UID,
                ~ApplicationInfo.FLAG_FULL_BACKUP_ONLY, CUSTOM_BACKUP_AGENT_NAME);

        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
        boolean result = eligibilityRules.appGetsFullBackup(packageInfo);

        assertThat(result).isFalse();
    }

    @Test
    @Test
    public void appIsKeyValueOnly_noCustomBackupAgent_returnsTrue() throws Exception {
    public void appIsKeyValueOnly_noCustomBackupAgent_returnsTrue() throws Exception {
        PackageInfo packageInfo = new PackageInfo();
        PackageInfo packageInfo = new PackageInfo();
@@ -542,52 +515,6 @@ public class BackupEligibilityRulesTest {
        assertThat(result).isTrue();
        assertThat(result).isTrue();
    }
    }


    @Test
    public void appIgnoresIncludeExcludeRules_systemAppAndInMigration_returnsFalse() {
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.uid = Process.SYSTEM_UID;

        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
        boolean result = eligibilityRules.appIgnoresIncludeExcludeRules(applicationInfo);

        assertThat(result).isFalse();
    }

    @Test
    public void appIgnoresIncludeExcludeRules_systemAppInBackup_returnsFalse() {
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.uid = Process.SYSTEM_UID;

        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
        boolean result = eligibilityRules.appIgnoresIncludeExcludeRules(applicationInfo);

        assertThat(result).isFalse();
    }

    @Test
    public void appIgnoresIncludeExcludeRules_nonSystemAppInMigration_returnsTrue() {
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.uid = Process.FIRST_APPLICATION_UID;

        BackupEligibilityRules eligibilityRules = getBackupEligibilityRules(
                OperationType.MIGRATION);
        boolean result = eligibilityRules.appIgnoresIncludeExcludeRules(applicationInfo);

        assertThat(result).isTrue();
    }

    @Test
    public void appIgnoresIncludeExcludeRules_nonSystemInBackup_returnsFalse() {
        ApplicationInfo applicationInfo = new ApplicationInfo();
        applicationInfo.uid = Process.FIRST_APPLICATION_UID;

        boolean result = mBackupEligibilityRules.appIgnoresIncludeExcludeRules(applicationInfo);

        assertThat(result).isFalse();
    }

    @Test
    @Test
    public void signaturesMatch_targetIsNull_returnsFalse() throws Exception {
    public void signaturesMatch_targetIsNull_returnsFalse() throws Exception {
        boolean result = mBackupEligibilityRules.signaturesMatch(new Signature[] {SIGNATURE_1}, null);
        boolean result = mBackupEligibilityRules.signaturesMatch(new Signature[] {SIGNATURE_1}, null);