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

Commit f305f8a9 authored by Hongyi Zhang's avatar Hongyi Zhang
Browse files

DeviceConfig: Unban all banned namespaces if any banned one unbanned

There are chances that innocent namespaces got banned with actually
harmful namespace, and since innocent namespaces' flags may not be
changed for a long time, they are getting banned on the devices for a
long time even though they are not problematic.
Thus we want to do: When a namespace's configuration got successfully updated, unban ALL the
banned namespaces(by clearing the hashcode map) if the updated namespace was banned before updated.

Bug: 153010388
Test: atest DeviceConfigTest & local device with Mob Dog
Change-Id: I61b92a09d77ef1e1b7bc9c86f6655ba62a8a3d89
parent 3dfc45e5
Loading
Loading
Loading
Loading
+51 −5
Original line number Original line Diff line number Diff line
@@ -644,15 +644,61 @@ public class DeviceConfigTest {
        assertThat(modifiedProperties1.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);
        assertThat(modifiedProperties1.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);


        Properties modifiedProperties2 = new Properties.Builder(namespaceToBan2).setString(KEY,
        Properties modifiedProperties2 = new Properties.Builder(namespaceToBan2).setString(KEY,
                VALUE)
                .setString(KEY3, NULL_VALUE).setString(KEY4, VALUE2).build();
        DeviceConfig.setProperties(modifiedProperties2);
        modifiedProperties2 = DeviceConfig.getProperties(namespaceToBan2);
        assertThat(modifiedProperties2.getKeyset()).containsExactly(KEY, KEY3, KEY4);
        assertThat(modifiedProperties2.getString(KEY, DEFAULT_VALUE)).isEqualTo(VALUE);
        assertThat(modifiedProperties2.getString(KEY4, DEFAULT_VALUE)).isEqualTo(VALUE2);
        // Since value is null DEFAULT_VALUE should be returned
        assertThat(modifiedProperties2.getString(KEY3, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);
    }

    @Test
    public void allConfigsUnbannedIfAnyUnbannedConfigUpdated()
            throws DeviceConfig.BadConfigException {
        // Given namespaces will be permanently banned, thus they need to be different every time
        final String namespaceToBan1 = NAMESPACE + System.currentTimeMillis();
        final String namespaceToBan2 = NAMESPACE + System.currentTimeMillis() + 1;

        // Set namespaces properties
        Properties properties1 = new Properties.Builder(namespaceToBan1).setString(KEY, VALUE)
                .setString(KEY4, NULL_VALUE).build();
        DeviceConfig.setProperties(properties1);
        Properties properties2 = new Properties.Builder(namespaceToBan2).setString(KEY2, VALUE2)
                .setString(KEY4, NULL_VALUE).build();
        DeviceConfig.setProperties(properties2);

        // Ban namespace with related properties
        DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS, namespaceToBan1);
        DeviceConfig.resetToDefaults(Settings.RESET_MODE_PACKAGE_DEFAULTS, namespaceToBan2);

        // Verify given namespace properties are banned
        assertThrows(DeviceConfig.BadConfigException.class,
                () -> DeviceConfig.setProperties(properties1));
        assertThrows(DeviceConfig.BadConfigException.class,
                () -> DeviceConfig.setProperties(properties2));

        // Modify properties and verify we can set them
        Properties modifiedProperties1 = new Properties.Builder(namespaceToBan1).setString(KEY,
                VALUE)
                VALUE)
                .setString(KEY4, NULL_VALUE).setString(KEY2, VALUE2).build();
                .setString(KEY4, NULL_VALUE).setString(KEY2, VALUE2).build();
        DeviceConfig.setProperties(modifiedProperties1);
        DeviceConfig.setProperties(modifiedProperties1);
        modifiedProperties2 = DeviceConfig.getProperties(namespaceToBan1);
        modifiedProperties1 = DeviceConfig.getProperties(namespaceToBan1);
        assertThat(modifiedProperties2.getKeyset()).containsExactly(KEY, KEY2, KEY4);
        assertThat(modifiedProperties1.getKeyset()).containsExactly(KEY, KEY2, KEY4);
        assertThat(modifiedProperties2.getString(KEY, DEFAULT_VALUE)).isEqualTo(VALUE);
        assertThat(modifiedProperties1.getString(KEY, DEFAULT_VALUE)).isEqualTo(VALUE);
        assertThat(modifiedProperties2.getString(KEY2, DEFAULT_VALUE)).isEqualTo(VALUE2);
        assertThat(modifiedProperties1.getString(KEY2, DEFAULT_VALUE)).isEqualTo(VALUE2);
        // Since value is null DEFAULT_VALUE should be returned
        assertThat(modifiedProperties1.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);

        // verify that other banned namespaces are unbanned now.
        DeviceConfig.setProperties(properties2);
        Properties result = DeviceConfig.getProperties(namespaceToBan2);
        assertThat(result.getKeyset()).containsExactly(KEY2, KEY4);
        assertThat(result.getString(KEY2, DEFAULT_VALUE)).isEqualTo(VALUE2);
        // Since value is null DEFAULT_VALUE should be returned
        // Since value is null DEFAULT_VALUE should be returned
        assertThat(modifiedProperties2.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);
        assertThat(result.getString(KEY4, DEFAULT_VALUE)).isEqualTo(DEFAULT_VALUE);
    }
    }


    // TODO(mpape): resolve b/142727848 and re-enable listener tests
    // TODO(mpape): resolve b/142727848 and re-enable listener tests
+1 −0
Original line number Original line Diff line number Diff line
@@ -2813,6 +2813,7 @@ public class SettingsProvider extends ContentProvider {
                if (settingsState.isNewConfigBannedLocked(prefix, keyValues)) {
                if (settingsState.isNewConfigBannedLocked(prefix, keyValues)) {
                    return false;
                    return false;
                }
                }
                settingsState.unbanAllConfigIfBannedConfigUpdatedLocked(prefix);
                List<String> changedSettings =
                List<String> changedSettings =
                        settingsState.setSettingsLocked(prefix, keyValues, packageName);
                        settingsState.setSettingsLocked(prefix, keyValues, packageName);
                if (!changedSettings.isEmpty()) {
                if (!changedSettings.isEmpty()) {
+10 −0
Original line number Original line Diff line number Diff line
@@ -458,6 +458,16 @@ final class SettingsState {
        return bannedHash.equals(hashCode(keyValues));
        return bannedHash.equals(hashCode(keyValues));
    }
    }


    @GuardedBy("mLock")
    public void unbanAllConfigIfBannedConfigUpdatedLocked(String prefix) {
        // If the prefix updated is a banned namespace, clear mNamespaceBannedHashes
        // to unban all unbanned namespaces.
        if (mNamespaceBannedHashes.get(prefix) != null) {
            mNamespaceBannedHashes.clear();
            scheduleWriteIfNeededLocked();
        }
    }

    @GuardedBy("mLock")
    @GuardedBy("mLock")
    public void banConfigurationLocked(String prefix, Map<String, String> keyValues) {
    public void banConfigurationLocked(String prefix, Map<String, String> keyValues) {
        if (prefix == null || keyValues.isEmpty()) {
        if (prefix == null || keyValues.isEmpty()) {