From 7a2ba1d188fdd49c9225ddf27f20be5310c35c54 Mon Sep 17 00:00:00 2001 From: JW Wang Date: Tue, 6 Oct 2020 14:22:49 +0800 Subject: [PATCH] Tests APK De directory is restored correctly (2/n) This test reveals a bug where the De directory of a staged-installed apk (including the case of apk-in-apex) is not restored correctly. The bug is fixed by aosp/1450376. Bug: 169594054 Test: atest StagedRollbackTest#testRollbackApkDataDirectories_De Change-Id: Ib5179a25446a7ea03f6a4436fe1623f57a013eaf --- .../tests/rollback/StagedRollbackTest.java | 20 +++++++++ .../rollback/host/StagedRollbackTest.java | 41 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/RollbackTest/RollbackTest/src/com/android/tests/rollback/StagedRollbackTest.java b/tests/RollbackTest/RollbackTest/src/com/android/tests/rollback/StagedRollbackTest.java index ab2e4923c88e4..52718bec91487 100644 --- a/tests/RollbackTest/RollbackTest/src/com/android/tests/rollback/StagedRollbackTest.java +++ b/tests/RollbackTest/RollbackTest/src/com/android/tests/rollback/StagedRollbackTest.java @@ -481,6 +481,26 @@ public class StagedRollbackTest { InstallUtils.waitForSessionReady(committed.getCommittedSessionId()); } + @Test + public void testRollbackApkDataDirectories_Phase1() throws Exception { + assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(-1); + Install.single(TestApp.A1).commit(); + assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(1); + } + + @Test + public void testRollbackApkDataDirectories_Phase2() throws Exception { + Install.single(TestApp.A2).setStaged().setEnableRollback().commit(); + } + + @Test + public void testRollbackApkDataDirectories_Phase3() throws Exception { + RollbackInfo available = RollbackUtils.getAvailableRollback(TestApp.A); + RollbackUtils.rollback(available.getRollbackId(), TestApp.A2); + RollbackInfo committed = RollbackUtils.getCommittedRollbackById(available.getRollbackId()); + InstallUtils.waitForSessionReady(committed.getCommittedSessionId()); + } + @Test public void isCheckpointSupported() { Context context = InstrumentationRegistry.getInstrumentation().getContext(); diff --git a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java index c2fd0c39221ee..226d2d5db8200 100644 --- a/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java +++ b/tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java @@ -453,6 +453,43 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { after.forEach(dir -> assertDirectoryIsEmpty(dir)); } + /** + * Tests that data in DE apk data directory is restored when apk is rolled back. + */ + @Test + public void testRollbackApkDataDirectories_De() throws Exception { + // Install version 1 of TESTAPP_A + runPhase("testRollbackApkDataDirectories_Phase1"); + + // Push files to apk data directory + String oldFilePath1 = apkDataDirDe(TESTAPP_A, 0) + "/" + TEST_FILENAME_1; + String oldFilePath2 = apkDataDirDe(TESTAPP_A, 0) + TEST_SUBDIR + TEST_FILENAME_2; + assertTrue(getDevice().pushString(TEST_STRING_1, oldFilePath1)); + assertTrue(getDevice().pushString(TEST_STRING_2, oldFilePath2)); + + // Install version 2 of TESTAPP_A with rollback enabled + runPhase("testRollbackApkDataDirectories_Phase2"); + getDevice().reboot(); + + // Replace files in data directory + getDevice().deleteFile(oldFilePath1); + getDevice().deleteFile(oldFilePath2); + String newFilePath3 = apkDataDirDe(TESTAPP_A, 0) + "/" + TEST_FILENAME_3; + String newFilePath4 = apkDataDirDe(TESTAPP_A, 0) + TEST_SUBDIR + TEST_FILENAME_4; + assertTrue(getDevice().pushString(TEST_STRING_3, newFilePath3)); + assertTrue(getDevice().pushString(TEST_STRING_4, newFilePath4)); + + // Roll back the APK + runPhase("testRollbackApkDataDirectories_Phase3"); + getDevice().reboot(); + + // Verify that old files have been restored and new files are gone + assertEquals(TEST_STRING_1, getDevice().pullFileContents(oldFilePath1)); + assertEquals(TEST_STRING_2, getDevice().pullFileContents(oldFilePath2)); + assertNull(getDevice().pullFile(newFilePath3)); + assertNull(getDevice().pullFile(newFilePath4)); + } + @Test public void testExpireApexRollback() throws Exception { List before = getSnapshotDirectories("/data/misc_ce/0/apexrollback"); @@ -503,6 +540,10 @@ public class StagedRollbackTest extends BaseHostJUnit4Test { return String.format("/data/misc_ce/%d/apexdata/%s", userId, apexName); } + private static String apkDataDirDe(String apexName, int userId) { + return String.format("/data/user_de/%d/%s", userId, apexName); + } + private List getSnapshotDirectories(String baseDir) { try { return getDevice().getFileEntry(baseDir).getChildren(false)