Skip to content

Commit ee8cc6f

Browse files
JW Wangandroid-build-team Robot
authored andcommitted
Backport of ag/14170751 and ag/14170752.
1. An available rollback shouldn't be deleted when its session expires. 2. Add a test to check we don't crash or delete a rollback when its session expires. Bug: 181087240 Bug: 185132440 Test: atest StagedRollbackTest Merged-In: I3c2b718905d2d7619d6f299ee5402fd858de030e Merged-In: I1bd786b38e2df1f4bdc80d0d371d5d2d52e21e06 Change-Id: Id55f1bde0ee69a6f26bc8e109918fb26d1da4e3f (cherry picked from commit 29d5f72db216eeae13cb7d395b2359544012ffce)
1 parent 2e4efe7 commit ee8cc6f

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

services/core/java/com/android/server/rollback/RollbackManagerServiceImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,10 @@ void onBootCompleted() {
554554
PackageInstaller.SessionInfo session = mContext.getPackageManager()
555555
.getPackageInstaller().getSessionInfo(rollback.getStagedSessionId());
556556
if (session == null || session.isStagedSessionFailed()) {
557-
iter.remove();
558-
rollback.delete(mAppDataRollbackHelper);
557+
if (rollback.isEnabling()) {
558+
iter.remove();
559+
rollback.delete(mAppDataRollbackHelper);
560+
}
559561
continue;
560562
}
561563

tests/RollbackTest/RollbackTest/src/com/android/tests/rollback/StagedRollbackTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,33 @@ public void isCheckpointSupported() {
481481
assertThat(sm.isCheckpointSupported()).isTrue();
482482
}
483483

484+
@Test
485+
public void testExpireSession_Phase1_Install() throws Exception {
486+
assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(-1);
487+
Install.single(TestApp.A1).commit();
488+
assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(1);
489+
Install.single(TestApp.A2).setEnableRollback().setStaged().commit();
490+
}
491+
492+
@Test
493+
public void testExpireSession_Phase2_VerifyInstall() throws Exception {
494+
assertThat(InstallUtils.getInstalledVersion(TestApp.A)).isEqualTo(2);
495+
RollbackManager rm = RollbackUtils.getRollbackManager();
496+
RollbackInfo rollback = getUniqueRollbackInfoForPackage(
497+
rm.getAvailableRollbacks(), TestApp.A);
498+
assertThat(rollback).isNotNull();
499+
assertThat(rollback).packagesContainsExactly(Rollback.from(TestApp.A2).to(TestApp.A1));
500+
assertThat(rollback.isStaged()).isTrue();
501+
}
502+
503+
@Test
504+
public void testExpireSession_Phase3_VerifyRollback() throws Exception {
505+
RollbackManager rm = RollbackUtils.getRollbackManager();
506+
RollbackInfo rollback = getUniqueRollbackInfoForPackage(
507+
rm.getAvailableRollbacks(), TestApp.A);
508+
assertThat(rollback).isNotNull();
509+
}
510+
484511
@Test
485512
public void hasMainlineModule() throws Exception {
486513
String pkgName = getModuleMetadataPackageName();

tests/RollbackTest/StagedRollbackTest/src/com/android/tests/rollback/host/StagedRollbackTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import org.junit.runner.RunWith;
3939

4040
import java.io.File;
41+
import java.time.Instant;
4142
import java.util.Collections;
43+
import java.util.Date;
4244
import java.util.List;
4345
import java.util.concurrent.TimeUnit;
4446
import java.util.stream.Collectors;
@@ -441,6 +443,27 @@ public void testRollbackApexDataDirectories_Ce() throws Exception {
441443
after.forEach(dir -> assertDirectoryIsEmpty(dir));
442444
}
443445

446+
/**
447+
* Tests an available rollback shouldn't be deleted when its session expires.
448+
*/
449+
@Test
450+
public void testExpireSession() throws Exception {
451+
runPhase("testExpireSession_Phase1_Install");
452+
getDevice().reboot();
453+
runPhase("testExpireSession_Phase2_VerifyInstall");
454+
455+
// Advance system clock by 7 days to expire the staged session
456+
Instant t1 = Instant.ofEpochMilli(getDevice().getDeviceDate());
457+
Instant t2 = t1.plusMillis(TimeUnit.DAYS.toMillis(7));
458+
runAsRoot(() -> getDevice().setDate(Date.from(t2)));
459+
460+
// Somehow we need to wait for a while before reboot. Otherwise the change to the
461+
// system clock will be reset after reboot.
462+
Thread.sleep(3000);
463+
getDevice().reboot();
464+
runPhase("testExpireSession_Phase3_VerifyRollback");
465+
}
466+
444467
private void pushTestApex() throws Exception {
445468
CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild());
446469
final String fileName = APK_IN_APEX_TESTAPEX_NAME + "_v1.apex";
@@ -521,4 +544,18 @@ private boolean hasMainlineModule() throws Exception {
521544
return false;
522545
}
523546
}
547+
548+
@FunctionalInterface
549+
private interface ExceptionalRunnable {
550+
void run() throws Exception;
551+
}
552+
553+
private void runAsRoot(ExceptionalRunnable runnable) throws Exception {
554+
try {
555+
getDevice().enableAdbRoot();
556+
runnable.run();
557+
} finally {
558+
getDevice().disableAdbRoot();
559+
}
560+
}
524561
}

0 commit comments

Comments
 (0)