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

Commit 2b8a4dfb authored by Hansen Kurli's avatar Hansen Kurli
Browse files

Escape the LIKE clause for list()

The LIKE clause uses '_' and '%' as wildcard characters. They
need to be escaped for the prefix to match exact names.

Bug: 307903113
Test: atest ConnectivityBlobStoreTest
Change-Id: Ia5b6cda5e06bf5e6f0992e85eccf47feae4b01a3
parent 906d4623
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);
    }
}