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

Commit 52f2786a authored by Khaled Abdelmohsen's avatar Khaled Abdelmohsen Committed by Android (Google) Code Review
Browse files

Merge "Add validations for rule sub-components"

parents 1e652863 be0454d8
Loading
Loading
Loading
Loading
+48 −15
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

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.Nullable;

/**
@@ -48,37 +51,45 @@ public final class AtomicFormula extends Formula {
    // The value of a key can take either 1 of 3 forms: String, Integer, or Boolean.
    // It cannot have multiple values.
    @Nullable
    final String mStringValue;
    private final String mStringValue;
    @Nullable
    final Integer mIntValue;
    private final Integer mIntValue;
    @Nullable
    final Boolean mBoolValue;
    private final Boolean mBoolValue;

    public AtomicFormula(Key key, Operator operator, String stringValue) {
        // TODO: Add validators
        this.mKey = key;
        this.mOperator = operator;
        this.mStringValue = stringValue;
        validateOperator(key, operator);
        checkArgument(
                key == Key.PACKAGE_NAME || key == Key.APP_CERTIFICATE || key == Key.INSTALLER_NAME
                        || key == Key.INSTALLER_CERTIFICATE,
                String.format("Key %s cannot have string value", key));
        this.mKey = checkNotNull(key);
        this.mOperator = checkNotNull(operator);
        this.mStringValue = checkNotNull(stringValue);
        this.mIntValue = null;
        this.mBoolValue = null;
    }

    public AtomicFormula(Key key, Operator operator, Integer intValue) {
        // TODO: Add validators
        this.mKey = key;
        this.mOperator = operator;
        validateOperator(key, operator);
        checkArgument(key == Key.VERSION_CODE,
                String.format("Key %s cannot have integer value", key));
        this.mKey = checkNotNull(key);
        this.mOperator = checkNotNull(operator);
        this.mStringValue = null;
        this.mIntValue = intValue;
        this.mIntValue = checkNotNull(intValue);
        this.mBoolValue = null;
    }

    public AtomicFormula(Key key, Operator operator, Boolean boolValue) {
        // TODO: Add validators
        this.mKey = key;
        this.mOperator = operator;
        validateOperator(key, operator);
        checkArgument(key == Key.PRE_INSTALLED,
                String.format("Key %s cannot have boolean value", key));
        this.mKey = checkNotNull(key);
        this.mOperator = checkNotNull(operator);
        this.mStringValue = null;
        this.mIntValue = null;
        this.mBoolValue = boolValue;
        this.mBoolValue = checkNotNull(boolValue);
    }

    public Key getKey() {
@@ -100,4 +111,26 @@ public final class AtomicFormula extends Formula {
    public Boolean getBoolValue() {
        return mBoolValue;
    }

    private void validateOperator(Key key, Operator operator) {
        boolean validOperator;
        switch (key) {
            case PACKAGE_NAME:
            case APP_CERTIFICATE:
            case INSTALLER_NAME:
            case INSTALLER_CERTIFICATE:
            case PRE_INSTALLED:
                validOperator = (operator == Operator.EQ);
                break;
            case VERSION_CODE:
                validOperator = true;
                break;
            default:
                validOperator = false;
        }
        if (!validOperator) {
            throw new IllegalArgumentException(
                    String.format("Invalid operator %s used for key %s", operator, key));
        }
    }
}
+21 −0
Original line number Diff line number Diff line
@@ -39,8 +39,10 @@ public final class OpenFormula extends Formula {

    public OpenFormula(Connector connector, Formula mainFormula,
            @Nullable Formula auxiliaryFormula) {
        validateAuxiliaryFormula(connector, auxiliaryFormula);
        this.mConnector = checkNotNull(connector);
        this.mMainFormula = checkNotNull(mainFormula);
        // TODO: Add validators on auxiliary formula
        this.mAuxiliaryFormula = auxiliaryFormula;
    }

@@ -55,4 +57,23 @@ public final class OpenFormula extends Formula {
    public Formula getAuxiliaryFormula() {
        return mAuxiliaryFormula;
    }

    private void validateAuxiliaryFormula(Connector connector, Formula auxiliaryFormula) {
        boolean validAuxiliaryFormula;
        switch (connector) {
            case AND:
            case OR:
                validAuxiliaryFormula = (auxiliaryFormula != null);
                break;
            case NOT:
                validAuxiliaryFormula = (auxiliaryFormula == null);
                break;
            default:
                validAuxiliaryFormula = false;
        }
        if (!validAuxiliaryFormula) {
            throw new IllegalArgumentException(
                    String.format("Invalid formulas used for connector %s", connector));
        }
    }
}
+100 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.integrity.model;

import static com.android.server.testutils.TestUtils.assertExpectException;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class AtomicFormulaTest {

    @Test
    public void testValidAtomicFormula_stringValue() {
        AtomicFormula atomicFormula = new AtomicFormula(AtomicFormula.Key.PACKAGE_NAME,
                AtomicFormula.Operator.EQ, "com.test.app");

        assertEquals(AtomicFormula.Key.PACKAGE_NAME, atomicFormula.getKey());
        assertEquals(AtomicFormula.Operator.EQ, atomicFormula.getOperator());
        assertEquals("com.test.app", atomicFormula.getStringValue());
    }

    @Test
    public void testValidAtomicFormula_intValue() {
        AtomicFormula atomicFormula = new AtomicFormula(AtomicFormula.Key.VERSION_CODE,
                AtomicFormula.Operator.LE, 1);

        assertEquals(AtomicFormula.Key.VERSION_CODE, atomicFormula.getKey());
        assertEquals(AtomicFormula.Operator.LE, atomicFormula.getOperator());
        assertEquals(1, atomicFormula.getIntValue().intValue());
    }

    @Test
    public void testValidAtomicFormula_boolValue() {
        AtomicFormula atomicFormula = new AtomicFormula(AtomicFormula.Key.PRE_INSTALLED,
                AtomicFormula.Operator.EQ, true);

        assertEquals(AtomicFormula.Key.PRE_INSTALLED, atomicFormula.getKey());
        assertEquals(AtomicFormula.Operator.EQ, atomicFormula.getOperator());
        assertEquals(true, atomicFormula.getBoolValue());
    }

    @Test
    public void testInvalidAtomicFormula_stringValue() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key %s cannot have string value", AtomicFormula.Key.VERSION_CODE),
                () -> new AtomicFormula(AtomicFormula.Key.VERSION_CODE, AtomicFormula.Operator.EQ,
                        "test-value"));
    }

    @Test
    public void testInvalidAtomicFormula_intValue() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key %s cannot have integer value", AtomicFormula.Key.PACKAGE_NAME),
                () -> new AtomicFormula(AtomicFormula.Key.PACKAGE_NAME, AtomicFormula.Operator.EQ,
                        1));
    }

    @Test
    public void testInvalidAtomicFormula_boolValue() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key %s cannot have boolean value", AtomicFormula.Key.PACKAGE_NAME),
                () -> new AtomicFormula(AtomicFormula.Key.PACKAGE_NAME, AtomicFormula.Operator.EQ,
                        true));
    }

    @Test
    public void testValidateOperator_invalidKeyOperatorPair() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Invalid operator %s used for key %s",
                        AtomicFormula.Operator.LE, AtomicFormula.Key.PACKAGE_NAME),
                () -> new AtomicFormula(AtomicFormula.Key.PACKAGE_NAME, AtomicFormula.Operator.LE,
                        "test-value"));
    }
}
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.integrity.model;

import static com.android.server.testutils.TestUtils.assertExpectException;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class OpenFormulaTest {

    private static final AtomicFormula ATOMIC_FORMULA_1 = new AtomicFormula(
            AtomicFormula.Key.PACKAGE_NAME, AtomicFormula.Operator.EQ, "test1");
    private static final AtomicFormula ATOMIC_FORMULA_2 = new AtomicFormula(
            AtomicFormula.Key.VERSION_CODE, AtomicFormula.Operator.EQ, 1);

    @Test
    public void testValidOpenFormula() {
        OpenFormula openFormula = new OpenFormula(OpenFormula.Connector.AND, ATOMIC_FORMULA_1,
                ATOMIC_FORMULA_2);

        assertEquals(OpenFormula.Connector.AND, openFormula.getConnector());
        assertEquals(ATOMIC_FORMULA_1, openFormula.getMainFormula());
        assertEquals(ATOMIC_FORMULA_2, openFormula.getAuxiliaryFormula());
    }

    @Test
    public void testValidateAuxiliaryFormula_binaryConnectors() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Invalid formulas used for connector %s", OpenFormula.Connector.AND),
                () -> new OpenFormula(OpenFormula.Connector.AND, ATOMIC_FORMULA_1,
                        null));
    }

    @Test
    public void testValidateAuxiliaryFormula_unaryConnectors() {
        assertExpectException(
                IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Invalid formulas used for connector %s", OpenFormula.Connector.NOT),
                () -> new OpenFormula(OpenFormula.Connector.NOT, ATOMIC_FORMULA_1,
                        ATOMIC_FORMULA_2));
    }
}