Initial Process and Binder support, with CTS.

The Ravenwood environment doesn't support IPC, but much of the code
that developers intend to test (such as AIDL stubs) still relies on
baseline same-process behavior to be intact.

This change adds UID and PID support to Process and Binder, but we
carefully communicate that it's only available when the test author
has configured it via a RavenwoodRule.  This ensures that tests
don't accidentally rely on unexpected defaults, as some tests will
want AID_SYSTEM, and others want a normal app UID.

Also bring along SystemClock, PatternMatcher, and ParcelUuid
along with relevant tests.

Finally, now that we have an IgnoreUnderRavenwood annotation, we
can pivot back our default failure to be a RuntimeException instead
of an AssumptionViolatedException.

Bug: 292141694
Test: atest-dev CtsOsTestCasesRavenwood CtsOsTestCases
Change-Id: I5f54c3179b2d305b9ab9144c43fd063c6b756e44
diff --git a/ravenwood/Android.bp b/ravenwood/Android.bp
index ec12d21..fc4ed1d 100644
--- a/ravenwood/Android.bp
+++ b/ravenwood/Android.bp
@@ -28,9 +28,8 @@
     name: "ravenwood-junit",
     srcs: ["junit-src/**/*.java"],
     libs: [
+        "framework-minus-apex.ravenwood",
         "junit",
     ],
-    sdk_version: "core_current",
-    host_supported: true,
     visibility: ["//visibility:public"],
 }
diff --git a/ravenwood/framework-minus-apex-ravenwood-policies.txt b/ravenwood/framework-minus-apex-ravenwood-policies.txt
index 48d5722..692d598 100644
--- a/ravenwood/framework-minus-apex-ravenwood-policies.txt
+++ b/ravenwood/framework-minus-apex-ravenwood-policies.txt
@@ -81,6 +81,8 @@
 class com.android.internal.util.Preconditions stubclass
 class com.android.internal.util.StringPool stubclass
 
+class com.android.internal.os.SomeArgs stubclass
+
 # Parcel
 class android.os.Parcel stubclass
     method writeException (Ljava/lang/Exception;)V @writeException$ravenwood
@@ -92,14 +94,16 @@
 class android.os.BadParcelableException stubclass
 class android.os.BadTypeParcelableException stubclass
 
-# Binder: just enough to construct, no further functionality
-class android.os.Binder stub
-    method <init> ()V stub
-    method <init> (Ljava/lang/String;)V stub
-    method isDirectlyHandlingTransaction ()Z stub
-    method isDirectlyHandlingTransactionNative ()Z @isDirectlyHandlingTransactionNative$ravenwood
-    method getNativeBBinderHolder ()J @getNativeBBinderHolder$ravenwood
+# Binder
+class android.os.DeadObjectException stubclass
+class android.os.DeadSystemException stubclass
+class android.os.RemoteException stubclass
+class android.os.TransactionTooLargeException stubclass
 
 # Containers
 class android.os.BaseBundle stubclass
 class android.os.Bundle stubclass
+
+# Misc
+class android.os.PatternMatcher stubclass
+class android.os.ParcelUuid stubclass
diff --git a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java
index a6b3f66..bffd0cd 100644
--- a/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java
+++ b/ravenwood/junit-src/android/platform/test/ravenwood/RavenwoodRule.java
@@ -16,6 +16,7 @@
 
 package android.platform.test.ravenwood;
 
+import android.os.Process;
 import android.platform.test.annotations.IgnoreUnderRavenwood;
 
 import org.junit.Assume;
@@ -23,6 +24,8 @@
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 /**
  * THIS RULE IS EXPERIMENTAL. REACH OUT TO g/ravenwood BEFORE USING IT, OR YOU HAVE ANY
  * QUESTIONS ABOUT IT.
@@ -30,20 +33,84 @@
  * @hide
  */
 public class RavenwoodRule implements TestRule {
+    private static AtomicInteger sNextPid = new AtomicInteger(100);
+
+    /**
+     * Unless the test author requests differently, run as "nobody", and give each collection of
+     * tests its own unique PID.
+     */
+    private int mUid = android.os.Process.NOBODY_UID;
+    private int mPid = sNextPid.getAndIncrement();
+
+    public RavenwoodRule() {
+    }
+
+    public static class Builder {
+        private RavenwoodRule mRule = new RavenwoodRule();
+
+        public Builder() {
+        }
+
+        /**
+         * Configure the identity of this process to be the system UID for the duration of the
+         * test. Has no effect under non-Ravenwood environments.
+         */
+        public Builder setProcessSystem() {
+            mRule.mUid = android.os.Process.SYSTEM_UID;
+            return this;
+        }
+
+        /**
+         * Configure the identity of this process to be an app UID for the duration of the
+         * test. Has no effect under non-Ravenwood environments.
+         */
+        public Builder setProcessApp() {
+            mRule.mUid = android.os.Process.FIRST_APPLICATION_UID;
+            return this;
+        }
+
+        public RavenwoodRule build() {
+            return mRule;
+        }
+    }
+
+    /**
+     * Return if the current process is running under a Ravenwood test environment.
+     */
     public boolean isUnderRavenwood() {
         // TODO: give ourselves a better environment signal
         return System.getProperty("java.class.path").contains("ravenwood");
     }
 
+    private void init() {
+        android.os.Process.init$ravenwood(mUid, mPid);
+        android.os.Binder.init$ravenwood();
+    }
+
+    private void reset() {
+        android.os.Process.reset$ravenwood();
+        android.os.Binder.reset$ravenwood();
+    }
+
     @Override
     public Statement apply(Statement base, Description description) {
         return new Statement() {
             @Override
             public void evaluate() throws Throwable {
+                final boolean isUnderRavenwood = isUnderRavenwood();
                 if (description.getAnnotation(IgnoreUnderRavenwood.class) != null) {
-                    Assume.assumeFalse(isUnderRavenwood());
+                    Assume.assumeFalse(isUnderRavenwood);
                 }
-                base.evaluate();
+                if (isUnderRavenwood) {
+                    init();
+                }
+                try {
+                    base.evaluate();
+                } finally {
+                    if (isUnderRavenwood) {
+                        reset();
+                    }
+                }
             }
         };
     }
diff --git a/ravenwood/ravenwood-annotation-allowed-classes.txt b/ravenwood/ravenwood-annotation-allowed-classes.txt
index c6dd6e3..776a19a 100644
--- a/ravenwood/ravenwood-annotation-allowed-classes.txt
+++ b/ravenwood/ravenwood-annotation-allowed-classes.txt
@@ -1,3 +1,9 @@
 # Only classes listed here can use the Ravenwood annotations.
 
 com.android.internal.util.ArrayUtils
+
+android.os.Binder
+android.os.Binder$IdentitySupplier
+android.os.IBinder
+android.os.Process
+android.os.SystemClock