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

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

Merge "Implement equals and hashCode for rule components"

parents 8b33b61f f5f98141
Loading
Loading
Loading
Loading
+28 −5
Original line number Original line Diff line number Diff line
@@ -22,6 +22,8 @@ import static com.android.internal.util.Preconditions.checkNotNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.util.Slog;
import android.util.Slog;


import java.util.Objects;

/**
/**
 * Represents a simple formula consisting of an app install metadata field and a value.
 * Represents a simple formula consisting of an app install metadata field and a value.
 *
 *
@@ -130,11 +132,6 @@ public final class AtomicFormula extends Formula {
        return mBoolValue.toString();
        return mBoolValue.toString();
    }
    }


    @Override
    public String toString() {
        return String.format("%s %s %s", mKey, mOperator, getValue());
    }

    /**
    /**
     * Check if the formula is true when substituting its {@link Key} with the string value.
     * Check if the formula is true when substituting its {@link Key} with the string value.
     *
     *
@@ -188,6 +185,32 @@ public final class AtomicFormula extends Formula {
        return false;
        return false;
    }
    }


    @Override
    public String toString() {
        return String.format("%s %s %s", mKey, mOperator, getValue());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        AtomicFormula that = (AtomicFormula) o;
        return mKey == that.mKey
                && mOperator == that.mOperator
                && Objects.equals(mStringValue, that.mStringValue)
                && Objects.equals(mIntValue, that.mIntValue)
                && Objects.equals(mBoolValue, that.mBoolValue);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mKey, mOperator, mStringValue, mIntValue, mBoolValue);
    }

    private void validateOperator(Key key, Operator operator) {
    private void validateOperator(Key key, Operator operator) {
        boolean validOperator;
        boolean validOperator;
        switch (key) {
        switch (key) {
+19 −0
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import static com.android.internal.util.Preconditions.checkNotNull;


import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
import java.util.Objects;


/**
/**
 * Represents a complex formula consisting of other simple and complex formulas.
 * Represents a complex formula consisting of other simple and complex formulas.
@@ -64,6 +65,24 @@ public final class OpenFormula extends Formula {
        return sb.toString();
        return sb.toString();
    }
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        OpenFormula that = (OpenFormula) o;
        return mConnector == that.mConnector
                && mFormulas.equals(that.mFormulas);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mConnector, mFormulas);
    }

    private void validateFormulas(Connector connector, List<Formula> formulas) {
    private void validateFormulas(Connector connector, List<Formula> formulas) {
        switch (connector) {
        switch (connector) {
            case AND:
            case AND:
+20 −0
Original line number Original line Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.server.integrity.model;


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


import java.util.Objects;

/**
/**
 * Represent rules to be used in the rule evaluation engine to match against app installs.
 * Represent rules to be used in the rule evaluation engine to match against app installs.
 *
 *
@@ -66,4 +68,22 @@ public final class Rule {
    public String toString() {
    public String toString() {
        return String.format("Rule: %s, %s", mFormula, mEffect);
        return String.format("Rule: %s, %s", mFormula, mEffect);
    }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Rule that = (Rule) o;
        return mFormula.equals(that.mFormula)
                && mEffect == that.mEffect;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mFormula, mEffect);
    }
}
}
+17 −0
Original line number Original line Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.integrity.model;
import static com.android.server.testutils.TestUtils.assertExpectException;
import static com.android.server.testutils.TestUtils.assertExpectException;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertNull;


import org.junit.Test;
import org.junit.Test;
@@ -83,4 +84,20 @@ public class RuleTest {
        assertEquals(String.format("Rule: PACKAGE_NAME EQ %s AND APP_CERTIFICATE EQ %s, DENY",
        assertEquals(String.format("Rule: PACKAGE_NAME EQ %s AND APP_CERTIFICATE EQ %s, DENY",
                PACKAGE_NAME, APP_CERTIFICATE), toString);
                PACKAGE_NAME, APP_CERTIFICATE), toString);
    }
    }

    @Test
    public void testEquals_trueCase() {
        Rule rule1 = new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT);
        Rule rule2 = new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT);

        assertEquals(rule1, rule2);
    }

    @Test
    public void testEquals_falseCase() {
        Rule rule1 = new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT);
        Rule rule2 = new Rule(APP_CERTIFICATE_ATOMIC_FORMULA, DENY_EFFECT);

        assertNotEquals(rule1, rule2);
    }
}
}