LockSettingsStorage: gracefully handle null keys in database

Bug: 261860102
Test: com.android.server.locksettings
Change-Id: I7606dad85826d82d663a701e9e7bccb9d908da98
This commit is contained in:
Eric Biggers
2023-01-10 19:38:51 +00:00
parent f76de3d634
commit 96ad5160e5
2 changed files with 20 additions and 2 deletions

View File

@@ -62,6 +62,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Storage for the lock settings service.
@@ -886,12 +887,15 @@ class LockSettingsStorage {
if (!(obj instanceof CacheKey))
return false;
CacheKey o = (CacheKey) obj;
return userId == o.userId && type == o.type && key.equals(o.key);
return userId == o.userId && type == o.type && Objects.equals(key, o.key);
}
@Override
public int hashCode() {
return key.hashCode() ^ userId ^ type;
int hashCode = Objects.hashCode(key);
hashCode = 31 * hashCode + userId;
hashCode = 31 * hashCode + type;
return hashCode;
}
}
}

View File

@@ -265,6 +265,20 @@ public class LockSettingsStorageTests {
assertEquals("Cached value didn't match stored value", storage, cached);
}
@Test
public void testNullKey() {
mStorage.setString(null, "value", 0);
// Verify that this doesn't throw an exception.
assertEquals("value", mStorage.readKeyValue(null, null, 0));
// The read that happens as part of prefetchUser shouldn't throw an exception either.
mStorage.clearCache();
mStorage.prefetchUser(0);
assertEquals("value", mStorage.readKeyValue(null, null, 0));
}
@Test
public void testRemoveUser() {
mStorage.writeKeyValue("key", "value", 0);