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

Commit 1efff874 authored by Khaled Abdelmohsen's avatar Khaled Abdelmohsen
Browse files

Include isHashedValue in rule structure

Bug: 141979167
Test: atest FrameworksServicesTests:RuleXmlSerializerTest
Test: atest FrameworksServicesTests:RuleXmlParserTest
Change-Id: Ic4587c5bcd65e0748c15655f79136eaa50a4eb1f
parent e39fa0fd
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -253,6 +253,8 @@ public abstract class AtomicFormula implements Formula {
    /** An {@link AtomicFormula} with a key and string value. */
    public static final class StringAtomicFormula extends AtomicFormula implements Parcelable {
        private final String mValue;
        // Indicates whether the value is the actual value or the hashed value.
        private final boolean mIsHashedValue;

        /**
         * Constructs a new {@link StringAtomicFormula}.
@@ -262,8 +264,9 @@ public abstract class AtomicFormula implements Formula {
         *
         * @throws IllegalArgumentException if {@code key} cannot be used with string value
         */
        public StringAtomicFormula(@Key int key, @NonNull String value) {
        public StringAtomicFormula(@Key int key, @NonNull String value, boolean isHashedValue) {
            super(key);
            mIsHashedValue = isHashedValue;
            checkArgument(
                    key == PACKAGE_NAME
                            || key == APP_CERTIFICATE
@@ -277,6 +280,7 @@ public abstract class AtomicFormula implements Formula {
        StringAtomicFormula(Parcel in) {
            super(in.readInt());
            mValue = in.readStringNoHelper();
            mIsHashedValue = in.readByte() != 0;
        }

        @NonNull
@@ -335,6 +339,7 @@ public abstract class AtomicFormula implements Formula {
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeInt(getKey());
            dest.writeStringNoHelper(mValue);
            dest.writeByte((byte) (mIsHashedValue ? 1 : 0));
        }

        @NonNull
@@ -342,6 +347,10 @@ public abstract class AtomicFormula implements Formula {
            return mValue;
        }

        public boolean getIsHashedValue() {
            return mIsHashedValue;
        }

        private String getMetadataValueByKey(AppInstallMetadata appInstallMetadata) {
            switch (getKey()) {
                case PACKAGE_NAME:
+7 −3
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ public final class RuleXmlParser implements RuleParser {
    private static final String OPERATOR_ATTRIBUTE = "O";
    private static final String VALUE_ATTRIBUTE = "V";
    private static final String CONNECTOR_ATTRIBUTE = "C";
    private static final String IS_HASHED_VALUE_ATTRIBUTE = "H";

    @Override
    public List<Rule> parse(String ruleText) throws RuleParseException {
@@ -179,6 +180,8 @@ public final class RuleXmlParser implements RuleParser {
        int operator = Integer.parseInt(
                extractAttributeValue(parser, OPERATOR_ATTRIBUTE).orElse("-1"));
        String value = extractAttributeValue(parser, VALUE_ATTRIBUTE).orElse(null);
        String isHashedValue = extractAttributeValue(parser, IS_HASHED_VALUE_ATTRIBUTE).orElse(
                null);

        int eventType;
        while ((eventType = parser.next()) != XmlPullParser.END_DOCUMENT) {
@@ -186,17 +189,18 @@ public final class RuleXmlParser implements RuleParser {
                break;
            }
        }
        return constructAtomicFormulaBasedOnKey(key, operator, value);
        return constructAtomicFormulaBasedOnKey(key, operator, value, isHashedValue);
    }

    private static Formula constructAtomicFormulaBasedOnKey(@AtomicFormula.Key int key,
            @AtomicFormula.Operator int operator, String value) {
            @AtomicFormula.Operator int operator, String value, String isHashedValue) {
        switch (key) {
            case AtomicFormula.PACKAGE_NAME:
            case AtomicFormula.INSTALLER_NAME:
            case AtomicFormula.APP_CERTIFICATE:
            case AtomicFormula.INSTALLER_CERTIFICATE:
                return new AtomicFormula.StringAtomicFormula(key, value);
                return new AtomicFormula.StringAtomicFormula(key, value,
                        Boolean.parseBoolean(isHashedValue));
            case AtomicFormula.PRE_INSTALLED:
                return new AtomicFormula.BooleanAtomicFormula(key, Boolean.parseBoolean(value));
            case AtomicFormula.VERSION_CODE:
+5 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ public class RuleXmlSerializer implements RuleSerializer {
    private static final String OPERATOR_ATTRIBUTE = "O";
    private static final String VALUE_ATTRIBUTE = "V";
    private static final String CONNECTOR_ATTRIBUTE = "C";
    private static final String IS_HASHED_VALUE_ATTRIBUTE = "H";

    @Override
    public void serialize(List<Rule> rules, OutputStream outputStream)
@@ -129,6 +130,10 @@ public class RuleXmlSerializer implements RuleSerializer {
        if (atomicFormula instanceof AtomicFormula.StringAtomicFormula) {
            serializeAttributeValue(VALUE_ATTRIBUTE,
                    ((AtomicFormula.StringAtomicFormula) atomicFormula).getValue(), xmlSerializer);
            serializeAttributeValue(IS_HASHED_VALUE_ATTRIBUTE,
                    String.valueOf(
                            ((AtomicFormula.StringAtomicFormula) atomicFormula).getIsHashedValue()),
                    xmlSerializer);
        } else if (atomicFormula instanceof AtomicFormula.IntAtomicFormula) {
            serializeAttributeValue(OPERATOR_ATTRIBUTE,
                    String.valueOf(((AtomicFormula.IntAtomicFormula) atomicFormula).getOperator()),
+32 −16
Original line number Diff line number Diff line
@@ -63,7 +63,8 @@ public class RuleEvaluatorTest {
    public void testEvaluateRules_noMatchedRules_allow() {
        Rule rule1 =
                new Rule(
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2),
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2,
                                /* isHashedValue= */ false),
                        Rule.DENY);

        IntegrityCheckResult result =
@@ -76,11 +77,13 @@ public class RuleEvaluatorTest {
    public void testEvaluateRules_oneMatch_deny() {
        Rule rule1 =
                new Rule(
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                /* isHashedValue= */ false),
                        Rule.DENY);
        Rule rule2 =
                new Rule(
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2),
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2,
                                /* isHashedValue= */ false),
                        Rule.DENY);

        IntegrityCheckResult result =
@@ -94,15 +97,18 @@ public class RuleEvaluatorTest {
    public void testEvaluateRules_multipleMatches_deny() {
        Rule rule1 =
                new Rule(
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                /* isHashedValue= */ false),
                        Rule.DENY);
        OpenFormula openFormula2 =
                new OpenFormula(
                        OpenFormula.AND,
                        Arrays.asList(
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                        /* isHashedValue= */ false),
                                new StringAtomicFormula(
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE)));
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE,
                                        /* isHashedValue= */ false)));
        Rule rule2 = new Rule(openFormula2, Rule.DENY);

        IntegrityCheckResult result =
@@ -119,7 +125,8 @@ public class RuleEvaluatorTest {
                        OpenFormula.NOT,
                        Collections.singletonList(
                                new StringAtomicFormula(
                                        AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2)));
                                        AtomicFormula.PACKAGE_NAME,
                                        PACKAGE_NAME_2, /* isHashedValue= */ false)));
        Rule rule = new Rule(openFormula, Rule.DENY);

        IntegrityCheckResult result =
@@ -150,9 +157,11 @@ public class RuleEvaluatorTest {
                new OpenFormula(
                        OpenFormula.AND,
                        Arrays.asList(
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                        /* isHashedValue= */ false),
                                new StringAtomicFormula(
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE)));
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE,
                                        /* isHashedValue= */ false)));
        Rule rule = new Rule(openFormula, Rule.DENY);

        IntegrityCheckResult result =
@@ -168,9 +177,11 @@ public class RuleEvaluatorTest {
                new OpenFormula(
                        OpenFormula.OR,
                        Arrays.asList(
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                        /* isHashedValue= */ false),
                                new StringAtomicFormula(
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE)));
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE,
                                        /* isHashedValue= */ false)));
        Rule rule = new Rule(openFormula, Rule.DENY);

        IntegrityCheckResult result =
@@ -185,9 +196,11 @@ public class RuleEvaluatorTest {
                new OpenFormula(
                        OpenFormula.AND,
                        Arrays.asList(
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2),
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_2,
                                        /* isHashedValue= */ false),
                                new StringAtomicFormula(
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE)));
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE,
                                        /* isHashedValue= */ false)));
        OpenFormula openFormula =
                new OpenFormula(OpenFormula.NOT, Collections.singletonList(openSubFormula));
        Rule rule = new Rule(openFormula, Rule.DENY);
@@ -202,15 +215,18 @@ public class RuleEvaluatorTest {
    public void testEvaluateRules_forceAllow() {
        Rule rule1 =
                new Rule(
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                        new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                /* isHashedValue= */ false),
                        Rule.FORCE_ALLOW);
        OpenFormula openFormula2 =
                new OpenFormula(
                        OpenFormula.AND,
                        Arrays.asList(
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1),
                                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, PACKAGE_NAME_1,
                                        /* isHashedValue= */ false),
                                new StringAtomicFormula(
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE)));
                                        AtomicFormula.APP_CERTIFICATE, APP_CERTIFICATE,
                                        /* isHashedValue= */ false)));
        Rule rule2 = new Rule(openFormula2, Rule.DENY);

        IntegrityCheckResult result =
+10 −5
Original line number Diff line number Diff line
@@ -38,7 +38,8 @@ public class AtomicFormulaTest {
    @Test
    public void testValidAtomicFormula_stringValue() {
        StringAtomicFormula stringAtomicFormula =
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, "com.test.app");
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME,
                        "com.test.app", /* isHashedValue= */ false);

        assertEquals(AtomicFormula.PACKAGE_NAME, stringAtomicFormula.getKey());
    }
@@ -66,7 +67,8 @@ public class AtomicFormulaTest {
                /* expectedExceptionMessageRegex */
                String.format(
                        "Key VERSION_CODE cannot be used with StringAtomicFormula"),
                () -> new StringAtomicFormula(AtomicFormula.VERSION_CODE, "test-value"));
                () -> new StringAtomicFormula(AtomicFormula.VERSION_CODE, "test-value",
                        /* isHashedValue= */ false));
    }

    @Test
@@ -92,7 +94,8 @@ public class AtomicFormulaTest {
    @Test
    public void testIsSatisfiable_string_true() {
        StringAtomicFormula stringAtomicFormula =
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, "com.test.app");
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME,
                        "com.test.app", /* isHashedValue= */ false);
        AppInstallMetadata appInstallMetadata =
                getAppInstallMetadataBuilder().setPackageName("com.test.app").build();

@@ -102,7 +105,8 @@ public class AtomicFormulaTest {
    @Test
    public void testIsSatisfiable_string_false() {
        StringAtomicFormula stringAtomicFormula =
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, "com.test.app");
                new StringAtomicFormula(AtomicFormula.PACKAGE_NAME,
                        "com.test.app", /* isHashedValue= */ false);
        AppInstallMetadata appInstallMetadata =
                getAppInstallMetadataBuilder().setPackageName("com.foo.bar").build();

@@ -231,7 +235,8 @@ public class AtomicFormulaTest {

    @Test
    public void testParcelUnparcel_string() {
        StringAtomicFormula formula = new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, "abc");
        StringAtomicFormula formula = new StringAtomicFormula(AtomicFormula.PACKAGE_NAME, "abc",
                /* isHashedValue= */ false);
        Parcel p = Parcel.obtain();
        formula.writeToParcel(p, 0);
        p.setDataPosition(0);
Loading