From 8775923a18f5cc155857dac7bdafccb46d0599e9 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Tue, 21 May 2024 11:20:08 -0700 Subject: Ensure Build is always usable on Ravenwood ... ... even without a RavenwoodRule. There are situation where android APIs are called outside of a RavenwoodRule, but some of the APIs require a RavenwoodRule to initialize them. Since Build and SystemProperties are critical and often used by the test infra code (e.g. test runner or other junit rules), make sure they're always accessible. Fix: 341735388 Test: atest CtsOsTestCases Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh Change-Id: Iedcfae7e75c5ee9f2c7fb87c61262c2b33450d04 --- .../test/ravenwood/RavenwoodSystemProperties.java | 40 +++++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'ravenwood/junit-src') diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodSystemProperties.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodSystemProperties.java index 85ad4e444f24..c3786ee0041d 100644 --- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodSystemProperties.java +++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodSystemProperties.java @@ -22,7 +22,9 @@ import java.util.Map; import java.util.Set; import java.util.function.Predicate; -class RavenwoodSystemProperties { +public class RavenwoodSystemProperties { + private volatile boolean mIsImmutable; + private final Map mValues = new HashMap<>(); /** Set of additional keys that should be considered readable */ @@ -101,15 +103,23 @@ class RavenwoodSystemProperties { setValue("ro.debuggable", "1"); } - Map getValues() { + /** Copy constructor */ + public RavenwoodSystemProperties(RavenwoodSystemProperties source, boolean immutable) { + this.mKeyReadable.addAll(source.mKeyReadable); + this.mKeyWritable.addAll(source.mKeyWritable); + this.mValues.putAll(source.mValues); + this.mIsImmutable = immutable; + } + + public Map getValues() { return new HashMap<>(mValues); } - Predicate getKeyReadablePredicate() { + public Predicate getKeyReadablePredicate() { return mKeyReadablePredicate; } - Predicate getKeyWritablePredicate() { + public Predicate getKeyWritablePredicate() { return mKeyWritablePredicate; } @@ -123,12 +133,20 @@ class RavenwoodSystemProperties { "vendor_dlkm", }; + private void ensureNotImmutable() { + if (mIsImmutable) { + throw new RuntimeException("Unable to update immutable instance"); + } + } + /** * Set the given property for all possible partitions where it could be defined. For * example, the value of {@code ro.build.type} is typically also mirrored under * {@code ro.system.build.type}, etc. */ private void setValueForPartitions(String key, String value) { + ensureNotImmutable(); + setValue("ro." + key, value); for (String partition : PARTITIONS) { setValue("ro." + partition + "." + key, value); @@ -136,6 +154,8 @@ class RavenwoodSystemProperties { } public void setValue(String key, Object value) { + ensureNotImmutable(); + final String valueString = (value == null) ? null : String.valueOf(value); if ((valueString == null) || valueString.isEmpty()) { mValues.remove(key); @@ -145,16 +165,19 @@ class RavenwoodSystemProperties { } public void setAccessNone(String key) { + ensureNotImmutable(); mKeyReadable.remove(key); mKeyWritable.remove(key); } public void setAccessReadOnly(String key) { + ensureNotImmutable(); mKeyReadable.add(key); mKeyWritable.remove(key); } public void setAccessReadWrite(String key) { + ensureNotImmutable(); mKeyReadable.add(key); mKeyWritable.add(key); } @@ -172,4 +195,11 @@ class RavenwoodSystemProperties { return key; } } -} + + /** + * Return an immutable, default instance. + */ + // Create a default instance, and make an immutable copy of it. + public static final RavenwoodSystemProperties DEFAULT_VALUES = + new RavenwoodSystemProperties(new RavenwoodSystemProperties(), true); +} \ No newline at end of file -- cgit v1.2.3-59-g8ed1b