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

Commit de7907a5 authored by Richard Uhler's avatar Richard Uhler
Browse files

Add test for multi-package rollback.

The test is marked @Ignore for now because we don't yet properly support
rollback of multi-package installs.

Test: atest RollbackTest (with selinux disabled).
Test: atest RollbackTest fails without the @Ignore.
Bug: 112431924

Change-Id: Ia76d48756bc1078344dcf1f2c5696ab79eb0c40c
parent 04e84e0e
Loading
Loading
Loading
Loading
+53 −0
Original line number Diff line number Diff line
@@ -485,4 +485,57 @@ public class RollbackTest {
            // Expected.
        }
    }

    /**
     * Test rollback of multi-package installs.
     * TODO: Stop ignoring this test once support for multi-package rollback
     * is implemented.
     */
    @Ignore @Test
    public void testMultiPackage() throws Exception {
        try {
            RollbackTestUtils.adoptShellPermissionIdentity(
                    Manifest.permission.INSTALL_PACKAGES,
                    Manifest.permission.DELETE_PACKAGES,
                    Manifest.permission.MANAGE_ROLLBACKS);
            RollbackManager rm = RollbackTestUtils.getRollbackManager();

            // Prep installation of the test apps.
            RollbackTestUtils.uninstall(TEST_APP_A);
            RollbackTestUtils.uninstall(TEST_APP_B);
            RollbackTestUtils.installMultiPackage(false,
                    "RollbackTestAppAv1.apk",
                    "RollbackTestAppBv1.apk");
            RollbackTestUtils.installMultiPackage(true,
                    "RollbackTestAppAv2.apk",
                    "RollbackTestAppBv2.apk");
            assertEquals(2, RollbackTestUtils.getInstalledVersion(TEST_APP_A));
            assertEquals(2, RollbackTestUtils.getInstalledVersion(TEST_APP_B));

            // TEST_APP_A should now be available for rollback.
            assertTrue(rm.getPackagesWithAvailableRollbacks().contains(TEST_APP_A));
            RollbackInfo rollback = rm.getAvailableRollback(TEST_APP_A);
            assertNotNull(rollback);

            // TODO: Test the dependent apps for rollback are correct once we
            // support that in the RollbackInfo API.

            // Rollback the app. It should cause both test apps to be rolled
            // back.
            RollbackTestUtils.rollback(rollback);
            assertEquals(1, RollbackTestUtils.getInstalledVersion(TEST_APP_A));
            assertEquals(1, RollbackTestUtils.getInstalledVersion(TEST_APP_B));

            // We should not see a recent rollback listed for TEST_APP_B
            for (RollbackInfo r : rm.getRecentlyExecutedRollbacks()) {
                assertNotEquals(TEST_APP_B, r.targetPackage.packageName);
            }

            // TODO: Test the listed dependent apps for the recently executed
            // rollback are correct once we support that in the RollbackInfo
            // API.
        } finally {
            RollbackTestUtils.dropShellPermissionIdentity();
        }
    }
}
+51 −0
Original line number Diff line number Diff line
@@ -135,6 +135,57 @@ class RollbackTestUtils {
        assertStatusSuccess(LocalIntentSender.getIntentSenderResult());
    }

    /**
     * Installs the apks with the given resource names as an atomic set.
     *
     * @param enableRollback if rollback should be enabled.
     * @param resourceNames names of the class loader resource for the apks to
     *        install.
     * @throws AssertionError if the installation fails.
     */
    static void installMultiPackage(boolean enableRollback, String... resourceNames)
            throws InterruptedException, IOException {
        Context context = InstrumentationRegistry.getContext();
        PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();

        PackageInstaller.SessionParams multiPackageParams = new PackageInstaller.SessionParams(
                PackageInstaller.SessionParams.MODE_FULL_INSTALL);
        multiPackageParams.setMultiPackage();
        if (enableRollback) {
            // TODO: Do we set this on the parent params, the child params, or
            // both?
            multiPackageParams.setEnableRollback();
        }
        int multiPackageId = packageInstaller.createSession(multiPackageParams);
        PackageInstaller.Session multiPackage = packageInstaller.openSession(multiPackageId);

        for (String resourceName : resourceNames) {
            PackageInstaller.Session session = null;
            PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                    PackageInstaller.SessionParams.MODE_FULL_INSTALL);
            if (enableRollback) {
                params.setEnableRollback();
            }
            int sessionId = packageInstaller.createSession(params);
            session = packageInstaller.openSession(sessionId);

            ClassLoader loader = RollbackTest.class.getClassLoader();
            try (OutputStream packageInSession = session.openWrite("package", 0, -1);
                 InputStream is = loader.getResourceAsStream(resourceName);) {
                byte[] buffer = new byte[4096];
                int n;
                while ((n = is.read(buffer)) >= 0) {
                    packageInSession.write(buffer, 0, n);
                }
            }
            multiPackage.addChildSessionId(sessionId);
        }

        // Commit the session (this will start the installation workflow).
        multiPackage.commit(LocalIntentSender.getIntentSender());
        assertStatusSuccess(LocalIntentSender.getIntentSenderResult());
    }

    static void adoptShellPermissionIdentity(String... permissions) {
        InstrumentationRegistry
            .getInstrumentation()