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

Commit f054f76d authored by Mark Punzalan's avatar Mark Punzalan
Browse files

Fix OverlayRemountedTest for HSUM

Devices with multiple users (such as HSUM) cause the output of `cmd
overlay dump isenabled` to have multiple lines, one for each user. The
tests fail trying to parse the output. The fix here is to enable the
overlay and lookup the resource value only for the current user.

Also removed some unused code in SystemPreparer.

Fixes: 396551034
Fixes: 406547979
Fixes: 414526588
Test: atest OverlayRemountedTest on HSUM device
Flag: EXEMPT bugfix

Change-Id: I8ff1b8e0884b5a7aaf8b4bed05fcc1e311c84bba
parent 95d0bed6
Loading
Loading
Loading
Loading
+7 −2
Original line number Original line Diff line number Diff line
@@ -38,12 +38,15 @@ public class OverlayRemountedTestBase extends BaseHostJUnit4Test {
    protected final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder,
    protected final SystemPreparer mPreparer = new SystemPreparer(mTemporaryFolder,
            this::getDevice);
            this::getDevice);


    private int mCurrentUserId;

    @Rule
    @Rule
    public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer);
    public final RuleChain ruleChain = RuleChain.outerRule(mTemporaryFolder).around(mPreparer);


    @Before
    @Before
    public void startBefore() throws DeviceNotAvailableException {
    public void startBefore() throws DeviceNotAvailableException {
        getDevice().waitForDeviceAvailable();
        getDevice().waitForDeviceAvailable();
        mCurrentUserId = getDevice().getCurrentUser();
    }
    }


    /** Builds the full name of a resource in the form package:type/entry. */
    /** Builds the full name of a resource in the form package:type/entry. */
@@ -58,7 +61,8 @@ public class OverlayRemountedTestBase extends BaseHostJUnit4Test {
        final long endMillis = System.currentTimeMillis() + ASSERT_RESOURCE_TIMEOUT_MS;
        final long endMillis = System.currentTimeMillis() + ASSERT_RESOURCE_TIMEOUT_MS;
        while (System.currentTimeMillis() <= endMillis) {
        while (System.currentTimeMillis() <= endMillis) {
            result = getDevice().executeShellCommand(
            result = getDevice().executeShellCommand(
                    String.format("cmd overlay lookup %s %s", TARGET_PACKAGE, resourceName));
                    String.format("cmd overlay lookup --user %d %s %s", mCurrentUserId,
                            TARGET_PACKAGE, resourceName));
            if (result.equals(expectedValue + "\n") ||
            if (result.equals(expectedValue + "\n") ||
                    result.endsWith("-> " + expectedValue + "\n")) {
                    result.endsWith("-> " + expectedValue + "\n")) {
                return;
                return;
@@ -70,6 +74,7 @@ public class OverlayRemountedTestBase extends BaseHostJUnit4Test {
            }
            }
        }
        }


        fail(String.format("expected: <[%s]> in: <[%s]>", expectedValue, result));
        fail(String.format("expected: <[%s]> in: <[%s]>\nOverlay dump:\n%s", expectedValue, result,
                getDevice().executeShellCommand("cmd overlay dump " + OVERLAY_PACKAGE)));
    }
    }
}
}
+5 −2
Original line number Original line Diff line number Diff line
@@ -73,9 +73,10 @@ public class RegenerateIdmapTest extends OverlayRemountedTestBase {
    public void testIdmapPoliciesChanged() throws Exception {
    public void testIdmapPoliciesChanged() throws Exception {
        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
        final String targetResource = resourceName(TARGET_PACKAGE, "bool",
                "signature_policy_overlaid");
                "signature_policy_overlaid");
        final String overlayPath = "/product/overlay/TestOverlay.apk";


        mPreparer.pushResourceFile(TARGET_APK, "/product/app/OverlayTarget.apk")
        mPreparer.pushResourceFile(TARGET_APK, "/product/app/OverlayTarget.apk")
                .pushResourceFile(OVERLAY_APK, "/product/overlay/TestOverlay.apk")
                .pushResourceFile(OVERLAY_APK, overlayPath)
                .reboot()
                .reboot()
                .setOverlayEnabled(OVERLAY_PACKAGE, false);
                .setOverlayEnabled(OVERLAY_PACKAGE, false);


@@ -87,7 +88,9 @@ public class RegenerateIdmapTest extends OverlayRemountedTestBase {


        // Replace the overlay with a version of the overlay that is signed with the same signature
        // Replace the overlay with a version of the overlay that is signed with the same signature
        // as the target.
        // as the target.
        mPreparer.pushResourceFile(OVERLAY_SIGNATURE_APK, "/product/overlay/TestOverlay.apk")
        mPreparer.remount()
                .deleteFile(overlayPath)
                .pushResourceFile(OVERLAY_SIGNATURE_APK, overlayPath)
                .reboot();
                .reboot();


        // The idmap should have been recreated with the signature policy fulfilled.
        // The idmap should have been recreated with the signature policy fulfilled.
+17 −25
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@


package com.android.internal.util.test;
package com.android.internal.util.test;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;


import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.DeviceNotAvailableException;
@@ -66,6 +65,8 @@ public class SystemPreparer extends ExternalResource {
    // to manually verify the device state.
    // to manually verify the device state.
    private boolean mDebugSkipAfterReboot;
    private boolean mDebugSkipAfterReboot;


    private int mCurrentUserId;

    public SystemPreparer(TemporaryFolder hostTempFolder, DeviceProvider deviceProvider) {
    public SystemPreparer(TemporaryFolder hostTempFolder, DeviceProvider deviceProvider) {
        this(hostTempFolder, RebootStrategy.FULL, null, deviceProvider);
        this(hostTempFolder, RebootStrategy.FULL, null, deviceProvider);
    }
    }
@@ -88,6 +89,11 @@ public class SystemPreparer extends ExternalResource {
        mDebugSkipAfterReboot = debugSkipAfterReboot;
        mDebugSkipAfterReboot = debugSkipAfterReboot;
    }
    }


    @Override
    protected void before() throws Throwable {
        mCurrentUserId = mDeviceProvider.getDevice().getCurrentUser();
    }

    /** Copies a file within the host test jar to a path on device. */
    /** Copies a file within the host test jar to a path on device. */
    public SystemPreparer pushResourceFile(String filePath, String outputPath)
    public SystemPreparer pushResourceFile(String filePath, String outputPath)
            throws DeviceNotAvailableException, IOException {
            throws DeviceNotAvailableException, IOException {
@@ -140,24 +146,6 @@ public class SystemPreparer extends ExternalResource {
        return this;
        return this;
    }
    }


    /** Stages multiple APEXs within the host test jar onto the device. */
    public SystemPreparer stageMultiplePackages(String[] resourcePaths, String[] packageNames)
            throws DeviceNotAvailableException, IOException {
        assertEquals(resourcePaths.length, packageNames.length);
        final ITestDevice device = mDeviceProvider.getDevice();
        final String[] adbCommandLine = new String[resourcePaths.length + 2];
        adbCommandLine[0] = "install-multi-package";
        adbCommandLine[1] = "--staged";
        for (int i = 0; i < resourcePaths.length; i++) {
            final File tmpFile = copyResourceToTemp(resourcePaths[i]);
            adbCommandLine[i + 2] = tmpFile.getAbsolutePath();
            mInstalledPackages.add(packageNames[i]);
        }
        final String output = device.executeAdbCommand(adbCommandLine);
        assertTrue(output.contains("Success. Reboot device to apply staged session"));
        return this;
    }

    /** Sets the enable state of an overlay package. */
    /** Sets the enable state of an overlay package. */
    public SystemPreparer setOverlayEnabled(String packageName, boolean enabled)
    public SystemPreparer setOverlayEnabled(String packageName, boolean enabled)
            throws DeviceNotAvailableException {
            throws DeviceNotAvailableException {
@@ -166,11 +154,14 @@ public class SystemPreparer extends ExternalResource {


        // Wait for the overlay to change its enabled state.
        // Wait for the overlay to change its enabled state.
        final long endMillis = System.currentTimeMillis() + OVERLAY_ENABLE_TIMEOUT_MS;
        final long endMillis = System.currentTimeMillis() + OVERLAY_ENABLE_TIMEOUT_MS;
        String result;
        String result = null;
        while (System.currentTimeMillis() <= endMillis) {
        while (System.currentTimeMillis() <= endMillis) {
            device.executeShellCommand(String.format("cmd overlay %s %s", enable, packageName));
            device.executeShellCommand(
            result = device.executeShellCommand("cmd overlay dump isenabled "
                    String.format("cmd overlay %s --user %d %s", enable, mCurrentUserId,
                    + packageName);
                            packageName));
            result = device.executeShellCommand(
                    String.format("cmd overlay dump --user %d isenabled %s", mCurrentUserId,
                            packageName));
            if (((enabled) ? "true\n" : "false\n").equals(result)) {
            if (((enabled) ? "true\n" : "false\n").equals(result)) {
                return this;
                return this;
            }
            }
@@ -181,8 +172,9 @@ public class SystemPreparer extends ExternalResource {
            }
            }
        }
        }


        throw new IllegalStateException(String.format("Failed to %s overlay %s:\n%s", enable,
        throw new IllegalStateException(String.format(
                packageName, device.executeShellCommand("cmd overlay list")));
                "Failed to %s overlay %s. Last result:\n%s\nOverlay list:\n%s",
                enable, packageName, result, device.executeShellCommand("cmd overlay list")));
    }
    }


    /** Restarts the device and waits until after boot is completed. */
    /** Restarts the device and waits until after boot is completed. */