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

Commit 03957fd5 authored by Dmitry Dementyev's avatar Dmitry Dementyev
Browse files

Recreate recoverablekeystore database if upgrade fails.

Ignore error during v6-> v7 update since the table was modified on some
devices without version change.

Bug: 274760375
Bug: 274722066
Test: atest com.android.server.locksettings.recoverablekeystore
Change-Id: I8cdb7aaa1f10791fc6c23a6a26ff9e9cba0ae0f1
parent 7a275c61
Loading
Loading
Loading
Loading
+40 −29
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.locksettings.recoverablekeystore.storage;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

@@ -122,14 +123,14 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {
    }

    private static int getDbVersion(Context context) {
        // TODO(b/254335492): Check flag
        // TODO(b/254335492): Update to version 7 and clean up code.
        return DATABASE_VERSION;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_KEYS_ENTRY);
        if (db.getVersion() == 6) {
        if (db.getVersion() == 6) { // always false
            db.execSQL(SQL_CREATE_USER_METADATA_ENTRY);
        } else {
            db.execSQL(SQL_CREATE_USER_METADATA_ENTRY_FOR_V7);
@@ -147,6 +148,7 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        try {
            if (oldVersion < 2) {
                dropAllKnownTables(db); // Wipe database.
                onCreate(db);
@@ -174,10 +176,19 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {
            }

            if (oldVersion < 7 && newVersion >= 7) {
                try {
                    upgradeDbForVersion7(db);
                } catch (SQLiteException e) {
                    Log.w(TAG, "Column was added without version update - ignore error", e);
                }
                oldVersion = 7;
            }

        } catch (SQLiteException e) {
            Log.e(TAG, "Recreating recoverablekeystore after unexpected upgrade error.", e);
            dropAllKnownTables(db); // Wipe database.
            onCreate(db);
            return;
        }
        if (oldVersion != newVersion) {
            Log.e(TAG, "Failed to update recoverablekeystore database to the most recent version");
        }
+14 −0
Original line number Diff line number Diff line
@@ -157,6 +157,20 @@ public class RecoverableKeyStoreDbHelperTest {
        checkAllColumns_latest();
    }

    @Test
    public void onUpgradeToV7_ignoresDuplicateColumnError() throws Exception {
        mDatabaseHelper.onCreate(mDatabase);
        mDatabaseHelper.onUpgrade(mDatabase, 6, 7);
        checkAllColumns_latest();
    }

    @Test
    public void onUpgradeToV7_recreatesDatabaseAfterFailure() throws Exception {
        mDatabaseHelper.onCreate(mDatabase);
        mDatabaseHelper.onUpgrade(mDatabase, 1, 7);
        checkAllColumns_latest();
    }

    private boolean isRootOfTrustTableAvailable() {
        ContentValues values = new ContentValues();
        values.put(RootOfTrustEntry.COLUMN_NAME_USER_ID, TEST_USER_ID);