summaryrefslogtreecommitdiff
path: root/wifi
diff options
context:
space:
mode:
author Gabriel Biren <gbiren@google.com> 2024-09-13 19:11:26 +0000
committer Gabriel Biren <gbiren@google.com> 2024-09-13 21:37:25 +0000
commitd57a9813e638d81db32ef906c73f42b1c4ba050b (patch)
tree71bbac77dfd743edfbc140c1f2cf246b673f23b4 /wifi
parent56707cdda60dcb15df5989d19c8c387100ee2e07 (diff)
Return an status code from
WifiMigration#migrateLegacyKeystoreToWifiBlobstore. Bug: 358618927 Flag: android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only Test: atest WifiMigrationTest Change-Id: I310819dcb2d47286326d4f808efba9136ae5f5c0
Diffstat (limited to 'wifi')
-rw-r--r--wifi/java/src/android/net/wifi/WifiMigration.java49
-rw-r--r--wifi/tests/src/android/net/wifi/WifiMigrationTest.java40
2 files changed, 80 insertions, 9 deletions
diff --git a/wifi/java/src/android/net/wifi/WifiMigration.java b/wifi/java/src/android/net/wifi/WifiMigration.java
index 7df1d4b47204..f1850dd91b5f 100644
--- a/wifi/java/src/android/net/wifi/WifiMigration.java
+++ b/wifi/java/src/android/net/wifi/WifiMigration.java
@@ -100,6 +100,39 @@ public final class WifiMigration {
public @interface UserStoreFileId { }
/**
+ * Keystore migration was completed successfully.
+ * @hide
+ */
+ @FlaggedApi(Flags.FLAG_LEGACY_KEYSTORE_TO_WIFI_BLOBSTORE_MIGRATION_READ_ONLY)
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE = 0;
+
+ /**
+ * Keystore migration was not needed.
+ * @hide
+ */
+ @FlaggedApi(Flags.FLAG_LEGACY_KEYSTORE_TO_WIFI_BLOBSTORE_MIGRATION_READ_ONLY)
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED = 1;
+
+ /**
+ * Keystore migration failed because an exception was encountered.
+ * @hide
+ */
+ @FlaggedApi(Flags.FLAG_LEGACY_KEYSTORE_TO_WIFI_BLOBSTORE_MIGRATION_READ_ONLY)
+ @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
+ public static final int KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION = 2;
+
+ /** @hide */
+ @IntDef(prefix = { "KEYSTORE_MIGRATION_" }, value = {
+ KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE,
+ KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED,
+ KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface KeystoreMigrationStatus { }
+
+ /**
* Mapping of Store file Id to Store file names.
*
* NOTE: This is the default path for the files on AOSP devices. If the OEM has modified
@@ -572,14 +605,17 @@ public final class WifiMigration {
/**
* Migrate any certificates in Legacy Keystore to the newer WifiBlobstore database.
*
+ * If there are no certificates to migrate, this method will return immediately.
+ *
* @hide
*/
@FlaggedApi(Flags.FLAG_LEGACY_KEYSTORE_TO_WIFI_BLOBSTORE_MIGRATION_READ_ONLY)
@SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
- public static void migrateLegacyKeystoreToWifiBlobstore() {
+ public static @KeystoreMigrationStatus int migrateLegacyKeystoreToWifiBlobstore() {
if (!WifiBlobStore.supplicantCanAccessBlobstore()) {
+ // Supplicant cannot access WifiBlobstore, so keep the certs in Legacy Keystore
Log.i(TAG, "Avoiding migration since supplicant cannot access WifiBlobstore");
- return;
+ return KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED;
}
final long identity = Binder.clearCallingIdentity();
try {
@@ -587,7 +623,7 @@ public final class WifiMigration {
String[] legacyAliases = legacyKeystore.list("", Process.WIFI_UID);
if (legacyAliases == null || legacyAliases.length == 0) {
Log.i(TAG, "No aliases need to be migrated");
- return;
+ return KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED;
}
WifiBlobStore wifiBlobStore = WifiBlobStore.getInstance();
@@ -605,14 +641,17 @@ public final class WifiMigration {
legacyKeystore.remove(legacyAlias, Process.WIFI_UID);
}
Log.i(TAG, "Successfully migrated aliases from Legacy Keystore");
+ return KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE;
} catch (ServiceSpecificException e) {
if (e.errorCode == ILegacyKeystore.ERROR_SYSTEM_ERROR) {
Log.i(TAG, "Legacy Keystore service has been deprecated");
- } else {
- Log.e(TAG, "Encountered an exception while migrating aliases. " + e);
+ return KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED;
}
+ Log.e(TAG, "Encountered a ServiceSpecificException while migrating aliases. " + e);
+ return KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION;
} catch (Exception e) {
Log.e(TAG, "Encountered an exception while migrating aliases. " + e);
+ return KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION;
} finally {
Binder.restoreCallingIdentity(identity);
}
diff --git a/wifi/tests/src/android/net/wifi/WifiMigrationTest.java b/wifi/tests/src/android/net/wifi/WifiMigrationTest.java
index d95069d46879..0aa299f959fe 100644
--- a/wifi/tests/src/android/net/wifi/WifiMigrationTest.java
+++ b/wifi/tests/src/android/net/wifi/WifiMigrationTest.java
@@ -16,6 +16,7 @@
package android.net.wifi;
+import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
@@ -27,6 +28,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
+import android.os.RemoteException;
+import android.os.ServiceSpecificException;
import android.security.legacykeystore.ILegacyKeystore;
import com.android.dx.mockito.inline.extended.ExtendedMockito;
@@ -77,7 +80,8 @@ public class WifiMigrationTest {
@Test
public void testKeystoreMigrationAvoidedOnLegacyVendorPartition() {
when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(false);
- WifiMigration.migrateLegacyKeystoreToWifiBlobstore();
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
verifyNoMoreInteractions(mLegacyKeystore, mWifiBlobStore);
}
@@ -88,7 +92,8 @@ public class WifiMigrationTest {
@Test
public void testKeystoreMigrationNoLegacyAliases() throws Exception {
when(mLegacyKeystore.list(anyString(), anyInt())).thenReturn(new String[0]);
- WifiMigration.migrateLegacyKeystoreToWifiBlobstore();
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
verify(mLegacyKeystore).list(anyString(), anyInt());
verifyNoMoreInteractions(mLegacyKeystore, mWifiBlobStore);
}
@@ -104,7 +109,8 @@ public class WifiMigrationTest {
when(mLegacyKeystore.list(anyString(), anyInt())).thenReturn(legacyAliases);
when(mWifiBlobStore.list(anyString())).thenReturn(blobstoreAliases);
- WifiMigration.migrateLegacyKeystoreToWifiBlobstore();
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
verify(mWifiBlobStore, times(legacyAliases.length)).put(anyString(), any(byte[].class));
}
@@ -122,9 +128,35 @@ public class WifiMigrationTest {
when(mWifiBlobStore.list(anyString())).thenReturn(blobstoreAliases);
// Expect that only the unique legacy alias is migrated to the blobstore
- WifiMigration.migrateLegacyKeystoreToWifiBlobstore();
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
verify(mWifiBlobStore).list(anyString());
verify(mWifiBlobStore).put(eq(uniqueLegacyAlias), any(byte[].class));
verifyNoMoreInteractions(mWifiBlobStore);
}
+
+ /**
+ * Verify that the Keystore migration is skipped if Legacy Keystore is deprecated,
+ * since the migration is not needed.
+ */
+ @Test
+ public void testKeystoreMigrationAvoidedIfLegacyKsDeprecated() throws Exception {
+ // Legacy Keystore will throw a ServiceSpecificException with
+ // code ERROR_SYSTEM_ERROR if a method is deprecated
+ when(mLegacyKeystore.list(anyString(), anyInt())).thenThrow(
+ new ServiceSpecificException(ILegacyKeystore.ERROR_SYSTEM_ERROR));
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
+ }
+
+ /**
+ * Verify that the Keystore migration method returns a failure code when an
+ * unexpected exception is encountered.
+ */
+ @Test
+ public void testKeystoreMigrationFailsIfExceptionEncountered() throws Exception {
+ when(mLegacyKeystore.list(anyString(), anyInt())).thenThrow(new RemoteException());
+ assertEquals(WifiMigration.KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION,
+ WifiMigration.migrateLegacyKeystoreToWifiBlobstore());
+ }
}