Add rule component validations

Bug: 141979167
Test: atest FrameworksServicesTests:RuleTest
Test: atest FrameworksServicesTests:AtomicFormulaTest
Test: atest FrameworksServicesTests:OpenFormulaTest
Change-Id: I3411a0fba37e7f3f9b9f6bbc5b2b895203bbd701
This commit is contained in:
Khaled Abdelmohsen
2019-11-25 18:20:46 +00:00
parent e60b9143e8
commit 5809a5f286
6 changed files with 74 additions and 4 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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()

View File

@@ -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()

View File

@@ -106,4 +106,12 @@ public class RuleTest {
assertEquals(newRule, rule);
}
@Test
public void testInvalidRule_invalidEffect() {
assertExpectException(
IllegalArgumentException.class,
/* expectedExceptionMessageRegex */ "Unknown effect: -1",
() -> new Rule(PACKAGE_NAME_ATOMIC_FORMULA, /* effect= */ -1));
}
}