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

Commit 5809a5f2 authored by Khaled Abdelmohsen's avatar Khaled Abdelmohsen
Browse files

Add rule component validations

Bug: 141979167
Test: atest FrameworksServicesTests:RuleTest
Test: atest FrameworksServicesTests:AtomicFormulaTest
Test: atest FrameworksServicesTests:OpenFormulaTest
Change-Id: I3411a0fba37e7f3f9b9f6bbc5b2b895203bbd701
parent e60b9143
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -112,6 +112,7 @@ public abstract class AtomicFormula implements Formula {
    private final @Key int mKey;

    public AtomicFormula(@Key int key) {
        checkArgument(isValidKey(key), String.format("Unknown key: %d", key));
        mKey = key;
    }

@@ -134,6 +135,8 @@ public abstract class AtomicFormula implements Formula {
            checkArgument(
                    key == VERSION_CODE,
                    String.format("Key %s cannot be used with IntAtomicFormula", keyToString(key)));
            checkArgument(isValidOperator(operator),
                    String.format("Unknown operator: %d", operator));
            mOperator = operator;
            mValue = value;
        }
@@ -237,6 +240,14 @@ public abstract class AtomicFormula implements Formula {
                            "Unexpected key in IntAtomicFormula" + getKey());
            }
        }

        private static boolean isValidOperator(int operator) {
            return operator == EQ
                    || operator == LT
                    || operator == LE
                    || operator == GT
                    || operator == GE;
        }
    }

    /** An {@link AtomicFormula} with a key and string value. */
@@ -486,4 +497,13 @@ public abstract class AtomicFormula implements Formula {
                throw new IllegalArgumentException("Unknown operator " + op);
        }
    }

    private static boolean isValidKey(int key) {
        return key == PACKAGE_NAME
                || key == APP_CERTIFICATE
                || key == VERSION_CODE
                || key == INSTALLER_NAME
                || key == INSTALLER_CERTIFICATE
                || key == PRE_INSTALLED;
    }
}
+8 −0
Original line number Diff line number Diff line
@@ -86,6 +86,8 @@ public final class OpenFormula implements Formula, Parcelable {
     *     for that operator (at least 2 for {@link #AND} and {@link #OR}, 1 for {@link #NOT}).
     */
    public OpenFormula(@Connector int connector, @NonNull List<Formula> formulas) {
        checkArgument(isValidConnector(connector),
                String.format("Unknown connector: %d", connector));
        validateFormulas(connector, formulas);
        this.mConnector = connector;
        this.mFormulas = Collections.unmodifiableList(formulas);
@@ -213,4 +215,10 @@ public final class OpenFormula implements Formula, Parcelable {
                throw new IllegalArgumentException("Unknown connector " + connector);
        }
    }

    private static boolean isValidConnector(int connector) {
        return connector == AND
                || connector == OR
                || connector == NOT;
    }
}
+7 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.integrity.model;

import static com.android.internal.util.Preconditions.checkArgument;
import static com.android.internal.util.Preconditions.checkNotNull;

import android.annotation.IntDef;
@@ -62,6 +63,7 @@ public final class Rule implements Parcelable {
    private final @Effect int mEffect;

    public Rule(@NonNull Formula formula, @Effect int effect) {
        checkArgument(isValidEffect(effect), String.format("Unknown effect: %d", effect));
        this.mFormula = checkNotNull(formula);
        this.mEffect = effect;
    }
@@ -137,4 +139,9 @@ public final class Rule implements Parcelable {
                throw new IllegalArgumentException("Unknown effect " + effect);
        }
    }

    private static boolean isValidEffect(int effect) {
        return effect == DENY
                || effect == FORCE_ALLOW;
    }
}
+16 −0
Original line number Diff line number Diff line
@@ -263,6 +263,22 @@ public class AtomicFormulaTest {
        assertEquals(formula, newFormula);
    }

    @Test
    public void testInvalidAtomicFormula_invalidKey() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown key: -1",
                () -> new IntAtomicFormula(/* key= */ -1, AtomicFormula.EQ, 0));
    }

    @Test
    public void testInvalidAtomicFormula_invalidOperator() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown operator: -1",
                () -> new IntAtomicFormula(AtomicFormula.VERSION_CODE, /* operator= */ -1, 0));
    }

    /** Returns a builder with all fields filled with some dummy data. */
    private AppInstallMetadata.Builder getAppInstallMetadataBuilder() {
        return new AppInstallMetadata.Builder()
+15 −4
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ public class OpenFormulaTest {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Connector AND must have at least 2 formulas"),
                "Connector AND must have at least 2 formulas",
                () ->
                        new OpenFormula(
                                OpenFormula.AND, Collections.singletonList(ATOMIC_FORMULA_1)));
@@ -64,7 +64,7 @@ public class OpenFormulaTest {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Connector NOT must have 1 formula only"),
                "Connector NOT must have 1 formula only",
                () ->
                        new OpenFormula(
                                OpenFormula.NOT,
@@ -73,7 +73,8 @@ public class OpenFormulaTest {

    @Test
    public void testIsSatisfiable_notFalse_true() {
        OpenFormula openFormula = new OpenFormula(OpenFormula.NOT, Arrays.asList(ATOMIC_FORMULA_1));
        OpenFormula openFormula = new OpenFormula(OpenFormula.NOT,
                Collections.singletonList(ATOMIC_FORMULA_1));
        AppInstallMetadata appInstallMetadata =
                getAppInstallMetadataBuilder().setPackageName("test2").build();
        // validate assumptions about the metadata
@@ -84,7 +85,8 @@ public class OpenFormulaTest {

    @Test
    public void testIsSatisfiable_notTrue_false() {
        OpenFormula openFormula = new OpenFormula(OpenFormula.NOT, Arrays.asList(ATOMIC_FORMULA_1));
        OpenFormula openFormula = new OpenFormula(OpenFormula.NOT,
                Collections.singletonList(ATOMIC_FORMULA_1));
        AppInstallMetadata appInstallMetadata =
                getAppInstallMetadataBuilder().setPackageName("test1").build();
        // validate assumptions about the metadata
@@ -209,6 +211,15 @@ public class OpenFormulaTest {
        assertEquals(formula, newFormula);
    }

    @Test
    public void testInvalidOpenFormula_invalidConnector() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown connector: -1",
                () -> new OpenFormula(/* connector= */ -1,
                        Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)));
    }

    /** Returns a builder with all fields filled with some dummy data. */
    private AppInstallMetadata.Builder getAppInstallMetadataBuilder() {
        return new AppInstallMetadata.Builder()
Loading