Add Checks For Modem Support of Null Ciphers to Telephony Service

When isNullCipherAndIntegrityPreferenceEnabled or
setNullCipherAndIntegrityEnabled are called, a check is added for
modem support of the feature. When the check fails, an
UnsupportedOperationException is thrown.

Bug: 264574296
Test: atest PhoneInterfaceManagerTest
Change-Id: I5a2e441c65873d4dc16675be864477d13c83508d
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index c4449d8..7bc5632 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -2429,7 +2429,11 @@
         return mTelephonySharedPreferences;
     }
 
-    private Phone getDefaultPhone() {
+    /**
+     * Get the default phone for this device.
+     */
+    @VisibleForTesting
+    public Phone getDefaultPhone() {
         Phone thePhone = getPhone(getDefaultSubscription());
         return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
     }
@@ -11743,6 +11747,10 @@
             throw new UnsupportedOperationException(
                     "Null cipher and integrity operations require HAL 2.1 or above");
         }
+        if (!getDefaultPhone().isNullCipherAndIntegritySupported()) {
+            throw new UnsupportedOperationException(
+                    "Null cipher and integrity operations unsupported by modem");
+        }
     }
 
     /**
diff --git a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
index 6e4a65f..b6d1087 100644
--- a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
+++ b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
@@ -149,6 +149,7 @@
 
     @Test
     public void setNullCipherAndIntegrityEnabled_successfullyEnable() {
+        whenModemSupportsNullCiphers();
         doReturn(201).when(mPhoneInterfaceManager).getHalVersion(anyInt());
         doNothing().when(mPhoneInterfaceManager).enforceModifyPermission();
         assertFalse(mSharedPreferences.contains(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED));
@@ -161,6 +162,7 @@
 
     @Test
     public void setNullCipherAndIntegrityEnabled_successfullyDisable() {
+        whenModemSupportsNullCiphers();
         doReturn(201).when(mPhoneInterfaceManager).getHalVersion(anyInt());
         doNothing().when(mPhoneInterfaceManager).enforceModifyPermission();
         assertFalse(mSharedPreferences.contains(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED));
@@ -194,10 +196,10 @@
 
     @Test
     public void isNullCipherAndIntegrityPreferenceEnabled() {
+        whenModemSupportsNullCiphers();
         doReturn(201).when(mPhoneInterfaceManager).getHalVersion(anyInt());
         doNothing().when(mPhoneInterfaceManager).enforceModifyPermission();
 
-        assertTrue(mPhoneInterfaceManager.isNullCipherAndIntegrityPreferenceEnabled());
         mPhoneInterfaceManager.setNullCipherAndIntegrityEnabled(false);
         assertFalse(
                 mSharedPreferences.getBoolean(Phone.PREF_NULL_CIPHER_AND_INTEGRITY_ENABLED, true));
@@ -215,6 +217,18 @@
     }
 
     @Test
+    public void isNullCipherAndIntegrityPreferenceEnabled_lackingModemSupport() {
+        whenModemDoesNotSupportNullCiphers();
+        doReturn(201).when(mPhoneInterfaceManager).getHalVersion(anyInt());
+        doNothing().when(mPhoneInterfaceManager).enforceModifyPermission();
+
+        assertThrows(UnsupportedOperationException.class, () -> {
+            mPhoneInterfaceManager.isNullCipherAndIntegrityPreferenceEnabled();
+        });
+
+    }
+
+    @Test
     public void isNullCipherAndIntegrityPreferenceEnabled_lackingPermissions() {
         doReturn(201).when(mPhoneInterfaceManager).getHalVersion(anyInt());
         doThrow(SecurityException.class).when(mPhoneInterfaceManager).enforceReadPermission();
@@ -223,4 +237,16 @@
             mPhoneInterfaceManager.isNullCipherAndIntegrityPreferenceEnabled();
         });
     }
+
+    private void whenModemDoesNotSupportNullCiphers() {
+        doReturn(false).when(mPhone).isNullCipherAndIntegritySupported();
+        doReturn(mPhone).when(
+                mPhoneInterfaceManager).getDefaultPhone();
+    }
+
+    private void whenModemSupportsNullCiphers() {
+        doReturn(true).when(mPhone).isNullCipherAndIntegritySupported();
+        doReturn(mPhone).when(
+                mPhoneInterfaceManager).getDefaultPhone();
+    }
 }