Merge "Fix DeviceOwnerTest#testDeviceOwnerCannotGetDeviceIdentifiersWithoutPermission" into rvc-dev
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/DeviceIdentifiersTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/DeviceIdentifiersTest.java
index 855958c..ec02404 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/DeviceIdentifiersTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/DeviceIdentifiersTest.java
@@ -15,8 +15,6 @@
  */
 package com.android.cts.deviceandprofileowner;
 
-import static org.testng.Assert.assertThrows;
-
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.Build;
@@ -50,24 +48,24 @@
         try {
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getDeviceId"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getDeviceId()), telephonyManager.getDeviceId());
+                            TelephonyManager::getDeviceId), telephonyManager.getDeviceId());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getImei"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getImei()), telephonyManager.getImei());
+                            TelephonyManager::getImei), telephonyManager.getImei());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getMeid"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getMeid()), telephonyManager.getMeid());
+                            TelephonyManager::getMeid), telephonyManager.getMeid());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getSubscriberId"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getSubscriberId()), telephonyManager.getSubscriberId());
+                            TelephonyManager::getSubscriberId), telephonyManager.getSubscriberId());
             assertEquals(
                     String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getSimSerialNumber"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getSimSerialNumber()),
+                            TelephonyManager::getSimSerialNumber),
                     telephonyManager.getSimSerialNumber());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getNai"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getNai()), telephonyManager.getNai());
+                            TelephonyManager::getNai), telephonyManager.getNai());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "Build#getSerial"),
                     ShellIdentityUtils.invokeStaticMethodWithShellPermissions(Build::getSerial),
                     Build.getSerial());
@@ -90,7 +88,7 @@
         }
     }
 
-    public void testProfileOwnerCannotGetDeviceIdentifiersWithoutPermission() throws Exception {
+    public void testProfileOwnerCannotGetDeviceIdentifiersWithoutPermission() {
         // The profile owner without the READ_PHONE_STATE permission should still receive a
         // SecurityException when querying for device identifiers.
         TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
@@ -98,22 +96,33 @@
         // Allow the APIs to also return null if the telephony feature is not supported.
         boolean hasTelephonyFeature =
                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
-        if (hasTelephonyFeature) {
-            assertThrows(SecurityException.class, telephonyManager::getDeviceId);
-            assertThrows(SecurityException.class, telephonyManager::getImei);
-            assertThrows(SecurityException.class, telephonyManager::getMeid);
-            assertThrows(SecurityException.class, telephonyManager::getSubscriberId);
-            assertThrows(SecurityException.class, telephonyManager::getSimSerialNumber);
-            assertThrows(SecurityException.class, telephonyManager::getNai);
-            assertThrows(SecurityException.class, Build::getSerial);
-        } else {
-            assertNull(telephonyManager.getDeviceId());
-            assertNull(telephonyManager.getImei());
-            assertNull(telephonyManager.getMeid());
-            assertNull(telephonyManager.getSubscriberId());
-            assertNull(telephonyManager.getSimSerialNumber());
-            assertNull(telephonyManager.getNai());
-            assertNull(Build.getSerial());
+
+        boolean mayReturnNull = !hasTelephonyFeature;
+
+        assertAccessDenied(telephonyManager::getDeviceId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getImei, mayReturnNull);
+        assertAccessDenied(telephonyManager::getMeid, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSubscriberId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSimSerialNumber, mayReturnNull);
+        assertAccessDenied(telephonyManager::getNai, mayReturnNull);
+        assertAccessDenied(Build::getSerial, mayReturnNull);
+    }
+
+    private static <T> void assertAccessDenied(ThrowingProvider<T> provider,
+            boolean mayReturnNull) {
+        try {
+            T object = provider.get();
+            if (!mayReturnNull) {
+                assertNull(object);
+            }
+        } catch (SecurityException ignored) {
+            // assertion succeeded
+        } catch (Throwable th) {
+            fail("Expected SecurityException but was: " + th);
         }
     }
+
+    private interface ThrowingProvider<T> {
+        T get() throws Throwable;
+    }
 }
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/Android.bp b/hostsidetests/devicepolicy/app/DeviceOwner/Android.bp
index 34040d9..70d59d6 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/Android.bp
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/Android.bp
@@ -35,7 +35,6 @@
         "compatibility-device-util-axt",
         "androidx.test.rules",
         "cts-security-test-support-library",
-        "testng",
         "truth-prebuilt",
         "androidx.legacy_legacy-support-v4",
     ],
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/DeviceIdentifiersTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/DeviceIdentifiersTest.java
index 2370cd0b..2559294 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/DeviceIdentifiersTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/DeviceIdentifiersTest.java
@@ -15,8 +15,6 @@
  */
 package com.android.cts.deviceowner;
 
-import static org.testng.Assert.assertThrows;
-
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.Build;
@@ -35,7 +33,7 @@
             "An unexpected value was received by the device owner with the READ_PHONE_STATE "
                     + "permission when invoking %s";
 
-    public void testDeviceOwnerCanGetDeviceIdentifiersWithPermission() throws Exception {
+    public void testDeviceOwnerCanGetDeviceIdentifiersWithPermission() {
         // The device owner with the READ_PHONE_STATE permission should have access to all device
         // identifiers. However since the TelephonyManager methods can return null this method
         // verifies that the device owner with the READ_PHONE_STATE permission receives the same
@@ -45,25 +43,25 @@
         try {
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getDeviceId"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getDeviceId()), telephonyManager.getDeviceId());
+                            TelephonyManager::getDeviceId), telephonyManager.getDeviceId());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getImei"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getImei()), telephonyManager.getImei());
+                            TelephonyManager::getImei), telephonyManager.getImei());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getMeid"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getMeid()), telephonyManager.getMeid());
+                            TelephonyManager::getMeid), telephonyManager.getMeid());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getSubscriberId"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getSubscriberId()), telephonyManager.getSubscriberId());
+                            TelephonyManager::getSubscriberId), telephonyManager.getSubscriberId());
             assertEquals(
                     String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getSimSerialNumber"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getSimSerialNumber()),
+                            TelephonyManager::getSimSerialNumber),
                     telephonyManager.getSimSerialNumber());
             assertEquals(
                     String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "getNai"),
                     ShellIdentityUtils.invokeMethodWithShellPermissions(telephonyManager,
-                            (tm) -> tm.getNai()), telephonyManager.getNai());
+                            TelephonyManager::getNai), telephonyManager.getNai());
             assertEquals(String.format(DEVICE_ID_WITH_PERMISSION_ERROR_MESSAGE, "Build#getSerial"),
                     ShellIdentityUtils.invokeStaticMethodWithShellPermissions(Build::getSerial),
                     Build.getSerial());
@@ -86,7 +84,7 @@
         }
     }
 
-    public void testDeviceOwnerCannotGetDeviceIdentifiersWithoutPermission() throws Exception {
+    public void testDeviceOwnerCannotGetDeviceIdentifiersWithoutPermission() {
         // The device owner without the READ_PHONE_STATE permission should still receive a
         // SecurityException when querying for device identifiers.
         TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
@@ -94,22 +92,33 @@
         // Allow the APIs to also return null if the telephony feature is not supported.
         boolean hasTelephonyFeature =
                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
-        if (hasTelephonyFeature) {
-            assertThrows(SecurityException.class, telephonyManager::getDeviceId);
-            assertThrows(SecurityException.class, telephonyManager::getImei);
-            assertThrows(SecurityException.class, telephonyManager::getMeid);
-            assertThrows(SecurityException.class, telephonyManager::getSubscriberId);
-            assertThrows(SecurityException.class, telephonyManager::getSimSerialNumber);
-            assertThrows(SecurityException.class, telephonyManager::getNai);
-            assertThrows(SecurityException.class, Build::getSerial);
-        } else {
-            assertNull(telephonyManager.getDeviceId());
-            assertNull(telephonyManager.getImei());
-            assertNull(telephonyManager.getMeid());
-            assertNull(telephonyManager.getSubscriberId());
-            assertNull(telephonyManager.getSimSerialNumber());
-            assertNull(telephonyManager.getNai());
-            assertNull(Build.getSerial());
+
+        boolean mayReturnNull = !hasTelephonyFeature;
+
+        assertAccessDenied(telephonyManager::getDeviceId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getImei, mayReturnNull);
+        assertAccessDenied(telephonyManager::getMeid, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSubscriberId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSimSerialNumber, mayReturnNull);
+        assertAccessDenied(telephonyManager::getNai, mayReturnNull);
+        assertAccessDenied(Build::getSerial, mayReturnNull);
+    }
+
+    private static <T> void assertAccessDenied(ThrowingProvider<T> provider,
+            boolean mayReturnNull) {
+        try {
+            T object = provider.get();
+            if (!mayReturnNull) {
+                assertNull(object);
+            }
+        } catch (SecurityException ignored) {
+            // assertion succeeded
+        } catch (Throwable th) {
+            fail("Expected SecurityException but was: " + th);
         }
     }
+
+    private interface ThrowingProvider<T> {
+        T get() throws Throwable;
+    }
 }
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/DeviceIdentifiersTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/DeviceIdentifiersTest.java
index 68c4d3c..f2167c3 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/DeviceIdentifiersTest.java
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/DeviceIdentifiersTest.java
@@ -15,8 +15,6 @@
  */
 package com.android.cts.managedprofile;
 
-import static org.testng.Assert.assertThrows;
-
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.os.Build;
@@ -27,7 +25,7 @@
  */
 public class DeviceIdentifiersTest extends BaseManagedProfileTest {
 
-    public void testProfileOwnerOnPersonalDeviceCannotGetDeviceIdentifiers() throws Exception {
+    public void testProfileOwnerOnPersonalDeviceCannotGetDeviceIdentifiers() {
         // The profile owner with the READ_PHONE_STATE permission should still receive a
         // SecurityException when querying for device identifiers if it's not on an
         // organization-owned device.
@@ -36,22 +34,33 @@
         // Allow the APIs to also return null if the telephony feature is not supported.
         boolean hasTelephonyFeature =
                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
-        if (hasTelephonyFeature) {
-            assertThrows(SecurityException.class, telephonyManager::getDeviceId);
-            assertThrows(SecurityException.class, telephonyManager::getImei);
-            assertThrows(SecurityException.class, telephonyManager::getMeid);
-            assertThrows(SecurityException.class, telephonyManager::getSubscriberId);
-            assertThrows(SecurityException.class, telephonyManager::getSimSerialNumber);
-            assertThrows(SecurityException.class, telephonyManager::getNai);
-            assertThrows(SecurityException.class, Build::getSerial);
-        } else {
-            assertNull(telephonyManager.getDeviceId());
-            assertNull(telephonyManager.getImei());
-            assertNull(telephonyManager.getMeid());
-            assertNull(telephonyManager.getSubscriberId());
-            assertNull(telephonyManager.getSimSerialNumber());
-            assertNull(telephonyManager.getNai());
-            assertNull(Build.getSerial());
+
+        boolean mayReturnNull = !hasTelephonyFeature;
+
+        assertAccessDenied(telephonyManager::getDeviceId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getImei, mayReturnNull);
+        assertAccessDenied(telephonyManager::getMeid, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSubscriberId, mayReturnNull);
+        assertAccessDenied(telephonyManager::getSimSerialNumber, mayReturnNull);
+        assertAccessDenied(telephonyManager::getNai, mayReturnNull);
+        assertAccessDenied(Build::getSerial, mayReturnNull);
+    }
+
+    private static <T> void assertAccessDenied(ThrowingProvider<T> provider,
+            boolean mayReturnNull) {
+        try {
+            T object = provider.get();
+            if (!mayReturnNull) {
+                assertNull(object);
+            }
+        } catch (SecurityException ignored) {
+            // assertion succeeded
+        } catch (Throwable th) {
+            fail("Expected SecurityException but was: " + th);
         }
     }
+
+    private interface ThrowingProvider<T> {
+        T get() throws Throwable;
+    }
 }