Add tests for revoking install permissions when definer is uninstalled.

Bug: 155648771
Test: atest RemovePermissionTest
Change-Id: I66decb0833c3a17c39ea9918b3444f0be1b6bdad
diff --git a/tests/tests/permission/AndroidTest.xml b/tests/tests/permission/AndroidTest.xml
index 22329e6..e895763 100644
--- a/tests/tests/permission/AndroidTest.xml
+++ b/tests/tests/permission/AndroidTest.xml
@@ -60,8 +60,9 @@
         <option name="push" value="CtsVictimPermissionDefinerApp.apk->/data/local/tmp/cts/permissions/CtsVictimPermissionDefinerApp.apk" />
         <option name="push" value="CtsRuntimePermissionDefinerApp.apk->/data/local/tmp/cts/permissions/CtsRuntimePermissionDefinerApp.apk" />
         <option name="push" value="CtsRuntimePermissionUserApp.apk->/data/local/tmp/cts/permissions/CtsRuntimePermissionUserApp.apk" />
-        <option name="push" value="CtsInstalltimePermissionDefinerApp.apk->/data/local/tmp/cts/permissions/CtsInstalltimePermissionDefinerApp.apk" />
-        <option name="push" value="CtsInstalltimePermissionUserApp.apk->/data/local/tmp/cts/permissions/CtsInstalltimePermissionUserApp.apk" />
+        <option name="push" value="CtsInstallPermissionDefinerApp.apk->/data/local/tmp/cts/permissions/CtsInstallPermissionDefinerApp.apk" />
+        <option name="push" value="CtsInstallPermissionUserApp.apk->/data/local/tmp/cts/permissions/CtsInstallPermissionUserApp.apk" />
+        <option name="push" value="CtsInstallPermissionEscalatorApp.apk->/data/local/tmp/cts/permissions/CtsInstallPermissionEscalatorApp.apk" />
         <option name="push" value="CtsAppThatRequestsOneTimePermission.apk->/data/local/tmp/cts/permissions/CtsAppThatRequestsOneTimePermission.apk" />
     </target_preparer>
 
diff --git a/tests/tests/permission/src/android/permission/cts/RemovePermissionTest.java b/tests/tests/permission/src/android/permission/cts/RemovePermissionTest.java
index 466fb7f..57e8755 100644
--- a/tests/tests/permission/src/android/permission/cts/RemovePermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/RemovePermissionTest.java
@@ -35,6 +35,7 @@
 
 import com.android.compatibility.common.util.SystemUtil;
 
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -52,17 +53,19 @@
             APP_PKG_NAME_BASE + ".runtimepermissionuserapp";
     private static final String RUNTIME_PERMISSION_DEFINER_PKG_NAME =
             APP_PKG_NAME_BASE + ".runtimepermissiondefinerapp";
-    private static final String INSTALLTIME_PERMISSION_USER_PKG_NAME =
-            APP_PKG_NAME_BASE + ".installtimepermissionuserapp";
-    private static final String INSTALLTIME_PERMISSION_DEFINER_PKG_NAME =
-            APP_PKG_NAME_BASE + ".installtimepermissiondefinerapp";
+    private static final String INSTALL_PERMISSION_USER_PKG_NAME =
+            APP_PKG_NAME_BASE + ".installpermissionuserapp";
+    private static final String INSTALL_PERMISSION_DEFINER_PKG_NAME =
+            APP_PKG_NAME_BASE + ".installpermissiondefinerapp";
+    private static final String INSTALL_PERMISSION_ESCALATOR_PKG_NAME =
+            APP_PKG_NAME_BASE + ".installpermissionescalatorapp";
 
     private static final String TEST_PERMISSION =
             "android.permission.cts.revokepermissionwhenremoved.TestPermission";
     private static final String TEST_RUNTIME_PERMISSION =
             APP_PKG_NAME_BASE + ".TestRuntimePermission";
-    private static final String TEST_INSTALLTIME_PERMISSION =
-            APP_PKG_NAME_BASE + ".TestInstalltimePermission";
+    private static final String TEST_INSTALL_PERMISSION =
+            APP_PKG_NAME_BASE + ".TestInstallPermission";
 
     private static final String ADVERSARIAL_PERMISSION_DEFINER_APK_NAME =
             "CtsAdversarialPermissionDefinerApp";
@@ -74,14 +77,15 @@
             "CtsRuntimePermissionDefinerApp";
     private static final String RUNTIME_PERMISSION_USER_APK_NAME =
             "CtsRuntimePermissionUserApp";
-    private static final String INSTALLTIME_PERMISSION_DEFINER_APK_NAME =
-            "CtsInstalltimePermissionDefinerApp";
-    private static final String INSTALLTIME_PERMISSION_USER_APK_NAME =
-            "CtsInstalltimePermissionUserApp";
+    private static final String INSTALL_PERMISSION_DEFINER_APK_NAME =
+            "CtsInstallPermissionDefinerApp";
+    private static final String INSTALL_PERMISSION_USER_APK_NAME =
+            "CtsInstallPermissionUserApp";
+    private static final String INSTALL_PERMISSION_ESCALATOR_APK_NAME =
+            "CtsInstallPermissionEscalatorApp";
 
     private Context mContext;
     private Instrumentation mInstrumentation;
-    private Object mMySync = new Object();
 
     @Before
     public void setContextAndInstrumentation() {
@@ -94,6 +98,19 @@
         SystemUtil.runShellCommand("input keyevent KEYCODE_WAKEUP");
     }
 
+    @After
+    public void cleanUpTestApps() throws Exception {
+        uninstallApp(ADVERSARIAL_PERMISSION_DEFINER_PKG_NAME, true);
+        uninstallApp(ADVERSARIAL_PERMISSION_USER_PKG_NAME, true);
+        uninstallApp(VICTIM_PERMISSION_DEFINER_PKG_NAME, true);
+        uninstallApp(RUNTIME_PERMISSION_DEFINER_PKG_NAME, true);
+        uninstallApp(RUNTIME_PERMISSION_USER_PKG_NAME, true);
+        uninstallApp(INSTALL_PERMISSION_USER_PKG_NAME, true);
+        uninstallApp(INSTALL_PERMISSION_DEFINER_PKG_NAME, true);
+        uninstallApp(INSTALL_PERMISSION_ESCALATOR_PKG_NAME, true);
+        Thread.sleep(5000);
+    }
+
     private boolean permissionGranted(String pkgName, String permName)
             throws PackageManager.NameNotFoundException {
         PackageInfo appInfo = mContext.getPackageManager().getPackageInfo(pkgName,
@@ -112,19 +129,20 @@
     private void installApp(String apk) throws InterruptedException {
         String installResult = SystemUtil.runShellCommand(
                 "pm install -r -d data/local/tmp/cts/permissions/" + apk + ".apk");
-        synchronized (mMySync) {
-            mMySync.wait(10000);
-        }
         assertEquals("Success", installResult.trim());
+        Thread.sleep(5000);
     }
 
     private void uninstallApp(String pkg) throws InterruptedException {
-        String uninstallResult = SystemUtil.runShellCommand(
-                "pm uninstall " + pkg);
-        synchronized (mMySync) {
-            mMySync.wait(10000);
+        uninstallApp(pkg, false);
+    }
+
+    private void uninstallApp(String pkg, boolean cleanUp) throws InterruptedException {
+        String uninstallResult = SystemUtil.runShellCommand("pm uninstall " + pkg);
+        if (!cleanUp) {
+            assertEquals("Success", uninstallResult.trim());
+            Thread.sleep(5000);
         }
-        assertEquals("Success", uninstallResult.trim());
     }
 
     private void grantPermission(String pkg, String permission) {
@@ -134,7 +152,7 @@
 
     @SecurityTest
     @Test
-    public void permissionShouldBeRevokedIfRemoved() throws Throwable {
+    public void runtimePermissionShouldBeRevokedIfRemoved() throws Throwable {
         installApp(ADVERSARIAL_PERMISSION_DEFINER_APK_NAME);
         installApp(ADVERSARIAL_PERMISSION_USER_APK_NAME);
 
@@ -146,12 +164,10 @@
         uninstallApp(ADVERSARIAL_PERMISSION_DEFINER_PKG_NAME);
         installApp(VICTIM_PERMISSION_DEFINER_APK_NAME);
         assertFalse(permissionGranted(ADVERSARIAL_PERMISSION_USER_PKG_NAME, TEST_PERMISSION));
-        uninstallApp(ADVERSARIAL_PERMISSION_USER_PKG_NAME);
-        uninstallApp(VICTIM_PERMISSION_DEFINER_PKG_NAME);
     }
 
     @Test
-    public void permissionShouldRemainGrantedAfterAppUpdate() throws Throwable {
+    public void runtimePermissionShouldRemainGrantedAfterAppUpdate() throws Throwable {
         installApp(RUNTIME_PERMISSION_DEFINER_APK_NAME);
         installApp(RUNTIME_PERMISSION_USER_APK_NAME);
 
@@ -162,8 +178,6 @@
         // operation
         installApp(RUNTIME_PERMISSION_DEFINER_APK_NAME);
         assertTrue(permissionGranted(RUNTIME_PERMISSION_USER_PKG_NAME, TEST_RUNTIME_PERMISSION));
-        uninstallApp(RUNTIME_PERMISSION_USER_PKG_NAME);
-        uninstallApp(RUNTIME_PERMISSION_DEFINER_PKG_NAME);
     }
 
     @Test
@@ -183,26 +197,46 @@
         // Now uninstall the permission definer; the user packages' permission should be revoked
         uninstallApp(ADVERSARIAL_PERMISSION_DEFINER_PKG_NAME);
         assertFalse(permissionGranted(ADVERSARIAL_PERMISSION_USER_PKG_NAME, TEST_PERMISSION));
+    }
 
-        uninstallApp(ADVERSARIAL_PERMISSION_USER_PKG_NAME);
+    @SecurityTest
+    @Test
+    public void installPermissionShouldBeRevokedIfRemoved() throws Throwable {
+        installApp(INSTALL_PERMISSION_DEFINER_APK_NAME);
+        installApp(INSTALL_PERMISSION_USER_APK_NAME);
+        assertTrue(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
+
+        // Uninstall the app which defines the install permission, and install another app
+        // redefining it as a runtime permission.
+        uninstallApp(INSTALL_PERMISSION_DEFINER_PKG_NAME);
+        installApp(INSTALL_PERMISSION_ESCALATOR_APK_NAME);
+        assertFalse(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
     }
 
     @Test
-    public void installtimePermissionDependencyTest() throws Throwable {
-        installApp(INSTALLTIME_PERMISSION_USER_APK_NAME);
-        // Should not have the permission auto-granted
-        assertFalse(permissionGranted(
-                INSTALLTIME_PERMISSION_USER_PKG_NAME, TEST_INSTALLTIME_PERMISSION));
-        // Now install the permission definer; user package should have the permission auto granted
-        installApp(INSTALLTIME_PERMISSION_DEFINER_APK_NAME);
-        installApp(INSTALLTIME_PERMISSION_USER_APK_NAME);
-        assertTrue(permissionGranted(
-                INSTALLTIME_PERMISSION_USER_PKG_NAME, TEST_INSTALLTIME_PERMISSION));
-        // Now uninstall the permission definer; the user packages' permission will not be revoked
-        uninstallApp(INSTALLTIME_PERMISSION_DEFINER_PKG_NAME);
-        assertTrue(permissionGranted(
-                INSTALLTIME_PERMISSION_USER_PKG_NAME, TEST_INSTALLTIME_PERMISSION));
+    public void installPermissionShouldRemainGrantedAfterAppUpdate() throws Throwable {
+        installApp(INSTALL_PERMISSION_DEFINER_APK_NAME);
+        installApp(INSTALL_PERMISSION_USER_APK_NAME);
+        assertTrue(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
 
-        uninstallApp(INSTALLTIME_PERMISSION_USER_PKG_NAME);
+        // Install the app which defines the install permission again, similar to updating the app.
+        installApp(INSTALL_PERMISSION_DEFINER_APK_NAME);
+        assertTrue(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
+    }
+
+    @Test
+    public void installPermissionDependencyTest() throws Throwable {
+        installApp(INSTALL_PERMISSION_USER_APK_NAME);
+        // Should not have the permission auto-granted
+        assertFalse(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
+
+        // Now install the permission definer; user package should have the permission auto granted
+        installApp(INSTALL_PERMISSION_DEFINER_APK_NAME);
+        installApp(INSTALL_PERMISSION_USER_APK_NAME);
+        assertTrue(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
+
+        // Now uninstall the permission definer; the user package's permission should be revoked
+        uninstallApp(INSTALL_PERMISSION_DEFINER_PKG_NAME);
+        assertFalse(permissionGranted(INSTALL_PERMISSION_USER_PKG_NAME, TEST_INSTALL_PERMISSION));
     }
 }
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/Android.bp
similarity index 94%
rename from tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp
rename to tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/Android.bp
index 94fa99e..0679b6a 100644
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/Android.bp
@@ -14,7 +14,7 @@
 //
 
 android_test_helper_app {
-    name: "CtsInstalltimePermissionDefinerApp",
+    name: "CtsInstallPermissionDefinerApp",
     defaults: ["cts_defaults"],
     sdk_version: "current",
     // Tag this module as a cts test artifact
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/AndroidManifest.xml
similarity index 72%
copy from tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml
copy to tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/AndroidManifest.xml
index 7a0e405..2df6743 100644
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionDefinerApp/AndroidManifest.xml
@@ -15,11 +15,13 @@
  * limitations under the License.
  -->
 
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="android.permission.cts.revokepermissionwhenremoved.installtimepermissionuserapp">
+<manifest
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.permission.cts.revokepermissionwhenremoved.installpermissiondefinerapp">
 
-    <uses-permission android:name="android.permission.cts.revokepermissionwhenremoved.TestInstalltimePermission" />
+    <permission
+        android:name="android.permission.cts.revokepermissionwhenremoved.TestInstallPermission"
+        android:protectionLevel="normal" />
 
-    <application>
-    </application>
+    <application />
 </manifest>
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/Android.bp
similarity index 94%
copy from tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp
copy to tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/Android.bp
index 94fa99e..3f560de 100644
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/Android.bp
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/Android.bp
@@ -14,7 +14,7 @@
 //
 
 android_test_helper_app {
-    name: "CtsInstalltimePermissionDefinerApp",
+    name: "CtsInstallPermissionEscalatorApp",
     defaults: ["cts_defaults"],
     sdk_version: "current",
     // Tag this module as a cts test artifact
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/AndroidManifest.xml b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/AndroidManifest.xml
new file mode 100644
index 0000000..c339d12
--- /dev/null
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionEscalatorApp/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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="android.permission.cts.revokepermissionwhenremoved.installpermissionescalatorrapp">
+
+    <permission
+        android:name="android.permission.cts.revokepermissionwhenremoved.TestInstallPermission"
+        android:permissionGroup="android.permission-group.PHONE"
+        android:protectionLevel="dangerous" />
+
+    <application />
+</manifest>
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/Android.bp b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/Android.bp
similarity index 94%
rename from tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/Android.bp
rename to tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/Android.bp
index 6531cec..c75d64b 100644
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/Android.bp
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/Android.bp
@@ -14,7 +14,7 @@
 //
 
 android_test_helper_app {
-    name: "CtsInstalltimePermissionUserApp",
+    name: "CtsInstallPermissionUserApp",
     defaults: ["cts_defaults"],
     sdk_version: "current",
     // Tag this module as a cts test artifact
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/AndroidManifest.xml
similarity index 80%
rename from tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml
rename to tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/AndroidManifest.xml
index 7a0e405..acfafa9 100644
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionUserApp/AndroidManifest.xml
+++ b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstallPermissionUserApp/AndroidManifest.xml
@@ -15,11 +15,11 @@
  * limitations under the License.
  -->
 
-<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="android.permission.cts.revokepermissionwhenremoved.installtimepermissionuserapp">
+<manifest
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    package="android.permission.cts.revokepermissionwhenremoved.installpermissionuserapp">
 
-    <uses-permission android:name="android.permission.cts.revokepermissionwhenremoved.TestInstalltimePermission" />
+    <uses-permission android:name="android.permission.cts.revokepermissionwhenremoved.TestInstallPermission" />
 
-    <application>
-    </application>
+    <application />
 </manifest>
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/AndroidManifest.xml b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/AndroidManifest.xml
deleted file mode 100644
index 75ce567..0000000
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/AndroidManifest.xml
+++ /dev/null
@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- * Copyright (C) 2019 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="android.permission.cts.revokepermissionwhenremoved.installtimepermissiondefinerapp">
-
-    <permission android:name="android.permission.cts.revokepermissionwhenremoved.TestInstalltimePermission"
-        android:protectionLevel="normal"
-        android:label="TestInstalltimePermission"
-        android:description="@string/test_permission" />
-
-    <application>
-    </application>
-</manifest>
diff --git a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/res/values/strings.xml b/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/res/values/strings.xml
deleted file mode 100644
index 0943be7..0000000
--- a/tests/tests/permission/testapps/RevokePermissionWhenRemoved/InstalltimePermissionDefinerApp/res/values/strings.xml
+++ /dev/null
@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2019 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.
--->
-
-<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
-    <string name="test_permission">Test Installtime Permission</string>
-</resources>