Adjust testInstrumentationDiffCert to test that startInstrumentation throws
SecurityException directly.
Previously it tested for 'adb shell am instrument' failing, which can be
indeterministic.
internal bug 2476321
Change-Id: I4da274143d51ca7970bf0ff657cb1a7e7599abc0
diff --git a/tests/appsecurity-tests/src/com/android/cts/appsecurity/AppSecurityTests.java b/tests/appsecurity-tests/src/com/android/cts/appsecurity/AppSecurityTests.java
index 9862b8d..04572f5 100644
--- a/tests/appsecurity-tests/src/com/android/cts/appsecurity/AppSecurityTests.java
+++ b/tests/appsecurity-tests/src/com/android/cts/appsecurity/AppSecurityTests.java
@@ -166,18 +166,15 @@
getTestAppFilePath(TARGET_INSTRUMENT_APK), false);
assertNull("failed to install target instrumentation app", installResult);
- // the app will install, but will get error at runtime
+ // the app will install, but will get error at runtime when starting instrumentation
installResult = getDevice().installPackage(getTestAppFilePath(INSTRUMENT_DIFF_CERT_APK),
false);
assertNull("failed to install instrumentation app with diff cert", installResult);
- // run INSTRUMENT_DIFF_CERT_PKG tests - expect the test run to fail
- String runResults = runDeviceTestsWithRunResult(INSTRUMENT_DIFF_CERT_PKG);
- assertNotNull("running instrumentation with diff cert unexpectedly succeeded",
- runResults);
- String msg = String.format("Unexpected error message result from %s. Received %s",
- "instrumentation with diff cert. Expected starts with Permission Denial",
- runResults);
- assertTrue(msg, runResults.startsWith("Permission Denial"));
+ // run INSTRUMENT_DIFF_CERT_PKG tests
+ // this test will attempt to call startInstrumentation directly and verify
+ // SecurityException is thrown
+ assertTrue("running instrumentation with diff cert unexpectedly succeeded",
+ runDeviceTests(INSTRUMENT_DIFF_CERT_PKG));
}
finally {
getDevice().uninstallPackage(TARGET_INSTRUMENT_PKG);
@@ -246,17 +243,6 @@
return listener;
}
- /**
- * Helper method to run the specified packages tests, and return the test run error message.
- *
- * @param pkgName Android application package for tests
- * @return the test run error message or <code>null</code> if test run completed.
- */
- private String runDeviceTestsWithRunResult(String pkgName) {
- CollectingTestRunListener listener = doRunTests(pkgName);
- return listener.getTestRunErrorMessage();
- }
-
private static class CollectingTestRunListener implements ITestRunListener {
private boolean mAllTestsPassed = true;
diff --git a/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/AndroidManifest.xml b/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/AndroidManifest.xml
index e62f968..8102656 100644
--- a/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/AndroidManifest.xml
+++ b/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/AndroidManifest.xml
@@ -26,7 +26,15 @@
</application>
<!-- assumes com.android.cts.targetinstrumentationapp is signed with diff cert -->
- <instrumentation android:name="android.test.InstrumentationTestRunner"
+ <instrumentation android:name="android.app.Instrumentation"
android:targetPackage="com.android.cts.targetinstrumentationapp"
+ android:label="Instrumentation that targets app with different cert" />
+
+ <!--
+ A self-instrumenting test runner, that will try to start the above instrumentation and
+ verify it fails.
+ -->
+ <instrumentation android:name="android.test.InstrumentationTestRunner"
+ android:targetPackage="com.android.cts.instrumentationdiffcertapp"
android:label="Test for instrumentation with different cert" />
</manifest>
diff --git a/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/src/com/android/cts/instrumentationdiffcertapp/InstrumentationFailToRunTest.java b/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/src/com/android/cts/instrumentationdiffcertapp/InstrumentationFailToRunTest.java
index bd5de8c..9c320d9 100644
--- a/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/src/com/android/cts/instrumentationdiffcertapp/InstrumentationFailToRunTest.java
+++ b/tests/appsecurity-tests/test-apps/InstrumentationAppDiffCert/src/com/android/cts/instrumentationdiffcertapp/InstrumentationFailToRunTest.java
@@ -16,17 +16,29 @@
package com.android.cts.instrumentationdiffcertapp;
-import junit.framework.TestCase;
+import android.app.Instrumentation;
+import android.content.ComponentName;
+import android.content.Context;
+import android.os.Bundle;
+import android.test.InstrumentationTestCase;
/**
- * Test that is expected not to run
+ * Test that a instrumentation targeting another app with a different cert fails.
*/
-public class InstrumentationFailToRunTest extends TestCase {
+public class InstrumentationFailToRunTest extends InstrumentationTestCase {
- /**
- * Test method that is expected not to run.
- */
public void testInstrumentationNotAllowed() {
- fail("instrumentating app with different cert should fail");
+ Context myContext = getInstrumentation().getContext();
+ // assumes android.app.Instrumentation has been defined in this app's AndroidManifest.xml
+ // as targeting an app with a different cert
+ ComponentName appDiffCertInstrumentation = new ComponentName(myContext,
+ Instrumentation.class);
+ try {
+ getInstrumentation().getContext().startInstrumentation(appDiffCertInstrumentation,
+ null, new Bundle());
+ fail("could launch instrumentation");
+ } catch (SecurityException e) {
+ // expected
+ }
}
}