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

Commit 4cce078b authored by Lee Shombert's avatar Lee Shombert Committed by Android (Google) Code Review
Browse files

Merge "Prepare for testing mainline module caches" into main

parents ddcbebad 2fdb2dbf
Loading
Loading
Loading
Loading
+16 −10
Original line number Diff line number Diff line
@@ -293,6 +293,7 @@ public class PropertyInvalidatedCache<Query, Result> {

    // The test mode. This is only used to ensure that the test functions setTestMode() and
    // testPropertyName() are used correctly.
    @GuardedBy("sGlobalLock")
    private static boolean sTestMode = false;

    /**
@@ -668,7 +669,7 @@ public class PropertyInvalidatedCache<Query, Result> {
        // True if this handler is in test mode.  If it is in test mode, then nonces are stored
        // and retrieved from mTestNonce.
        @GuardedBy("mLock")
        private boolean mTestMode = false;
        private boolean mTestMode;

        // This is the local value of the nonce, as last set by the NonceHandler.  It is always
        // updated by the setNonce() operation.  The getNonce() operation returns this value in
@@ -692,6 +693,9 @@ public class PropertyInvalidatedCache<Query, Result> {

        NonceHandler(@NonNull String name) {
            mName = name;
            synchronized (sGlobalLock) {
                mTestMode = sTestMode;
            }
        }

        /**
@@ -1414,9 +1418,13 @@ public class PropertyInvalidatedCache<Query, Result> {

    /**
     * Enable or disable testing.  The protocol requires that the mode toggle: for instance, it is
     * illegal to clear the test mode if the test mode is already off.  The purpose is solely to
     * ensure that test clients do not forget to use the test mode properly, even though the
     * current logic does not care.
     * illegal to clear the test mode if the test mode is already off.  Enabling test mode puts
     * all caches in the process into test mode; all nonces are initialized to UNSET and
     * subsequent reads and writes are to process memory.  This has the effect of disabling all
     * caches that are not local to the process.  Disabling test mode restores caches to normal
     * operation.
     * @param mode The desired test mode.
     * @throws IllegalStateException if the supplied mode is already set.
     * @hide
     */
    @VisibleForTesting
@@ -1431,10 +1439,8 @@ public class PropertyInvalidatedCache<Query, Result> {
                }
            }
            sTestMode = mode;
            if (mode) {
                // No action when testing begins.
            } else {
                resetAfterTestLocked();
            if (Flags.picTestMode() || !mode) {
                setTestModeLocked(mode);
            }
        }
    }
@@ -1445,11 +1451,11 @@ public class PropertyInvalidatedCache<Query, Result> {
     * that were not originally in test mode.
     */
    @GuardedBy("sGlobalLock")
    private static void resetAfterTestLocked() {
    private static void setTestModeLocked(boolean mode) {
        for (Iterator<String> e = sHandlers.keys().asIterator(); e.hasNext(); ) {
            String s = e.next();
            final NonceHandler h = sHandlers.get(s);
            h.setTestMode(false);
            h.setTestMode(mode);
        }
    }

+7 −0
Original line number Diff line number Diff line
@@ -50,3 +50,10 @@ flag {
     description: "Cache null returns from binder calls"
     bug: "372923336"
}

flag {
     namespace: "system_performance"
     name: "pic_test_mode"
     description: "Updated test mode for PIC"
     bug: "396173886"
}
+7 −3
Original line number Diff line number Diff line
@@ -719,9 +719,13 @@ public class IpcDataCache<Query, Result> extends PropertyInvalidatedCache<Query,

    /**
     * Enable or disable testing.  The protocol requires that the mode toggle: for instance, it is
     * illegal to clear the test mode if the test mode is already off.  The purpose is solely to
     * ensure that test clients do not forget to use the test mode properly, even though the
     * current logic does not care.
     * illegal to clear the test mode if the test mode is already off.  Enabling test mode puts
     * all caches in the process into test mode; all nonces are initialized to UNSET and
     * subsequent reads and writes are to process memory.  This has the effect of disabling all
     * caches that are not local to the process.  Disabling test mode restores caches to normal
     * operation.
     * @param mode The desired test mode.
     * @throws IllegalStateException if the supplied mode is already set.
     * @hide
     */
    @TestApi
+16 −9
Original line number Diff line number Diff line
@@ -414,16 +414,23 @@ public class PropertyInvalidatedCacheTests {
    @Test
    @DisabledOnRavenwood(reason = "SystemProperties doesn't have permission check")
    public void testPermissionFailure() {
        try {
            // Disable the test mode for this test, but ensure that it will be enabled when the
            // test exits.
            PropertyInvalidatedCache.setTestMode(false);
            // Create a cache that will write a system nonce.
            TestCache sysCache = new TestCache(MODULE_SYSTEM, "mode1");
            try {
            // Invalidate the cache, which writes the system property.  There must be a permission
            // failure.
                // Invalidate the cache, which writes the system property.  There must be a
                // permission failure.
                sysCache.invalidateCache();
                fail("expected permission failure");
            } catch (RuntimeException e) {
            // The expected exception is a bare RuntimeException.  The test does not attempt to
            // validate the text of the exception message.
                // The expected exception is a bare RuntimeException.  The test does not attempt
                // to validate the text of the exception message.
            }
        } finally {
            PropertyInvalidatedCache.setTestMode(true);
        }
    }

+16 −10
Original line number Diff line number Diff line
@@ -452,22 +452,28 @@ public class IpcDataCacheTest {
        assertTrue(ec.isDisabled());
    }


    // Verify that invalidating the cache from an app process would fail due to lack of permissions.
    @Test
    @android.platform.test.annotations.DisabledOnRavenwood(
            reason = "SystemProperties doesn't have permission check")
    public void testPermissionFailure() {
        try {
            // Disable test mode for this test.  Guarantee that the mode is enabled before the
            // test exits.
            IpcDataCache.setTestMode(false);
            // Create a cache that will write a system nonce.
            TestCache sysCache = new TestCache(IpcDataCache.MODULE_SYSTEM, "mode1");
            try {
            // Invalidate the cache, which writes the system property.  There must be a permission
            // failure.
                // Invalidate the cache, which writes the system property.  There must be a
                // permission failure.
                sysCache.invalidateCache();
                fail("expected permission failure");
            } catch (RuntimeException e) {
            // The expected exception is a bare RuntimeException.  The test does not attempt to
            // validate the text of the exception message.
                // The expected exception is a bare RuntimeException.  The test does not attempt
                // to validate the text of the exception message.
            }
        } finally {
            IpcDataCache.setTestMode(true);
        }
    }