Merge changes Ic8632068,I7606dad8

* changes:
  LockSettingsService: reject null keys in setString() et al.
  LockSettingsStorage: gracefully handle null keys in database
This commit is contained in:
Eric Biggers
2023-01-13 18:36:57 +00:00
committed by Android (Google) Code Review
4 changed files with 38 additions and 2 deletions

View File

@@ -1156,18 +1156,21 @@ public class LockSettingsService extends ILockSettings.Stub {
@Override
public void setBoolean(String key, boolean value, int userId) {
checkWritePermission();
Objects.requireNonNull(key);
mStorage.setBoolean(key, value, userId);
}
@Override
public void setLong(String key, long value, int userId) {
checkWritePermission();
Objects.requireNonNull(key);
mStorage.setLong(key, value, userId);
}
@Override
public void setString(String key, String value, int userId) {
checkWritePermission();
Objects.requireNonNull(key);
mStorage.setString(key, value, userId);
}

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

@@ -423,6 +423,21 @@ public class LockSettingsServiceTests extends BaseLockSettingsServiceTests {
checkPasswordHistoryLength(userId, 3);
}
@Test(expected=NullPointerException.class)
public void testSetBooleanRejectsNullKey() {
mService.setBoolean(null, false, 0);
}
@Test(expected=NullPointerException.class)
public void testSetLongRejectsNullKey() {
mService.setLong(null, 0, 0);
}
@Test(expected=NullPointerException.class)
public void testSetStringRejectsNullKey() {
mService.setString(null, "value", 0);
}
private void checkPasswordHistoryLength(int userId, int expectedLen) {
String history = mService.getString(LockPatternUtils.PASSWORD_HISTORY_KEY, "", userId);
String[] hashes = TextUtils.split(history, LockPatternUtils.PASSWORD_HISTORY_DELIMITER);

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);