Add tests for PackageManager.setInstallerPackageName().
Change-Id: I0b6c59b9aa93acd0f7172bd884138f704aea7dbe
diff --git a/tests/appsecurity-tests/test-apps/PermissionDeclareApp/AndroidManifest.xml b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/AndroidManifest.xml
index 683ec9e..de71966 100644
--- a/tests/appsecurity-tests/test-apps/PermissionDeclareApp/AndroidManifest.xml
+++ b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/AndroidManifest.xml
@@ -31,6 +31,8 @@
<application>
<receiver android:name="GrantUriPermission" android:exported="true">
</receiver>
+ <receiver android:name="SetInstallerPackage" android:exported="true">
+ </receiver>
<!-- Need a way for another app to try to access the permission. So create a content
provider which is enforced by the permission -->
diff --git a/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/GrantUriPermission.java b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/GrantUriPermission.java
index 8c14575..31a3f47 100644
--- a/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/GrantUriPermission.java
+++ b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/GrantUriPermission.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2010 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.permissiondeclareapp;
import android.content.BroadcastReceiver;
diff --git a/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/SetInstallerPackage.java b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/SetInstallerPackage.java
new file mode 100644
index 0000000..ea06d8c
--- /dev/null
+++ b/tests/appsecurity-tests/test-apps/PermissionDeclareApp/src/com/android/cts/permissiondeclareapp/SetInstallerPackage.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2010 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.permissiondeclareapp;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class SetInstallerPackage extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String targetPackage = intent.getStringExtra("target");
+ String installerPackage = intent.getStringExtra("installer");
+ try {
+ context.getPackageManager().setInstallerPackageName(targetPackage, installerPackage);
+ if (isOrderedBroadcast()) {
+ setResultCode(101);
+ }
+ } catch (SecurityException e) {
+ Log.i("SetInstallerPackage", "Security exception", e);
+ if (isOrderedBroadcast()) {
+ setResultCode(100);
+ }
+ }
+ }
+}
diff --git a/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/src/com/android/cts/usespermissiondiffcertapp/ModifyInstallerPackageTest.java b/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/src/com/android/cts/usespermissiondiffcertapp/ModifyInstallerPackageTest.java
new file mode 100644
index 0000000..989e24b
--- /dev/null
+++ b/tests/appsecurity-tests/test-apps/UsePermissionDiffCert/src/com/android/cts/usespermissiondiffcertapp/ModifyInstallerPackageTest.java
@@ -0,0 +1,212 @@
+/*
+ * 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.usespermissiondiffcertapp;
+
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.net.Uri;
+import android.os.SystemClock;
+import android.test.AndroidTestCase;
+import android.util.Log;
+
+/**
+ * Tests that one application can and can not modify the installer package
+ * of another application is appropriate.
+ *
+ * Accesses app cts/tests/appsecurity-tests/test-apps/PermissionDeclareApp/...
+ */
+public class ModifyInstallerPackageTest extends AndroidTestCase {
+ static final ComponentName SET_INSTALLER_PACKAGE_COMP
+ = new ComponentName("com.android.cts.permissiondeclareapp",
+ "com.android.cts.permissiondeclareapp.SetInstallerPackage");
+ static final String OTHER_PACKAGE = "com.android.cts.permissiondeclareapp";
+ static final String MY_PACKAGE = "com.android.cts.usespermissiondiffcertapp";
+
+ static class SetInstallerPackageReceiver extends BroadcastReceiver {
+ boolean mHaveResult = false;
+ boolean mGoodResult = false;
+ boolean mSucceeded = false;
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ synchronized (this) {
+ mHaveResult = true;
+ switch (getResultCode()) {
+ case 100:
+ mGoodResult = true;
+ mSucceeded = false;
+ break;
+ case 101:
+ mGoodResult = true;
+ mSucceeded = true;
+ break;
+ default:
+ mGoodResult = false;
+ break;
+ }
+ notifyAll();
+ }
+ }
+
+ void assertSuccess(String failureMessage) {
+ synchronized (this) {
+ final long startTime = SystemClock.uptimeMillis();
+ while (!mHaveResult) {
+ try {
+ wait(5000);
+ } catch (InterruptedException e) {
+ }
+ if (SystemClock.uptimeMillis() >= (startTime+5000)) {
+ throw new RuntimeException("Timeout");
+ }
+ }
+ if (!mGoodResult) {
+ fail("Broadcast receiver did not return good result");
+ }
+ if (!mSucceeded) {
+ fail(failureMessage);
+ }
+ }
+ }
+
+ void assertFailure(String failureMessage) {
+ synchronized (this) {
+ final long startTime = SystemClock.uptimeMillis();
+ while (!mHaveResult) {
+ try {
+ wait(5000);
+ } catch (InterruptedException e) {
+ }
+ if (SystemClock.uptimeMillis() >= (startTime+5000)) {
+ throw new RuntimeException("Timeout");
+ }
+ }
+ if (!mGoodResult) {
+ fail("Broadcast receiver did not return good result");
+ }
+ if (mSucceeded) {
+ fail(failureMessage);
+ }
+ }
+ }
+ }
+
+ PackageManager getPackageManager() {
+ return getContext().getPackageManager();
+ }
+
+ /**
+ * Test that we can set the installer package name.
+ */
+ public void testSetInstallPackage() {
+ // Pre-condition.
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ getPackageManager().setInstallerPackageName(OTHER_PACKAGE, MY_PACKAGE);
+ assertEquals(MY_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ // Clean up.
+ getPackageManager().setInstallerPackageName(OTHER_PACKAGE, null);
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+ }
+
+ /**
+ * Test that we fail if trying to set an installer package with an unknown
+ * target package name.
+ */
+ public void testSetInstallPackageBadTarget() {
+ try {
+ getPackageManager().setInstallerPackageName("thisdoesnotexistihope!", MY_PACKAGE);
+ fail("setInstallerPackageName did not throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // That's what we want!
+ }
+ }
+
+ /**
+ * Test that we fail if trying to set an installer package with an unknown
+ * installer package name.
+ */
+ public void testSetInstallPackageBadInstaller() {
+ try {
+ getPackageManager().setInstallerPackageName(OTHER_PACKAGE, "thisdoesnotexistihope!");
+ fail("setInstallerPackageName did not throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // That's what we want!
+ }
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+ }
+
+ /**
+ * Test that we fail if trying to set an installer package that is not
+ * signed with our cert.
+ */
+ public void testSetInstallPackageWrongCertificate() {
+ // Pre-condition.
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ try {
+ getPackageManager().setInstallerPackageName(OTHER_PACKAGE, OTHER_PACKAGE);
+ fail("setInstallerPackageName did not throw SecurityException");
+ } catch (SecurityException e) {
+ // That's what we want!
+ }
+
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+ }
+
+ /**
+ * Test that we fail if trying to set an installer package that is not
+ * signed with the same cert as the currently set installer.
+ */
+ public void testSetInstallPackageConflictingInstaller() {
+ // Pre-condition.
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ // Have the other package set the installer, under its cert.
+ Intent intent = new Intent();
+ intent.setComponent(SET_INSTALLER_PACKAGE_COMP);
+ intent.putExtra("target", OTHER_PACKAGE);
+ intent.putExtra("installer", OTHER_PACKAGE);
+ SetInstallerPackageReceiver receiver = new SetInstallerPackageReceiver();
+ getContext().sendOrderedBroadcast(intent, null, receiver, null, 0, null, null);
+ receiver.assertSuccess("Failure initializing with other installer");
+
+ assertEquals(OTHER_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ try {
+ getPackageManager().setInstallerPackageName(OTHER_PACKAGE, MY_PACKAGE);
+ fail("setInstallerPackageName did not throw SecurityException");
+ } catch (SecurityException e) {
+ // That's what we want!
+ }
+
+ assertEquals(OTHER_PACKAGE, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+
+ // Now clear the installer
+ intent.putExtra("target", OTHER_PACKAGE);
+ intent.putExtra("installer", (String)null);
+ receiver = new SetInstallerPackageReceiver();
+ getContext().sendOrderedBroadcast(intent, null, receiver, null, 0, null, null);
+ receiver.assertSuccess("Failure clearing other installer");
+
+ assertEquals(null, getPackageManager().getInstallerPackageName(OTHER_PACKAGE));
+ }
+}