From c718e54c27103ba690d99266b1d2f63fbe7f163d Mon Sep 17 00:00:00 2001 From: Michael Groover Date: Wed, 8 Jan 2020 17:42:48 -0800 Subject: [PATCH] Ensure adb key store is instantiated before revoking grants If adb is not enabled on a device then the adb key store is not instantiated when the AdbDebuggingManager's handler is started as in most cases it will not be needed. However if the user attempts to revoke any previous adb grants when a device is booted with adb disabled it will result in a NPE. This commit ensures the key store is instantiated before attempting to revoke authorizations. Test: atest AdbDebuggingManagerTest Test: Manually invoked 'Revoke USB debugging authorizations' after a boot with adb disabled. Fixes: 145790817 Change-Id: Id00e531c18412be009a211eec80a72867659608f Merged-In: Id00e531c18412be009a211eec80a72867659608f --- .../server/adb/AdbDebuggingManager.java | 5 ++++ .../server/adb/AdbDebuggingManagerTest.java | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/services/core/java/com/android/server/adb/AdbDebuggingManager.java b/services/core/java/com/android/server/adb/AdbDebuggingManager.java index 4b48ef917744b..143474bd5c94a 100644 --- a/services/core/java/com/android/server/adb/AdbDebuggingManager.java +++ b/services/core/java/com/android/server/adb/AdbDebuggingManager.java @@ -413,6 +413,11 @@ public class AdbDebuggingManager { case MESSAGE_ADB_CLEAR: { Slog.d(TAG, "Received a request to clear the adb authorizations"); mConnectedKeys.clear(); + // If the key store has not yet been instantiated then do so now; this avoids + // the unnecessary creation of the key store when adb is not enabled. + if (mAdbKeyStore == null) { + mAdbKeyStore = new AdbKeyStore(); + } mAdbKeyStore.deleteKeyStore(); cancelJobToUpdateAdbKeyStore(); break; diff --git a/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java b/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java index d4182f3d31e2d..5a1ad8655ab0d 100644 --- a/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/adb/AdbDebuggingManagerTest.java @@ -672,6 +672,30 @@ public final class AdbDebuggingManagerTest { connectionTime2, mKeyStore.getLastConnectionTime(TEST_KEY_2)); } + @Test + public void testClearAuthorizationsBeforeAdbEnabled() throws Exception { + // The adb key store is not instantiated until adb is enabled; however if the user attempts + // to clear the adb authorizations when adb is disabled after a boot a NullPointerException + // was thrown as deleteKeyStore is invoked against the key store. This test ensures the + // key store can be successfully cleared when adb is disabled. + mHandler = mManager.new AdbDebuggingHandler(FgThread.get().getLooper()); + + clearKeyStore(); + } + + @Test + public void testClearAuthorizationsDeletesKeyFiles() throws Exception { + mAdbKeyFile.createNewFile(); + mAdbKeyXmlFile.createNewFile(); + + clearKeyStore(); + + assertFalse("The adb key file should have been deleted after revocation of the grants", + mAdbKeyFile.exists()); + assertFalse("The adb xml key file should have been deleted after revocation of the grants", + mAdbKeyXmlFile.exists()); + } + /** * Runs an adb test with the provided configuration. *