AI 146911: am: CL 146910 [Issue 1556587] CTS: CTS should include test cases to ensure application process and user isolation
  Original author: sus
  Merged from: //branches/cupcake/...

Automated import of CL 146911
diff --git a/tests/ProcessTest/Android.mk b/tests/ProcessTest/Android.mk
new file mode 100644
index 0000000..129a7c7
--- /dev/null
+++ b/tests/ProcessTest/Android.mk
@@ -0,0 +1,29 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+LOCAL_JAVA_LIBRARIES := framework-tests android.test.runner
+
+LOCAL_AAPT_FLAGS = -c xx_YY -c cs
+
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_PACKAGE_NAME := ProcessTests
+
+include $(BUILD_PACKAGE)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/ProcessTest/AndroidManifest.xml b/tests/ProcessTest/AndroidManifest.xml
new file mode 100644
index 0000000..b860bde
--- /dev/null
+++ b/tests/ProcessTest/AndroidManifest.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.cts.process"
+       android:sharedUserId="com.android.cts.process.uidpid_test">
+
+    <!-- InstrumentationTestRunner for AndroidTests -->
+    <instrumentation android:name="android.test.InstrumentationTestRunner"
+                     android:targetPackage="com.android.cts.process"
+                     android:label="Test process"/>
+    <application>
+
+        <uses-library android:name="android.test.runner" />
+
+        <activity android:name=".activity.SharePidActivity"
+                android:process=":shareProcess"/>
+        <activity android:name=".activity.SharePidSubActivity"
+                android:process=":shareProcess"/>
+        <activity android:name=".activity.NoSharePidActivity"
+                android:process=":noShareProcess"/>
+    </application>
+
+</manifest>
diff --git a/tests/ProcessTest/NoShareUidApp/Android.mk b/tests/ProcessTest/NoShareUidApp/Android.mk
new file mode 100644
index 0000000..df93349
--- /dev/null
+++ b/tests/ProcessTest/NoShareUidApp/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_PACKAGE_NAME := NoShareUidApp
+
+include $(BUILD_PACKAGE)
diff --git a/tests/ProcessTest/NoShareUidApp/AndroidManifest.xml b/tests/ProcessTest/NoShareUidApp/AndroidManifest.xml
new file mode 100644
index 0000000..ddc73cf
--- /dev/null
+++ b/tests/ProcessTest/NoShareUidApp/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ -->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.cts.process.noshareuidapp">
+
+</manifest>
diff --git a/tests/ProcessTest/NoShareUidApp/src/com/android/cts/process/activity/NoSharePidActivity.java b/tests/ProcessTest/NoShareUidApp/src/com/android/cts/process/activity/NoSharePidActivity.java
new file mode 100644
index 0000000..e5f0ab4
--- /dev/null
+++ b/tests/ProcessTest/NoShareUidApp/src/com/android/cts/process/activity/NoSharePidActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.process.activity;
+
+import android.app.Activity;
+
+public class NoSharePidActivity extends Activity {
+
+}
diff --git a/tests/ProcessTest/ShareUidApp/Android.mk b/tests/ProcessTest/ShareUidApp/Android.mk
new file mode 100644
index 0000000..caa2ad6
--- /dev/null
+++ b/tests/ProcessTest/ShareUidApp/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2009 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES := $(call all-subdir-java-files)
+
+LOCAL_PACKAGE_NAME := ShareUidApp
+
+include $(BUILD_PACKAGE)
diff --git a/tests/ProcessTest/ShareUidApp/AndroidManifest.xml b/tests/ProcessTest/ShareUidApp/AndroidManifest.xml
new file mode 100644
index 0000000..19981f4
--- /dev/null
+++ b/tests/ProcessTest/ShareUidApp/AndroidManifest.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+       package="com.android.cts.process.shareuidapp"
+       android:sharedUserId="com.android.cts.process.uidpid_test">
+
+</manifest>
diff --git a/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidActivity.java b/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidActivity.java
new file mode 100644
index 0000000..55db78b
--- /dev/null
+++ b/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.process.activity;
+
+import android.app.Activity;
+
+public class SharePidActivity extends Activity {
+
+}
diff --git a/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidSubActivity.java b/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidSubActivity.java
new file mode 100644
index 0000000..c06d9fb
--- /dev/null
+++ b/tests/ProcessTest/ShareUidApp/src/com/android/cts/process/activity/SharePidSubActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.cts.process.activity;
+
+import android.app.Activity;
+
+public class SharePidSubActivity extends Activity {
+
+}
diff --git a/tests/ProcessTest/src/com/android/cts/process/ProcessTest.java b/tests/ProcessTest/src/com/android/cts/process/ProcessTest.java
new file mode 100644
index 0000000..a64a900
--- /dev/null
+++ b/tests/ProcessTest/src/com/android/cts/process/ProcessTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.process;
+
+import java.util.List;
+
+import android.app.ActivityManager;
+import android.app.ActivityManager.RunningAppProcessInfo;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.test.AndroidTestCase;
+
+import com.android.cts.process.activity.NoSharePidActivity;
+import com.android.cts.process.activity.SharePidActivity;
+import com.android.cts.process.activity.SharePidSubActivity;
+
+public class ProcessTest extends AndroidTestCase {
+    private final int WAIT_TIME = 2000;
+
+    public void testUid() throws Exception {
+        String enableApp = "com.android.cts.process.shareuidapp";
+        String disableApp = "com.android.cts.process.noshareuidapp";
+        String testApp = mContext.getPackageName();
+        int uid1 = mContext.getPackageManager().getApplicationInfo(enableApp,
+                PackageManager.GET_META_DATA).uid;
+        int uid2 = mContext.getPackageManager().getApplicationInfo(disableApp,
+                PackageManager.GET_META_DATA).uid;
+        int uid3 = mContext.getPackageManager().getApplicationInfo(testApp,
+                PackageManager.GET_META_DATA).uid;
+        assertEquals(uid1, uid3);
+        assertNotSame(uid2, uid3);
+    }
+
+    public void testPid() throws Exception {
+        ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
+        String shareProcessName = mContext.getPackageName() + ":shareProcess";
+        String noShareProcessName = mContext.getPackageName() + ":noShareProcess";
+        List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
+        assertEquals(-1, getPid(shareProcessName, list));
+        // share pid will use same process
+        Intent sharePidIntent = new Intent();
+        sharePidIntent.setClass(mContext, SharePidActivity.class);
+        sharePidIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mContext.startActivity(sharePidIntent);
+        Thread.sleep(WAIT_TIME);
+        List<RunningAppProcessInfo> sharelist = am.getRunningAppProcesses();
+        int sharePid = getPid(shareProcessName, sharelist);
+        assertTrue(-1 != sharePid);
+        Intent sharePidStubIntent = new Intent();
+        sharePidStubIntent.setClass(mContext, SharePidSubActivity.class);
+        sharePidStubIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mContext.startActivity(sharePidStubIntent);
+        Thread.sleep(WAIT_TIME);
+        List<RunningAppProcessInfo> shareStublist = am.getRunningAppProcesses();
+        int shareStubPid = getPid(shareProcessName, shareStublist);
+        assertTrue(-1 != shareStubPid);
+        assertEquals(sharePid, shareStubPid);
+        // no share pid will create a new process
+        Intent noSharePidIntent = new Intent();
+        noSharePidIntent.setClass(mContext, NoSharePidActivity.class);
+        noSharePidIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        mContext.startActivity(noSharePidIntent);
+        Thread.sleep(WAIT_TIME);
+        List<RunningAppProcessInfo> noShareStublist = am.getRunningAppProcesses();
+        int noSharePid = getPid(noShareProcessName, noShareStublist);
+        assertTrue(-1 != noSharePid);
+        assertTrue(sharePid != noSharePid);
+        // kill the process after test
+        android.os.Process.killProcess(noSharePid);
+        android.os.Process.killProcess(sharePid);
+    }
+
+    private int getPid(String processName, List<RunningAppProcessInfo> list) {
+        for (RunningAppProcessInfo rai : list) {
+            if (processName.equals(rai.processName))
+                return rai.pid;
+        }
+        return -1;
+    }
+
+}