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

Commit e1a4326f authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Update WifiKeystore and WifiMigration behavior when the supplicant...

Merge "Update WifiKeystore and WifiMigration behavior when the supplicant cannot access WifiBlobstore." into main
parents 2e2e0679 f6823e71
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ import java.util.Set;
@SuppressLint("UnflaggedApi") // Promoting from @SystemApi(MODULE_LIBRARIES)
public final class WifiKeystore {
    private static final String TAG = "WifiKeystore";
    private static final String sPrimaryDbName =
            WifiBlobStore.supplicantCanAccessBlobstore() ? "WifiBlobstore" : "LegacyKeystore";

    /** @hide */
    WifiKeystore() {
@@ -57,8 +59,13 @@ public final class WifiKeystore {
        // are able to access the same values.
        final long identity = Binder.clearCallingIdentity();
        try {
            Log.i(TAG, "put blob. alias " + alias);
            Log.i(TAG, "put blob. alias=" + alias + ", primaryDb=" + sPrimaryDbName);
            if (WifiBlobStore.supplicantCanAccessBlobstore()) {
                return WifiBlobStore.getInstance().put(alias, blob);
            } else {
                WifiBlobStore.getLegacyKeystore().put(alias, Process.WIFI_UID, blob);
                return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "Failed to put blob.", e);
            return false;
@@ -80,7 +87,7 @@ public final class WifiKeystore {
    public static @NonNull byte[] get(@NonNull String alias) {
        final long identity = Binder.clearCallingIdentity();
        try {
            Log.i(TAG, "get blob. alias " + alias);
            Log.i(TAG, "get blob. alias=" + alias + ", primaryDb=" + sPrimaryDbName);
            byte[] blob = WifiBlobStore.getInstance().get(alias);
            if (blob != null) {
                return blob;
@@ -112,7 +119,7 @@ public final class WifiKeystore {
        boolean legacyKsSuccess = false;
        final long identity = Binder.clearCallingIdentity();
        try {
            Log.i(TAG, "remove blob. alias " + alias);
            Log.i(TAG, "remove blob. alias=" + alias + ", primaryDb=" + sPrimaryDbName);
            blobStoreSuccess = WifiBlobStore.getInstance().remove(alias);
            // Legacy Keystore will throw an exception if the alias is not found.
            WifiBlobStore.getLegacyKeystore().remove(alias, Process.WIFI_UID);
+4 −0
Original line number Diff line number Diff line
@@ -577,6 +577,10 @@ public final class WifiMigration {
    @FlaggedApi(Flags.FLAG_LEGACY_KEYSTORE_TO_WIFI_BLOBSTORE_MIGRATION_READ_ONLY)
    @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
    public static void migrateLegacyKeystoreToWifiBlobstore() {
        if (!WifiBlobStore.supplicantCanAccessBlobstore()) {
            Log.i(TAG, "Avoiding migration since supplicant cannot access WifiBlobstore");
            return;
        }
        final long identity = Binder.clearCallingIdentity();
        try {
            ILegacyKeystore legacyKeystore = WifiBlobStore.getLegacyKeystore();
+17 −2
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ public class WifiKeystoreTest {
        mSession = ExtendedMockito.mockitoSession()
                .mockStatic(WifiBlobStore.class, withSettings().lenient())
                .startMocking();
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(true);
        when(WifiBlobStore.getLegacyKeystore()).thenReturn(mLegacyKeystore);
        when(WifiBlobStore.getInstance()).thenReturn(mWifiBlobStore);
    }
@@ -74,15 +75,29 @@ public class WifiKeystoreTest {
    }

    /**
     * Test that put() only writes to the WifiBlobStore database.
     * Test that put() writes to the WifiBlobStore database when it
     * is available to supplicant.
     */
    @Test
    public void testPut() throws Exception {
    public void testPut_wifiBlobstore() throws Exception {
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(true);
        WifiKeystore.put(TEST_ALIAS, TEST_VALUE);
        verify(mWifiBlobStore).put(anyString(), any());
        verify(mLegacyKeystore, never()).put(anyString(), anyInt(), any());
    }

    /**
     * Test that put() writes to Legacy Keystore if the WifiBlobstore database
     * is not available to supplicant.
     */
    @Test
    public void testPut_legacyKeystore() throws Exception {
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(false);
        WifiKeystore.put(TEST_ALIAS, TEST_VALUE);
        verify(mLegacyKeystore).put(anyString(), anyInt(), any());
        verify(mWifiBlobStore, never()).put(anyString(), any());
    }

    /**
     * Test that if the alias is found in the WifiBlobStore database,
     * then the legacy database is not searched.
+12 −0
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ public class WifiMigrationTest {
        mSession = ExtendedMockito.mockitoSession()
                .mockStatic(WifiBlobStore.class, withSettings().lenient())
                .startMocking();
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(true);
        when(WifiBlobStore.getLegacyKeystore()).thenReturn(mLegacyKeystore);
        when(WifiBlobStore.getInstance()).thenReturn(mWifiBlobStore);
        when(mLegacyKeystore.get(anyString(), anyInt())).thenReturn(TEST_VALUE);
@@ -69,6 +70,17 @@ public class WifiMigrationTest {
        }
    }

    /**
     * Verify that the Keystore migration is skipped if supplicant does not have
     * access to the WifiBlobstore database.
     */
    @Test
    public void testKeystoreMigrationAvoidedOnLegacyVendorPartition() {
        when(WifiBlobStore.supplicantCanAccessBlobstore()).thenReturn(false);
        WifiMigration.migrateLegacyKeystoreToWifiBlobstore();
        verifyNoMoreInteractions(mLegacyKeystore, mWifiBlobStore);
    }

    /**
     * Verify that the Keystore migration method returns immediately if no aliases
     * are found in Legacy Keystore.