Merge "Test apexd behaves correctly when not in fs-checkpoint mode" into sc-dev am: b713ec62bb

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15302099

Change-Id: I2edfb53a54d4d784296eb648be8d2cf29b542250
diff --git a/tests/StagedInstallTest/app/src/com/android/tests/stagedinstallinternal/StagedInstallInternalTest.java b/tests/StagedInstallTest/app/src/com/android/tests/stagedinstallinternal/StagedInstallInternalTest.java
index 9cdaef7..738e68e 100644
--- a/tests/StagedInstallTest/app/src/com/android/tests/stagedinstallinternal/StagedInstallInternalTest.java
+++ b/tests/StagedInstallTest/app/src/com/android/tests/stagedinstallinternal/StagedInstallInternalTest.java
@@ -196,6 +196,47 @@
     }
 
     @Test
+    public void testActiveApexIsRevertedOnCheckpointRollback_Prepare() throws Exception {
+        int sessionId = Install.single(TestApp.Apex2).setStaged().commit();
+        assertSessionReady(sessionId);
+        storeSessionId(sessionId);
+    }
+
+    @Test
+    public void testActiveApexIsRevertedOnCheckpointRollback_Commit() throws Exception {
+        // Verify apex installed during preparation was successful
+        int sessionId = retrieveLastSessionId();
+        assertSessionApplied(sessionId);
+        assertThat(InstallUtils.getInstalledVersion(SHIM_APEX_PACKAGE_NAME)).isEqualTo(2);
+        // Commit a new staged session
+        sessionId = Install.single(TestApp.Apex3).setStaged().commit();
+        assertSessionReady(sessionId);
+        storeSessionId(sessionId);
+    }
+
+    @Test
+    public void testActiveApexIsRevertedOnCheckpointRollback_VerifyPostReboot() throws Exception {
+        int sessionId = retrieveLastSessionId();
+        assertSessionFailed(sessionId);
+        assertThat(InstallUtils.getInstalledVersion(SHIM_APEX_PACKAGE_NAME)).isEqualTo(2);
+    }
+
+    @Test
+    public void testApexIsNotActivatedIfNotInCheckpointMode_Commit() throws Exception {
+        int sessionId = Install.single(TestApp.Apex2).setStaged().commit();
+        assertSessionReady(sessionId);
+        storeSessionId(sessionId);
+        assertThat(InstallUtils.getInstalledVersion(SHIM_APEX_PACKAGE_NAME)).isEqualTo(1);
+    }
+
+    @Test
+    public void testApexIsNotActivatedIfNotInCheckpointMode_VerifyPostReboot() throws Exception {
+        int sessionId = retrieveLastSessionId();
+        assertSessionFailed(sessionId);
+        assertThat(InstallUtils.getInstalledVersion(SHIM_APEX_PACKAGE_NAME)).isEqualTo(1);
+    }
+
+    @Test
     public void testRebootlessUpdates() throws Exception {
         InstallUtils.dropShellPermissionIdentity();
         InstallUtils.adoptShellPermissionIdentity(Manifest.permission.INSTALL_PACKAGE_UPDATES);
@@ -257,6 +298,18 @@
         }
     }
 
+    private static void assertSessionApplied(int sessionId) {
+        assertSessionState(sessionId, (session) -> {
+            assertThat(session.isStagedSessionApplied()).isTrue();
+        });
+    }
+
+    private static void assertSessionFailed(int sessionId) {
+        assertSessionState(sessionId, (session) -> {
+            assertThat(session.isStagedSessionFailed()).isTrue();
+        });
+    }
+
     private static void assertSessionFailedWithMessage(int sessionId, String msg) {
         assertSessionState(sessionId, (session) -> {
             assertThat(session.isStagedSessionFailed()).isTrue();
diff --git a/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java b/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java
index e19f97b..3bd3767 100644
--- a/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java
+++ b/tests/StagedInstallTest/src/com/android/tests/stagedinstallinternal/host/StagedInstallInternalTest.java
@@ -346,6 +346,49 @@
     }
 
     @Test
+    public void testActiveApexIsRevertedOnCheckpointRollback() throws Exception {
+        assumeTrue("Device does not support updating APEX",
+                mHostUtils.isApexUpdateSupported());
+        assumeTrue("Device does not support file-system checkpoint",
+                mHostUtils.isCheckpointSupported());
+
+        // Install something so that /data/apex/active is not empty
+        runPhase("testActiveApexIsRevertedOnCheckpointRollback_Prepare");
+        getDevice().reboot();
+
+        // Stage another session which will be installed during fs-rollback mode
+        runPhase("testActiveApexIsRevertedOnCheckpointRollback_Commit");
+
+        // Set checkpoint to 0 so that we enter fs-rollback mode immediately on reboot
+        getDevice().enableAdbRoot();
+        getDevice().executeShellCommand("vdc checkpoint startCheckpoint 0");
+        getDevice().disableAdbRoot();
+        getDevice().reboot();
+
+        // Verify that session was reverted and we have fallen back to
+        // apex installed during preparation stage.
+        runPhase("testActiveApexIsRevertedOnCheckpointRollback_VerifyPostReboot");
+    }
+
+    @Test
+    public void testApexIsNotActivatedIfNotInCheckpointMode() throws Exception {
+        assumeTrue("Device does not support updating APEX",
+                mHostUtils.isApexUpdateSupported());
+        assumeTrue("Device does not support file-system checkpoint",
+                mHostUtils.isCheckpointSupported());
+
+        runPhase("testApexIsNotActivatedIfNotInCheckpointMode_Commit");
+        // Delete checkpoint file in /metadata so that device thinks
+        // fs-checkpointing was never activated
+        getDevice().enableAdbRoot();
+        getDevice().executeShellCommand("rm /metadata/vold/checkpoint");
+        getDevice().disableAdbRoot();
+        getDevice().reboot();
+        // Verify that session was not installed when not in fs-checkpoint mode
+        runPhase("testApexIsNotActivatedIfNotInCheckpointMode_VerifyPostReboot");
+    }
+
+    @Test
     public void testRebootlessUpdates() throws Exception {
         pushTestApex("test.rebootless_apex_v1.apex");
         runPhase("testRebootlessUpdates");