From f7378376ca2ddc78a6183fe6024186c56707f84d Mon Sep 17 00:00:00 2001 From: Joshua Duong Date: Wed, 26 Feb 2020 08:22:12 -0800 Subject: [PATCH] Remove key from adb_keys if user forgets the key. It was being removed from adb_temp_keys.xml but not adb_keys. Bug: 150138676 Bug: 111434128 Test: atest AdbDebuggingManagerTest Change-Id: I3c31394e4480e787c771e193b179a472d6ee4a62 Merged-In: I3c31394e4480e787c771e193b179a472d6ee4a62 --- .../server/adb/AdbDebuggingManager.java | 1 + .../server/adb/AdbDebuggingManagerTest.java | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/services/core/java/com/android/server/adb/AdbDebuggingManager.java b/services/core/java/com/android/server/adb/AdbDebuggingManager.java index 10a2b91695e77..4a6c2bee2fc87 100644 --- a/services/core/java/com/android/server/adb/AdbDebuggingManager.java +++ b/services/core/java/com/android/server/adb/AdbDebuggingManager.java @@ -1849,6 +1849,7 @@ public class AdbDebuggingManager { public void removeKey(String key) { if (mKeyMap.containsKey(key)) { mKeyMap.remove(key); + writeKeys(mKeyMap.keySet()); sendPersistKeyStoreMessage(); } } 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..c687ede5fdf79 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,31 @@ public final class AdbDebuggingManagerTest { connectionTime2, mKeyStore.getLastConnectionTime(TEST_KEY_2)); } + @Test + public void testAdbKeyStore_removeKey() throws Exception { + // Accept the test key with the 'Always allow' option selected. + runAdbTest(TEST_KEY_1, true, true, false); + runAdbTest(TEST_KEY_2, true, true, false); + + // Set the connection time to 0 to restore the original behavior. + setAllowedConnectionTime(0); + + // Verify that the key is in the adb_keys file to ensure subsequent connections are + // automatically allowed by adbd. + persistKeyStore(); + assertTrue("The key was not in the adb_keys file after persisting the keystore", + isKeyInFile(TEST_KEY_1, mAdbKeyFile)); + assertTrue("The key was not in the adb_keys file after persisting the keystore", + isKeyInFile(TEST_KEY_2, mAdbKeyFile)); + + // Now remove one of the keys and make sure the other key is still there + mKeyStore.removeKey(TEST_KEY_1); + assertFalse("The key was still in the adb_keys file after removing the key", + isKeyInFile(TEST_KEY_1, mAdbKeyFile)); + assertTrue("The key was not in the adb_keys file after removing a different key", + isKeyInFile(TEST_KEY_2, mAdbKeyFile)); + } + /** * Runs an adb test with the provided configuration. *