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

Commit ed71fb46 authored by Joël Stemmer's avatar Joël Stemmer Committed by Android (Google) Code Review
Browse files

Merge "Avoid entering an infinite loop in PackageManagerBackupAgent#onRestore" into main

parents b081c321 1a67da88
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -781,6 +781,9 @@ public class PackageManagerBackupAgent extends BackupAgent {
                                "Not restoring package "
                                        + key
                                        + " since it appears to have no signatures.");
                        if (!data.readNextHeader()) {
                            break;
                        }
                        continue;
                    }

@@ -790,18 +793,15 @@ public class PackageManagerBackupAgent extends BackupAgent {
                    sigMap.put(key, new Metadata(versionCode, sigs));
                }

                boolean readNextHeader = data.readNextHeader();
                if (!readNextHeader) {
                    if (DEBUG) {
                        Slog.v(
                                TAG,
                                "LegacyRestoreDataConsumer:"
                                        + " we're done reading all the headers");
                    }
                if (!data.readNextHeader()) {
                    break;
                }
            }

            if (DEBUG) {
                Slog.v(TAG, "LegacyRestoreDataConsumer:" + " we're done reading all the headers");
            }

            // On successful completion, cache the signature map for the Backup Manager to use
            mRestoredSignatures = sigMap;
        }
+24 −0
Original line number Diff line number Diff line
@@ -211,6 +211,30 @@ public class PackageManagerBackupAgentTest {
        assertThat(mNewState.length()).isEqualTo(0);
    }

    @Test
    public void onRestore_legacyBackupWithMissingSignature_restoresBackup() throws Exception {
        PackageInfo pkgWithoutSigs = createPackage("pkg.no.sigs", 1);
        pkgWithoutSigs.signingInfo =
                new SigningInfo(new SigningDetails(new Signature[0], 1, null, null));
        when(mPackageManager.getPackageInfoAsUser(
                        eq(pkgWithoutSigs.packageName), anyInt(), anyInt()))
                .thenReturn(pkgWithoutSigs);
        ImmutableList<PackageInfo> packages =
                ImmutableList.<PackageInfo>builder().addAll(mPackages).add(pkgWithoutSigs).build();
        mPackageManagerBackupAgent =
                new PackageManagerBackupAgent(mPackageManager, packages, USER_ID);
        // A legacy backup is one without an ancestral record version. Ancestral record versions
        // are always written however, so we'll need to delete it from the backup data before
        // restoring.
        runBackupAgentOnBackup();
        deleteKeyFromBackupData(mBackupData, PackageManagerBackupAgent.ANCESTRAL_RECORD_KEY);

        runBackupAgentOnRestore(); // should not fail or timeout

        assertThat(mPackageManagerBackupAgent.getRestoredPackages())
                .containsExactly(EXISTING_PACKAGE_NAME);
    }

    private void runBackupAgentOnBackup() throws Exception {
        try (ParcelFileDescriptor oldStateDescriptor = openForReading(mOldState);
                ParcelFileDescriptor backupDataDescriptor = openForWriting(mBackupData);