Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d57a9813 authored by Gabriel Biren's avatar Gabriel Biren
Browse files

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
parent 56707cdd
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -321,7 +321,10 @@ package android.net.netstats {
package android.net.wifi {

  public final class WifiMigration {
    method @FlaggedApi("android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only") public static void migrateLegacyKeystoreToWifiBlobstore();
    method @FlaggedApi("android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only") public static int migrateLegacyKeystoreToWifiBlobstore();
    field @FlaggedApi("android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only") public static final int KEYSTORE_MIGRATION_FAILURE_ENCOUNTERED_EXCEPTION = 2; // 0x2
    field @FlaggedApi("android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only") public static final int KEYSTORE_MIGRATION_SUCCESS_MIGRATION_COMPLETE = 0; // 0x0
    field @FlaggedApi("android.net.wifi.flags.legacy_keystore_to_wifi_blobstore_migration_read_only") public static final int KEYSTORE_MIGRATION_SUCCESS_MIGRATION_NOT_NEEDED = 1; // 0x1
  }

}
+44 −5
Original line number Diff line number Diff line
@@ -99,6 +99,39 @@ public final class WifiMigration {
    @Retention(RetentionPolicy.SOURCE)
    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.
     *
@@ -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);
        }
+36 −4
Original line number Diff line number Diff line
@@ -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());
    }
}