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

Commit 53e9f441 authored by Hansen Kurli's avatar Hansen Kurli Committed by Automerger Merge Worker
Browse files

Merge "Escape the LIKE clause for list()" into main am: f3ef502a

parents 410c4f18 f3ef502a
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.internal.net;
import android.annotation.NonNull;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Binder;
@@ -153,8 +154,11 @@ public class ConnectivityBlobStore {
        final List<String> names = new ArrayList<String>();
        try (Cursor cursor = mDb.query(TABLENAME,
                new String[] {"name"} /* columns */,
                "owner=? AND name LIKE ?" /* selection */,
                new String[] {Integer.toString(ownerUid), prefix + "%"} /* selectionArgs */,
                "owner=? AND name LIKE ? ESCAPE '\\'" /* selection */,
                new String[] {
                        Integer.toString(ownerUid),
                        DatabaseUtils.escapeForLike(prefix) + "%"
                } /* selectionArgs */,
                null /* groupBy */,
                null /* having */,
                "name ASC" /* orderBy */)) {
+37 −0
Original line number Diff line number Diff line
@@ -153,4 +153,41 @@ public class ConnectivityBlobStoreTest {
        final String[] actual = connectivityBlobStore.list(TEST_NAME /* prefix */);
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testList_underscoreInPrefix() throws Exception {
        final String prefix = TEST_NAME + "_";
        final String[] unsortedNames = new String[] {
                prefix + "000",
                TEST_NAME + "123",
        };
        // The '_' in the prefix should not be treated as a wildcard so the only match is "000".
        final String[] expected = new String[] {"000"};
        final ConnectivityBlobStore connectivityBlobStore = createConnectivityBlobStore();

        for (int i = 0; i < unsortedNames.length; i++) {
            assertTrue(connectivityBlobStore.put(unsortedNames[i], TEST_BLOB));
        }
        final String[] actual = connectivityBlobStore.list(prefix);
        assertArrayEquals(expected, actual);
    }

    @Test
    public void testList_percentInPrefix() throws Exception {
        final String prefix = "%" + TEST_NAME + "%";
        final String[] unsortedNames = new String[] {
                TEST_NAME + "12345",
                prefix + "0",
                "abc" + TEST_NAME + "987",
        };
        // The '%' in the prefix should not be treated as a wildcard so the only match is "0".
        final String[] expected = new String[] {"0"};
        final ConnectivityBlobStore connectivityBlobStore = createConnectivityBlobStore();

        for (int i = 0; i < unsortedNames.length; i++) {
            assertTrue(connectivityBlobStore.put(unsortedNames[i], TEST_BLOB));
        }
        final String[] actual = connectivityBlobStore.list(prefix);
        assertArrayEquals(expected, actual);
    }
}