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

Commit 6739c333 authored by Hansen Kurli's avatar Hansen Kurli Committed by Automerger Merge Worker
Browse files

Merge "Initialize ConnectivityBlobStore and ConnectivityBlobStoreTest" into...

Merge "Initialize ConnectivityBlobStore and ConnectivityBlobStoreTest" into main am: 5dca2aa3 am: 436a9d20

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/2836715



Change-Id: Ia4e70d4e70056c7a2f13565ec8eefcd9c2e7ab2f
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents e7a0c775 436a9d20
Loading
Loading
Loading
Loading
+61 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.net;

import android.database.sqlite.SQLiteDatabase;

import com.android.internal.annotations.VisibleForTesting;

import java.io.File;

/**
 * Database for storing blobs with a key of name strings.
 * @hide
 */
public class ConnectivityBlobStore {
    private static final String TAG = ConnectivityBlobStore.class.getSimpleName();
    private static final String TABLENAME = "blob_table";
    private static final String ROOT_DIR = "/data/misc/connectivityblobdb/";

    private static final String CREATE_TABLE =
            "CREATE TABLE IF NOT EXISTS " + TABLENAME + " ("
            + "owner INTEGER,"
            + "name BLOB,"
            + "blob BLOB,"
            + "UNIQUE(owner, name));";

    private final SQLiteDatabase mDb;

    /**
     * Construct a ConnectivityBlobStore object.
     *
     * @param dbName the filename of the database to create/access.
     */
    public ConnectivityBlobStore(String dbName) {
        this(new File(ROOT_DIR + dbName));
    }

    @VisibleForTesting
    public ConnectivityBlobStore(File file) {
        final SQLiteDatabase.OpenParams params = new SQLiteDatabase.OpenParams.Builder()
                .addOpenFlags(SQLiteDatabase.CREATE_IF_NECESSARY)
                .addOpenFlags(SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING)
                .build();
        mDb = SQLiteDatabase.openDatabase(file, params);
        mDb.execSQL(CREATE_TABLE);
    }
}
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.internal.net;

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

import android.content.Context;

import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;

@RunWith(AndroidJUnit4.class)
@SmallTest
public class ConnectivityBlobStoreTest {
    private static final String DATABASE_FILENAME = "ConnectivityBlobStore.db";

    private Context mContext;
    private File mFile;

    private ConnectivityBlobStore createConnectivityBlobStore() {
        return new ConnectivityBlobStore(mFile);
    }

    @Before
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getContext();
        mFile = mContext.getDatabasePath(DATABASE_FILENAME);
    }

    @After
    public void tearDown() throws Exception {
        mContext.deleteDatabase(DATABASE_FILENAME);
    }

    @Test
    public void testFileCreateDelete() {
        assertFalse(mFile.exists());
        createConnectivityBlobStore();
        assertTrue(mFile.exists());

        assertTrue(mContext.deleteDatabase(DATABASE_FILENAME));
        assertFalse(mFile.exists());
    }
}
+1 −0
Original line number Diff line number Diff line
include /core/java/com/android/internal/net/OWNERS