diff options
5 files changed, 107 insertions, 152 deletions
diff --git a/core/tests/coretests/src/android/content/integrity/AtomicFormulaTest.java b/core/tests/coretests/src/android/content/integrity/AtomicFormulaTest.java index c0d9be5dde59..7328f35790da 100644 --- a/core/tests/coretests/src/android/content/integrity/AtomicFormulaTest.java +++ b/core/tests/coretests/src/android/content/integrity/AtomicFormulaTest.java @@ -16,10 +16,10 @@ package android.content.integrity; -import static android.content.integrity.TestUtils.assertExpectException; - import static com.google.common.truth.Truth.assertThat; +import static org.testng.Assert.expectThrows; + import android.content.integrity.AtomicFormula.BooleanAtomicFormula; import android.content.integrity.AtomicFormula.StringAtomicFormula; import android.os.Parcel; @@ -133,35 +133,38 @@ public class AtomicFormulaTest { @Test public void testInvalidAtomicFormula_stringValue() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ - String.format("Key VERSION_CODE cannot be used with StringAtomicFormula"), - () -> - new StringAtomicFormula( - AtomicFormula.VERSION_CODE, - "test-value", - /* isHashedValue= */ false)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new StringAtomicFormula( + AtomicFormula.VERSION_CODE, + "test-value", + /* isHashedValue= */ false)); + assertThat(e.getMessage()).matches( + "Key VERSION_CODE cannot be used with StringAtomicFormula"); } @Test public void testInvalidAtomicFormula_longValue() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ - String.format("Key PACKAGE_NAME cannot be used with LongAtomicFormula"), - () -> - new AtomicFormula.LongAtomicFormula( - AtomicFormula.PACKAGE_NAME, AtomicFormula.EQ, 1)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new AtomicFormula.LongAtomicFormula( + AtomicFormula.PACKAGE_NAME, AtomicFormula.EQ, 1)); + assertThat(e.getMessage()).matches( + "Key PACKAGE_NAME cannot be used with LongAtomicFormula"); } @Test public void testInvalidAtomicFormula_boolValue() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ - String.format("Key PACKAGE_NAME cannot be used with BooleanAtomicFormula"), - () -> new BooleanAtomicFormula(AtomicFormula.PACKAGE_NAME, true)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> new BooleanAtomicFormula(AtomicFormula.PACKAGE_NAME, true)); + assertThat(e.getMessage()).matches( + "Key PACKAGE_NAME cannot be used with BooleanAtomicFormula"); } @Test @@ -205,20 +208,24 @@ public class AtomicFormulaTest { @Test public void testInvalidAtomicFormula_invalidKey() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ "Unknown key: -1", - () -> new AtomicFormula.LongAtomicFormula(/* key= */ -1, AtomicFormula.EQ, 0)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> new AtomicFormula.LongAtomicFormula(/* key= */ -1, + AtomicFormula.EQ, /* value= */0)); + assertThat(e.getMessage()).matches("Unknown key: -1"); } @Test public void testInvalidAtomicFormula_invalidOperator() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ "Unknown operator: -1", - () -> - new AtomicFormula.LongAtomicFormula( - AtomicFormula.VERSION_CODE, /* operator= */ -1, 0)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new AtomicFormula.LongAtomicFormula( + AtomicFormula.VERSION_CODE, /* operator= */ -1, /* value= */ + 0)); + assertThat(e.getMessage()).matches("Unknown operator: -1"); } @Test diff --git a/core/tests/coretests/src/android/content/integrity/CompoundFormulaTest.java b/core/tests/coretests/src/android/content/integrity/CompoundFormulaTest.java index fa3d671c09e5..4054c4916089 100644 --- a/core/tests/coretests/src/android/content/integrity/CompoundFormulaTest.java +++ b/core/tests/coretests/src/android/content/integrity/CompoundFormulaTest.java @@ -16,11 +16,9 @@ package android.content.integrity; -import static android.content.integrity.TestUtils.assertExpectException; - import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; +import static org.testng.Assert.expectThrows; import android.os.Parcel; @@ -46,32 +44,32 @@ public class CompoundFormulaTest { new CompoundFormula( CompoundFormula.AND, Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)); - assertEquals(CompoundFormula.AND, compoundFormula.getConnector()); - assertEquals( - Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2), compoundFormula.getFormulas()); + assertThat(compoundFormula.getConnector()).isEqualTo(CompoundFormula.AND); + assertThat(compoundFormula.getFormulas()).containsAllOf(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2); } @Test public void testValidateAuxiliaryFormula_binaryConnectors() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ - "Connector AND must have at least 2 formulas", - () -> - new CompoundFormula( - CompoundFormula.AND, Collections.singletonList(ATOMIC_FORMULA_1))); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new CompoundFormula( + CompoundFormula.AND, + Collections.singletonList(ATOMIC_FORMULA_1))); + assertThat(e.getMessage()).matches("Connector AND must have at least 2 formulas"); } @Test public void testValidateAuxiliaryFormula_unaryConnectors() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ - "Connector NOT must have 1 formula only", - () -> - new CompoundFormula( - CompoundFormula.NOT, - Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2))); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new CompoundFormula( + CompoundFormula.NOT, + Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2))); + assertThat(e.getMessage()).matches("Connector NOT must have 1 formula only"); } @Test @@ -82,20 +80,20 @@ public class CompoundFormulaTest { Parcel p = Parcel.obtain(); formula.writeToParcel(p, 0); p.setDataPosition(0); - CompoundFormula newFormula = CompoundFormula.CREATOR.createFromParcel(p); - assertEquals(formula, newFormula); + assertThat(CompoundFormula.CREATOR.createFromParcel(p)).isEqualTo(formula); } @Test public void testInvalidCompoundFormula_invalidConnector() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ "Unknown connector: -1", - () -> - new CompoundFormula( - /* connector= */ -1, - Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2))); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> + new CompoundFormula( + /* connector= */ -1, + Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2))); + assertThat(e.getMessage()).matches("Unknown connector: -1"); } @Test diff --git a/core/tests/coretests/src/android/content/integrity/IntegrityUtilsTest.java b/core/tests/coretests/src/android/content/integrity/IntegrityUtilsTest.java index 639adf6a05d7..e6f6686dcb6f 100644 --- a/core/tests/coretests/src/android/content/integrity/IntegrityUtilsTest.java +++ b/core/tests/coretests/src/android/content/integrity/IntegrityUtilsTest.java @@ -18,6 +18,7 @@ package android.content.integrity; import static com.google.common.truth.Truth.assertThat; +import static org.testng.Assert.expectThrows; import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; import org.junit.Test; @@ -43,15 +44,20 @@ public class IntegrityUtilsTest { } @Test - public void testInvalidHexDigest() { - TestUtils.assertExpectException( - IllegalArgumentException.class, - "must have even length", - () -> IntegrityUtils.getBytesFromHexDigest("ABC")); - - TestUtils.assertExpectException( - IllegalArgumentException.class, - "Invalid hex char", - () -> IntegrityUtils.getBytesFromHexDigest("GH")); + public void testInvalidHexDigest_mustHaveEvenLength() { + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> IntegrityUtils.getBytesFromHexDigest("ABC")); + assertThat(e.getMessage()).containsMatch("must have even length"); + } + + @Test + public void testInvalidHexDigest_invalidHexChar() { + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> IntegrityUtils.getBytesFromHexDigest("GH")); + assertThat(e.getMessage()).containsMatch("Invalid hex char"); } } diff --git a/core/tests/coretests/src/android/content/integrity/RuleTest.java b/core/tests/coretests/src/android/content/integrity/RuleTest.java index 8c4cfca39e90..8faf8ba2f52a 100644 --- a/core/tests/coretests/src/android/content/integrity/RuleTest.java +++ b/core/tests/coretests/src/android/content/integrity/RuleTest.java @@ -16,10 +16,9 @@ package android.content.integrity; -import static android.content.integrity.TestUtils.assertExpectException; +import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.testng.Assert.expectThrows; import android.os.Parcel; @@ -50,16 +49,16 @@ public class RuleTest { public void testValidRule() { Rule validRule = new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT); - assertEquals(PACKAGE_NAME_ATOMIC_FORMULA, validRule.getFormula()); - assertEquals(DENY_EFFECT, validRule.getEffect()); + assertThat(validRule.getFormula()).isEqualTo(PACKAGE_NAME_ATOMIC_FORMULA); + assertThat(validRule.getEffect()).isEqualTo(DENY_EFFECT); } @Test public void testInvalidRule_invalidFormula() { - assertExpectException( - NullPointerException.class, - /* expectedExceptionMessageRegex */ null, - () -> new Rule(null, DENY_EFFECT)); + Exception e = + expectThrows( + NullPointerException.class, + () -> new Rule(null, DENY_EFFECT)); } @Test @@ -70,27 +69,23 @@ public class RuleTest { Arrays.asList(PACKAGE_NAME_ATOMIC_FORMULA, APP_CERTIFICATE_ATOMIC_FORMULA)); Rule rule = new Rule(compoundFormula, Rule.DENY); - assertEquals( - String.format( - "Rule: (PACKAGE_NAME EQ %s) AND (APP_CERTIFICATE EQ %s), DENY", - PACKAGE_NAME, APP_CERTIFICATE), - rule.toString()); + assertThat(rule.toString()) + .isEqualTo( + String.format( + "Rule: (PACKAGE_NAME EQ %s) AND (APP_CERTIFICATE EQ %s), DENY", + PACKAGE_NAME, APP_CERTIFICATE)); } @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); + assertThat(new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT)) + .isEqualTo(new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT)); } @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); + assertThat(new Rule(PACKAGE_NAME_ATOMIC_FORMULA, DENY_EFFECT)) + .isNotEqualTo(new Rule(APP_CERTIFICATE_ATOMIC_FORMULA, DENY_EFFECT)); } @Test @@ -108,16 +103,16 @@ public class RuleTest { Parcel p = Parcel.obtain(); rule.writeToParcel(p, 0); p.setDataPosition(0); - Rule newRule = Rule.CREATOR.createFromParcel(p); - assertEquals(newRule, rule); + assertThat(Rule.CREATOR.createFromParcel(p)).isEqualTo(rule); } @Test public void testInvalidRule_invalidEffect() { - assertExpectException( - IllegalArgumentException.class, - /* expectedExceptionMessageRegex */ "Unknown effect: -1", - () -> new Rule(PACKAGE_NAME_ATOMIC_FORMULA, /* effect= */ -1)); + Exception e = + expectThrows( + IllegalArgumentException.class, + () -> new Rule(PACKAGE_NAME_ATOMIC_FORMULA, /* effect= */ -1)); + assertThat(e.getMessage()).isEqualTo("Unknown effect: -1"); } } diff --git a/core/tests/coretests/src/android/content/integrity/TestUtils.java b/core/tests/coretests/src/android/content/integrity/TestUtils.java deleted file mode 100644 index af984cf318f5..000000000000 --- a/core/tests/coretests/src/android/content/integrity/TestUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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 android.content.integrity; - -import android.test.MoreAsserts; - -import junit.framework.Assert; - -/** Helper methods used in tests. */ -class TestUtils { - private TestUtils() {} - - public interface ExceptionRunnable { - void run() throws Exception; - } - - public static void assertExpectException( - Class<? extends Throwable> expectedExceptionType, - String expectedExceptionMessageRegex, - ExceptionRunnable r) { - try { - r.run(); - } catch (Throwable e) { - Assert.assertTrue( - "Expected exception type was " - + expectedExceptionType.getName() - + " but caught " - + e.getClass().getName(), - expectedExceptionType.isAssignableFrom(e.getClass())); - if (expectedExceptionMessageRegex != null) { - MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage()); - } - return; // Pass. - } - Assert.fail( - "Expected exception type " + expectedExceptionType.getName() + " was not thrown"); - } -} |