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

Commit e066965c authored by Hansen Kurli's avatar Hansen Kurli
Browse files

Implement ConnectivityBlobStore get and put.

Implement get() and put(), including tests.

Bug: 307903113
Test: atest ConnectivityBlobStoreTest
Change-Id: Id396b0e266475fdc9476eff642b7f15d3fc581d9
parent be7b2af7
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
@@ -16,7 +16,13 @@

package com.android.internal.net;

import android.annotation.NonNull;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Binder;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;

@@ -58,4 +64,55 @@ public class ConnectivityBlobStore {
        mDb = SQLiteDatabase.openDatabase(file, params);
        mDb.execSQL(CREATE_TABLE);
    }

    /**
     * Stores the blob under the name in the database. Existing blobs by the same name will be
     * replaced.
     *
     * @param name The name of the blob
     * @param blob The blob.
     * @return true if the blob was successfully added. False otherwise.
     * @hide
     */
    public boolean put(@NonNull String name, @NonNull byte[] blob) {
        final int ownerUid = Binder.getCallingUid();
        final ContentValues values = new ContentValues();
        values.put("owner", ownerUid);
        values.put("name", name);
        values.put("blob", blob);

        // No need for try-catch since it is done within db.replace
        // nullColumnHack is for the case where values may be empty since SQL does not allow
        // inserting a completely empty row. Since values is never empty, set this to null.
        final long res = mDb.replace(TABLENAME, null /* nullColumnHack */, values);
        return res > 0;
    }

    /**
     * Retrieves a blob by the name from the database.
     *
     * @param name Name of the blob to retrieve.
     * @return The unstructured blob, that is the blob that was stored using
     *         {@link com.android.internal.net.ConnectivityBlobStore#put}.
     *         Returns null if no blob was found.
     * @hide
     */
    public byte[] get(@NonNull String name) {
        final int ownerUid = Binder.getCallingUid();
        try (Cursor cursor = mDb.query(TABLENAME,
                new String[] {"blob"} /* columns */,
                "owner=? AND name=?" /* selection */,
                new String[] {Integer.toString(ownerUid), name} /* selectionArgs */,
                null /* groupBy */,
                null /* having */,
                null /* orderBy */)) {
            if (cursor.moveToFirst()) {
                return cursor.getBlob(0);
            }
        } catch (SQLException e) {
            Log.e(TAG, "Error in getting " + name + ": " + e);
        }

        return null;
    }
}
+18 −0
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package com.android.internal.net;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import android.content.Context;
@@ -36,6 +38,8 @@ import java.io.File;
@SmallTest
public class ConnectivityBlobStoreTest {
    private static final String DATABASE_FILENAME = "ConnectivityBlobStore.db";
    private static final String TEST_NAME = "TEST_NAME";
    private static final byte[] TEST_BLOB = new byte[] {(byte) 10, (byte) 90, (byte) 45, (byte) 12};

    private Context mContext;
    private File mFile;
@@ -64,4 +68,18 @@ public class ConnectivityBlobStoreTest {
        assertTrue(mContext.deleteDatabase(DATABASE_FILENAME));
        assertFalse(mFile.exists());
    }

    @Test
    public void testPutAndGet() throws Exception {
        final ConnectivityBlobStore connectivityBlobStore = createConnectivityBlobStore();
        assertNull(connectivityBlobStore.get(TEST_NAME));

        assertTrue(connectivityBlobStore.put(TEST_NAME, TEST_BLOB));
        assertArrayEquals(TEST_BLOB, connectivityBlobStore.get(TEST_NAME));

        // Test replacement
        final byte[] newBlob = new byte[] {(byte) 15, (byte) 20};
        assertTrue(connectivityBlobStore.put(TEST_NAME, newBlob));
        assertArrayEquals(newBlob, connectivityBlobStore.get(TEST_NAME));
    }
}