Merge "KeyChain: Adopt to null return value"
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyChainTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyChainTestActivity.java
index fa88e32..d774823 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyChainTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/managedprovisioning/KeyChainTestActivity.java
@@ -208,6 +208,11 @@
             logStatus("Got right alias.");
             try {
                 PrivateKey privateKey = KeyChain.getPrivateKey(KeyChainTestActivity.this, alias);
+                if (privateKey == null) {
+                    logStatus("FAILED (key unavailable)");
+                    return;
+                }
+
                 byte[] data = new String("hello").getBytes();
                 Signature sign = Signature.getInstance("SHA256withRSA");
                 sign.initSign(privateKey);
diff --git a/hostsidetests/devicepolicy/app/CertInstaller/src/com/android/cts/certinstaller/DirectDelegatedCertInstallerTest.java b/hostsidetests/devicepolicy/app/CertInstaller/src/com/android/cts/certinstaller/DirectDelegatedCertInstallerTest.java
index 59d386e..97ff034 100644
--- a/hostsidetests/devicepolicy/app/CertInstaller/src/com/android/cts/certinstaller/DirectDelegatedCertInstallerTest.java
+++ b/hostsidetests/devicepolicy/app/CertInstaller/src/com/android/cts/certinstaller/DirectDelegatedCertInstallerTest.java
@@ -184,8 +184,7 @@
 
         // Test cleaning up the key.
         assertThat(mDpm.removeKeyPair(null, alias)).isTrue();
-        assertThrows(
-                KeyChainException.class, () -> KeyChain.getPrivateKey(getContext(), alias));
+        assertThat(KeyChain.getPrivateKey(getContext(), alias)).isNull();
     }
 
     // Test that a key generation request succeeds when device identifiers are not requested.
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/KeyManagementTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/KeyManagementTest.java
index 85b2903..302523b 100755
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/KeyManagementTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/KeyManagementTest.java
@@ -715,15 +715,9 @@
         }
     }
 
-    private void assertGranted(String alias, boolean expected) throws InterruptedException {
-        boolean granted = false;
-        try {
-            granted = (KeyChain.getPrivateKey(mActivity, alias) != null);
-        } catch (KeyChainException e) {
-            if (expected) {
-                e.printStackTrace();
-            }
-        }
+    private void assertGranted(String alias, boolean expected)
+            throws InterruptedException, KeyChainException {
+        boolean granted = (KeyChain.getPrivateKey(mActivity, alias) != null);
         assertWithMessage("Grant for alias: \"" + alias + "\"").that(granted).isEqualTo(expected);
     }