Merge "Fixing RescuePartyTests" into main am: 25d36a4916 am: 56db1fde24

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

Change-Id: I9d20191c01645798508b6f2d35809bc547ed70b3
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/services/core/java/com/android/server/RescueParty.java b/services/core/java/com/android/server/RescueParty.java
index 271d552..37c2d26 100644
--- a/services/core/java/com/android/server/RescueParty.java
+++ b/services/core/java/com/android/server/RescueParty.java
@@ -494,10 +494,10 @@
     private static void executeRescueLevelInternalOld(Context context, int level, @Nullable
             String failedPackage) throws Exception {
 
-        if (level <= LEVEL_RESET_SETTINGS_TRUSTED_DEFAULTS) {
-            // Disabling flag resets on master branch for trunk stable launch.
-            // TODO(b/287618292): Re-enable them after the trunk stable is launched and we
-            // figured out a way to reset flags without interfering with trunk development.
+        // Note: DeviceConfig reset is disabled currently and would be enabled using the flag,
+        // after we have figured out a way to reset flags without interfering with trunk
+        // development. TODO: b/287618292 For enabling flag resets.
+        if (!Flags.allowRescuePartyFlagResets() && level <= LEVEL_RESET_SETTINGS_TRUSTED_DEFAULTS) {
             return;
         }
 
@@ -572,12 +572,16 @@
                 level, levelToString(level));
         switch (level) {
             case RESCUE_LEVEL_SCOPED_DEVICE_CONFIG_RESET:
-                // Temporary disable deviceConfig reset
-                // resetDeviceConfig(context, /*isScoped=*/true, failedPackage);
+                // Enable deviceConfig reset behind flag
+                if (Flags.allowRescuePartyFlagResets()) {
+                    resetDeviceConfig(context, /*isScoped=*/true, failedPackage);
+                }
                 break;
             case RESCUE_LEVEL_ALL_DEVICE_CONFIG_RESET:
-                // Temporary disable deviceConfig reset
-                // resetDeviceConfig(context, /*isScoped=*/false, failedPackage);
+                // Enable deviceConfig reset behind flag
+                if (Flags.allowRescuePartyFlagResets()) {
+                    resetDeviceConfig(context, /*isScoped=*/false, failedPackage);
+                }
                 break;
             case RESCUE_LEVEL_WARM_REBOOT:
                 executeWarmReboot(context, level, failedPackage);
diff --git a/services/tests/mockingservicestests/src/com/android/server/RescuePartyTest.java b/services/tests/mockingservicestests/src/com/android/server/RescuePartyTest.java
index 697548c..4a21645 100644
--- a/services/tests/mockingservicestests/src/com/android/server/RescuePartyTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/RescuePartyTest.java
@@ -25,6 +25,7 @@
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
 import static com.android.dx.mockito.inline.extended.ExtendedMockito.when;
+import static com.android.server.RescueParty.DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN;
 import static com.android.server.RescueParty.LEVEL_FACTORY_RESET;
 import static com.android.server.RescueParty.RESCUE_LEVEL_FACTORY_RESET;
 
@@ -103,8 +104,6 @@
     private static final String PROP_DISABLE_FACTORY_RESET_FLAG =
             "persist.device_config.configuration.disable_rescue_party_factory_reset";
 
-    private static final int THROTTLING_DURATION_MIN = 10;
-
     @Rule
     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
 
@@ -228,6 +227,9 @@
         setCrashRecoveryPropRescueBootCount(0);
         SystemProperties.set(RescueParty.PROP_ENABLE_RESCUE, Boolean.toString(true));
         SystemProperties.set(PROP_DEVICE_CONFIG_DISABLE_FLAG, Boolean.toString(false));
+
+        // enable flag resets for tests
+        mSetFlagsRule.enableFlags(Flags.FLAG_ALLOW_RESCUE_PARTY_FLAG_RESETS);
     }
 
     @After
@@ -312,6 +314,9 @@
 
     @Test
     public void testPersistentAppCrashDetectionWithExecutionForAllRescueLevels() {
+        // this is old test where the flag needs to be disabled
+        mSetFlagsRule.disableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
+
         noteAppCrash(1, true);
 
         verifySettingsResets(Settings.RESET_MODE_UNTRUSTED_DEFAULTS, /*resetNamespaces=*/ null,
@@ -378,6 +383,9 @@
 
     @Test
     public void testNonPersistentAppOnlyPerformsFlagResets() {
+        // this is old test where the flag needs to be disabled
+        mSetFlagsRule.disableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
+
         noteAppCrash(1, false);
 
         verifySettingsResets(Settings.RESET_MODE_UNTRUSTED_DEFAULTS, /*resetNamespaces=*/ null,
@@ -628,7 +636,8 @@
     public void testThrottlingOnBootFailures() {
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN - 1);
+        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN - 1);
         setCrashRecoveryPropLastFactoryReset(beforeTimeout);
         for (int i = 1; i <= LEVEL_FACTORY_RESET; i++) {
             noteBoot(i);
@@ -641,7 +650,8 @@
         mSetFlagsRule.enableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN - 1);
+        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN - 1);
         setCrashRecoveryPropLastFactoryReset(beforeTimeout);
         for (int i = 1; i <= RESCUE_LEVEL_FACTORY_RESET; i++) {
             noteBoot(i);
@@ -653,7 +663,8 @@
     public void testThrottlingOnAppCrash() {
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN - 1);
+        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN - 1);
         setCrashRecoveryPropLastFactoryReset(beforeTimeout);
         for (int i = 0; i <= LEVEL_FACTORY_RESET; i++) {
             noteAppCrash(i + 1, true);
@@ -666,7 +677,8 @@
         mSetFlagsRule.enableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN - 1);
+        long beforeTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN - 1);
         setCrashRecoveryPropLastFactoryReset(beforeTimeout);
         for (int i = 0; i <= RESCUE_LEVEL_FACTORY_RESET; i++) {
             noteAppCrash(i + 1, true);
@@ -678,7 +690,8 @@
     public void testNotThrottlingAfterTimeoutOnBootFailures() {
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long afterTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN + 1);
+        long afterTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN + 1);
         setCrashRecoveryPropLastFactoryReset(afterTimeout);
         for (int i = 1; i <= LEVEL_FACTORY_RESET; i++) {
             noteBoot(i);
@@ -691,7 +704,8 @@
         mSetFlagsRule.enableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long afterTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN + 1);
+        long afterTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN + 1);
         setCrashRecoveryPropLastFactoryReset(afterTimeout);
         for (int i = 1; i <= RESCUE_LEVEL_FACTORY_RESET; i++) {
             noteBoot(i);
@@ -703,7 +717,8 @@
     public void testNotThrottlingAfterTimeoutOnAppCrash() {
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long afterTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN + 1);
+        long afterTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN + 1);
         setCrashRecoveryPropLastFactoryReset(afterTimeout);
         for (int i = 0; i <= LEVEL_FACTORY_RESET; i++) {
             noteAppCrash(i + 1, true);
@@ -716,7 +731,8 @@
         mSetFlagsRule.enableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
         setCrashRecoveryPropAttemptingReboot(false);
         long now = System.currentTimeMillis();
-        long afterTimeout = now - TimeUnit.MINUTES.toMillis(THROTTLING_DURATION_MIN + 1);
+        long afterTimeout = now - TimeUnit.MINUTES.toMillis(
+                DEFAULT_FACTORY_RESET_THROTTLE_DURATION_MIN + 1);
         setCrashRecoveryPropLastFactoryReset(afterTimeout);
         for (int i = 0; i <= RESCUE_LEVEL_FACTORY_RESET; i++) {
             noteAppCrash(i + 1, true);
@@ -794,6 +810,9 @@
 
     @Test
     public void testHealthCheckLevels() {
+        // this is old test where the flag needs to be disabled
+        mSetFlagsRule.disableFlags(Flags.FLAG_RECOVERABILITY_DETECTION);
+
         RescuePartyObserver observer = RescuePartyObserver.getInstance(mMockContext);
 
         // Ensure that no action is taken for cases where the failure reason is unknown