diff options
author | Thomas Stuart <tjstuart@google.com> | 2023-02-27 21:33:19 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2023-02-27 21:33:19 +0000 |
commit | 1cf3b0987bb7775db009ff5f8e05d619f9084a36 (patch) | |
tree | 6357bd676a806fd75e26280d4013a2fd0cc1d5fe | |
parent | eeda10d7966f8bff381e5571ba7e9571b1d9acdf (diff) | |
parent | 461d139b22a81a80102a8ae8a42b0b84287f2756 (diff) | |
download | cts-1cf3b0987bb7775db009ff5f8e05d619f9084a36.tar.gz |
Merge "enforce stricter rules when registering phoneAccounts" into sc-qpr1-dev
-rw-r--r-- | tests/tests/telecom/src/android/telecom/cts/PhoneAccountRegistrarTest.java | 103 | ||||
-rw-r--r-- | tests/tests/telecom/src/android/telecom/cts/TestUtils.java | 60 |
2 files changed, 155 insertions, 8 deletions
diff --git a/tests/tests/telecom/src/android/telecom/cts/PhoneAccountRegistrarTest.java b/tests/tests/telecom/src/android/telecom/cts/PhoneAccountRegistrarTest.java index f52b183875d..dcb83d3728b 100644 --- a/tests/tests/telecom/src/android/telecom/cts/PhoneAccountRegistrarTest.java +++ b/tests/tests/telecom/src/android/telecom/cts/PhoneAccountRegistrarTest.java @@ -47,12 +47,12 @@ public class PhoneAccountRegistrarTest extends BaseTelecomTestWithMockServices { private static final String RANDOM_CHAR_VALUE = "a"; private static final String TEL_PREFIX = "tel:"; private static final String TELECOM_CLEANUP_ACCTS_CMD = "telecom cleanup-orphan-phone-accounts"; - + public static final long SEED = 52L; // random seed chosen public static final int MAX_PHONE_ACCOUNT_REGISTRATIONS = 10; // mirrors constant in... // PhoneAccountRegistrar called MAX_PHONE_ACCOUNT_REGISTRATIONS // permissions - private static final String READ_PHONE_STATE_PERMISSION = + private static final String READ_PRIVILEGED_PHONE_STATE = "android.permission.READ_PRIVILEGED_PHONE_STATE"; private static final String MODIFY_PHONE_STATE_PERMISSION = "android.permission.MODIFY_PHONE_STATE"; @@ -95,6 +95,86 @@ public class PhoneAccountRegistrarTest extends BaseTelecomTestWithMockServices { } /** + * Ensure an app does not register accounts over the upper bound limit by disabling them + */ + public void testDisablingAccountsAfterRegStillThrowsException() throws Exception { + if (!mShouldTestTelecom) return; + + // ensure the test starts without any phone accounts registered to the test package + cleanupPhoneAccounts(); + + // Create MAX_PHONE_ACCOUNT_REGISTRATIONS + 1 via helper function + ArrayList<PhoneAccount> accounts = TestUtils.generateRandomPhoneAccounts(SEED, + MAX_PHONE_ACCOUNT_REGISTRATIONS + 1, TestUtils.PACKAGE, + TestUtils.COMPONENT); + + try { + // Try to register more phone accounts than allowed by the upper bound limit + for (PhoneAccount pa : accounts) { + mTelecomManager.registerPhoneAccount(pa); + TestUtils.disablePhoneAccount(getInstrumentation(), pa.getAccountHandle()); + // verify the account is both registered and disabled + verifyAccountIsDisabled(pa); + } + + // A successful test should never reach this line of execution. + // However, if it does, fail the test by throwing a fail(...) + fail("Test failed. The test did not throw an IllegalArgumentException when " + + "registering phone accounts over the upper bound: " + + "MAX_PHONE_ACCOUNT_REGISTRATIONS"); + } catch (IllegalArgumentException e) { + // Assert the IllegalArgumentException was thrown + assertNotNull(e.toString()); + } finally { + // Cleanup accounts registered + accounts.stream().forEach(d -> mTelecomManager.unregisterPhoneAccount( + d.getAccountHandle())); + } + } + + /** + * Ensure an app does not register accounts that will be auto-disabled upon registered and + * bypass the limit. Note: CAPABILITY_CALL_PROVIDER will register the account as disabled. + */ + public void testDisabledAccountsThrowsException() throws Exception { + if (!mShouldTestTelecom) return; + + // ensure the test starts without any phone accounts registered to the test package + cleanupPhoneAccounts(); + + // Create MAX_PHONE_ACCOUNT_REGISTRATIONS + 1 + ArrayList<PhoneAccount> accounts = new ArrayList<>(); + for (int i = 0; i < MAX_PHONE_ACCOUNT_REGISTRATIONS + 1; i++) { + accounts.add(new PhoneAccount.Builder( + TestUtils.makePhoneAccountHandle(Integer.toString(i)), + TestUtils.ACCOUNT_LABEL) + .setCapabilities(CAPABILITY_CALL_PROVIDER) + .build()); + } + + try { + // Try to register more phone accounts than allowed by the upper bound limit + for (PhoneAccount pa : accounts) { + mTelecomManager.registerPhoneAccount(pa); + // verify the account is both registered and disabled + verifyAccountIsDisabled(pa); + } + // A successful test should never reach this line of execution. + // However, if it does, fail the test by throwing a fail(...) + fail("Test failed. The test did not throw an IllegalArgumentException when " + + "registering phone accounts over the upper bound: " + + "MAX_PHONE_ACCOUNT_REGISTRATIONS"); + } catch (IllegalArgumentException e) { + // Assert the IllegalArgumentException was thrown + assertNotNull(e.toString()); + } finally { + // Cleanup accounts registered + accounts.stream().forEach(d -> mTelecomManager.unregisterPhoneAccount( + d.getAccountHandle())); + } + } + + /** * Test the scenario where {@link android.telecom.TelecomManager * #getCallCapablePhoneAccounts(boolean)} is called with a heavy payload * that could cause a {@link android.os.TransactionTooLargeException}. Telecom is expected to @@ -338,6 +418,12 @@ public class PhoneAccountRegistrarTest extends BaseTelecomTestWithMockServices { } } + public void verifyAccountIsDisabled(PhoneAccount account) { + PhoneAccount phoneAccount = mTelecomManager.getPhoneAccount(account.getAccountHandle()); + assertNotNull(phoneAccount); + assertFalse(phoneAccount.isEnabled()); + } + public void verifyCanFetchCallCapableAccounts() { List<PhoneAccountHandle> res = mTelecomManager.getCallCapablePhoneAccounts(true); @@ -428,9 +514,10 @@ public class PhoneAccountRegistrarTest extends BaseTelecomTestWithMockServices { if (mTelecomManager != null) { // Get all handles registered to the testing package List<PhoneAccountHandle> handles = - ShellIdentityUtils.invokeMethodWithShellPermissions( - mTelecomManager, (tm) -> tm.getPhoneAccountsForPackage(), - READ_PHONE_STATE_PERMISSION); + ShellIdentityUtils.invokeMethodWithShellPermissions(mTelecomManager, + (tm) -> tm.getPhoneAccountsForPackage(), + READ_PRIVILEGED_PHONE_STATE); + // cleanup any extra phone accounts registered to the testing package if (handles.size() > 0 && mTelecomManager != null) { handles.stream().forEach( @@ -452,9 +539,9 @@ public class PhoneAccountRegistrarTest extends BaseTelecomTestWithMockServices { */ private int getNumberOfPhoneAccountsRegisteredToTestPackage() { if (mTelecomManager != null) { - return ShellIdentityUtils.invokeMethodWithShellPermissions( - mTelecomManager, (tm) -> tm.getPhoneAccountsForPackage(), - READ_PHONE_STATE_PERMISSION).size(); + return ShellIdentityUtils.invokeMethodWithShellPermissions(mTelecomManager, + (tm) -> tm.getPhoneAccountsForPackage(), + READ_PRIVILEGED_PHONE_STATE).size(); } return 0; } diff --git a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java index e4f552635b7..4b395ca5d5d 100644 --- a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java +++ b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java @@ -49,6 +49,8 @@ import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Optional; +import java.util.Random; +import java.util.UUID; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.function.Predicate; @@ -79,6 +81,8 @@ public class TestUtils { public static final String ACCOUNT_ID_SIM = "sim_acct"; public static final String ACCOUNT_ID_EMERGENCY = "xtstest_CALL_PROVIDER_EMERGENCY"; public static final String EXTRA_PHONE_NUMBER = "android.telecom.cts.extra.PHONE_NUMBER"; + public static final ComponentName TELECOM_CTS_COMPONENT_NAME = new ComponentName( + TestUtils.PACKAGE, TestUtils.COMPONENT); public static final PhoneAccountHandle TEST_PHONE_ACCOUNT_HANDLE = new PhoneAccountHandle(new ComponentName(PACKAGE, COMPONENT), ACCOUNT_ID_1); public static final PhoneAccountHandle TEST_SIM_PHONE_ACCOUNT_HANDLE = @@ -320,6 +324,8 @@ public class TestUtils { private static final String COMMAND_ENABLE = "telecom set-phone-account-enabled "; + private static final String COMMAND_DISABLE = "telecom set-phone-account-disabled "; + private static final String COMMAND_SET_ACCT_SUGGESTION = "telecom set-phone-acct-suggestion-component "; @@ -401,6 +407,15 @@ public class TestUtils { + handle.getId() + " " + currentUserSerial); } + public static void disablePhoneAccount(Instrumentation instrumentation, + PhoneAccountHandle handle) throws Exception { + final ComponentName component = handle.getComponentName(); + final long currentUserSerial = getCurrentUserSerialNumber(instrumentation); + executeShellCommand(instrumentation, COMMAND_DISABLE + + component.getPackageName() + "/" + component.getClassName() + " " + + handle.getId() + " " + currentUserSerial); + } + public static void registerSimPhoneAccount(Instrumentation instrumentation, PhoneAccountHandle handle, String label, String address) throws Exception { final ComponentName component = handle.getComponentName(); @@ -817,4 +832,49 @@ public class TestUtils { public static int deleteContact(ContentResolver contentResolver, Uri deleteUri) { return contentResolver.delete(deleteUri, null, null); } + + /** + * Generates random phone accounts. + * @param seed random seed to use for random UUIDs; passed in for determinism. + * @param count How many phone accounts to use. + * @return Random phone accounts. + */ + public static ArrayList<PhoneAccount> generateRandomPhoneAccounts(long seed, int count, + String packageName, String component) { + Random random = new Random(seed); + ArrayList<PhoneAccount> accounts = new ArrayList<>(); + for (int ix = 0; ix < count; ix++) { + PhoneAccountHandle handle = new PhoneAccountHandle( + new ComponentName(packageName, component), getRandomUuid(random).toString()); + PhoneAccount acct = new PhoneAccount.Builder(handle, "TelecommTests") + .setAddress(Uri.parse("sip:test@test.com")) + .setSubscriptionAddress(Uri.parse("sip:test@test.com")) + .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED + | PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING + | PhoneAccount.CAPABILITY_VIDEO_CALLING) + .setHighlightColor(Color.BLUE) + .setShortDescription(TestUtils.SELF_MANAGED_ACCOUNT_LABEL) + .addSupportedUriScheme(PhoneAccount.SCHEME_TEL) + .addSupportedUriScheme(PhoneAccount.SCHEME_SIP) + .setExtras(TestUtils.SELF_MANAGED_ACCOUNT_1_EXTRAS) + .build(); + accounts.add(acct); + } + return accounts; + } + + /** + * Returns a random UUID based on the passed in Random generator. + * @param random Random generator. + * @return The UUID. + */ + public static UUID getRandomUuid(Random random) { + byte[] array = new byte[16]; + random.nextBytes(array); + return UUID.nameUUIDFromBytes(array); + } + + public static PhoneAccountHandle makePhoneAccountHandle(String id) { + return new PhoneAccountHandle(TELECOM_CTS_COMPONENT_NAME, id); + } } |