summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Lee Shombert <shombert@google.com> 2025-03-24 13:22:00 -0700
committer Android (Google) Code Review <android-gerrit@google.com> 2025-03-24 13:22:00 -0700
commit493b23e3f411977be81e67432c84779a3ebab2c1 (patch)
tree88b51298a79c06d23557a918d945ca105c4e4ae5
parent815fdd8475cf205bf81f7c17db2c0b7e306fbe0b (diff)
parent06701b0e695c025fb839621969f0e631f899677d (diff)
Merge "Make setCacheTestMode() a module API" into main
-rw-r--r--core/api/module-lib-current.txt1
-rw-r--r--core/java/android/os/IpcDataCache.java20
-rw-r--r--core/java/android/os/flags.aconfig8
-rw-r--r--core/tests/coretests/src/android/os/IpcDataCacheTest.java42
4 files changed, 71 insertions, 0 deletions
diff --git a/core/api/module-lib-current.txt b/core/api/module-lib-current.txt
index 98570172e43c..0a8b18443ff0 100644
--- a/core/api/module-lib-current.txt
+++ b/core/api/module-lib-current.txt
@@ -412,6 +412,7 @@ package android.os {
method public void invalidateCache();
method public static void invalidateCache(@NonNull String, @NonNull String);
method @Nullable public Result query(@NonNull Query);
+ method @FlaggedApi("android.os.ipc_data_cache_test_apis") public static void setCacheTestMode(boolean);
field public static final String MODULE_BLUETOOTH = "bluetooth";
}
diff --git a/core/java/android/os/IpcDataCache.java b/core/java/android/os/IpcDataCache.java
index 07a7f8b50f20..befc07116418 100644
--- a/core/java/android/os/IpcDataCache.java
+++ b/core/java/android/os/IpcDataCache.java
@@ -733,4 +733,24 @@ public class IpcDataCache<Query, Result> extends PropertyInvalidatedCache<Query,
public static void setTestMode(boolean mode) {
PropertyInvalidatedCache.setTestMode(mode);
}
+
+ /**
+ * Enable or disable test mode. The protocol requires that the mode toggle: for instance, it is
+ * 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.
+ * @throws IllegalStateException if the process is not running an instrumentation test.
+ * @hide
+ */
+ @FlaggedApi(android.os.Flags.FLAG_IPC_DATA_CACHE_TEST_APIS)
+ @SystemApi(client=SystemApi.Client.MODULE_LIBRARIES)
+ public static void setCacheTestMode(boolean mode) {
+ // Trunk-stable flagging requires that this API have a name different from the existing
+ // setTestMode() API. However, the functionality is identical.
+ setTestMode(mode);
+ }
}
diff --git a/core/java/android/os/flags.aconfig b/core/java/android/os/flags.aconfig
index 14c154d2e3a9..4aa0c1488bd7 100644
--- a/core/java/android/os/flags.aconfig
+++ b/core/java/android/os/flags.aconfig
@@ -246,6 +246,14 @@ flag {
}
flag {
+ name: "ipc_data_cache_test_apis"
+ namespace: "system_performance"
+ description: "Expose IpcDataCache test apis to mainline modules."
+ bug: "396173886"
+ is_exported: true
+}
+
+flag {
name: "mainline_vcn_platform_api"
namespace: "vcn"
description: "Expose platform APIs to mainline VCN"
diff --git a/core/tests/coretests/src/android/os/IpcDataCacheTest.java b/core/tests/coretests/src/android/os/IpcDataCacheTest.java
index 2b49b38607d8..8c97786ff5e5 100644
--- a/core/tests/coretests/src/android/os/IpcDataCacheTest.java
+++ b/core/tests/coretests/src/android/os/IpcDataCacheTest.java
@@ -518,6 +518,48 @@ public class IpcDataCacheTest {
IpcDataCache.setTestMode(true);
}
+ // Verify that test cache mode works properly. This test is identical to testTestMode except
+ // that it uses the alternative name (the API that is visible to mainline modules).
+ @Test
+ public void testCacheTestMode() {
+ // Create a cache that will write a system nonce.
+ TestCache sysCache = new TestCache(IpcDataCache.MODULE_SYSTEM, "mode1");
+
+ sysCache.testPropertyName();
+ // Invalidate the cache. This must succeed because the property has been marked for
+ // testing.
+ sysCache.invalidateCache();
+
+ // Create a cache that uses MODULE_TEST. Invalidation succeeds whether or not the
+ // property is tagged as being tested.
+ TestCache testCache = new TestCache(IpcDataCache.MODULE_TEST, "mode2");
+ testCache.invalidateCache();
+ testCache.testPropertyName();
+ testCache.invalidateCache();
+
+ // Clear test mode. This fails if test mode is not enabled.
+ IpcDataCache.setCacheTestMode(false);
+ try {
+ IpcDataCache.setCacheTestMode(false);
+ if (android.app.Flags.enforcePicTestmodeProtocol()) {
+ fail("expected an IllegalStateException");
+ }
+ } catch (IllegalStateException e) {
+ // The expected exception.
+ }
+ // Configuring a property for testing must fail if test mode is false.
+ TestCache cache2 = new TestCache(IpcDataCache.MODULE_SYSTEM, "mode3");
+ try {
+ cache2.testPropertyName();
+ fail("expected an IllegalStateException");
+ } catch (IllegalStateException e) {
+ // The expected exception.
+ }
+
+ // Re-enable test mode (so that the cleanup for the test does not throw).
+ IpcDataCache.setCacheTestMode(true);
+ }
+
@Test
public void testCachingNulls() {
IpcDataCache.Config c =