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

Commit 4f341bd3 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove the unnecessary class that performs the same function with expectException method."

parents e06da209 ffc7435f
Loading
Loading
Loading
Loading
+40 −33
Original line number Diff line number Diff line
@@ -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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key VERSION_CODE cannot be used with StringAtomicFormula"),
                        () ->
                                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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key PACKAGE_NAME cannot be used with LongAtomicFormula"),
                        () ->
                                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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                String.format("Key PACKAGE_NAME cannot be used with BooleanAtomicFormula"),
                        () -> 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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown key: -1",
                () -> new AtomicFormula.LongAtomicFormula(/* key= */ -1, AtomicFormula.EQ, 0));
                        () -> new AtomicFormula.LongAtomicFormula(/* key= */ -1,
                                AtomicFormula.EQ, /* value= */0));
        assertThat(e.getMessage()).matches("Unknown key: -1");
    }

    @Test
    public void testInvalidAtomicFormula_invalidOperator() {
        assertExpectException(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown operator: -1",
                        () ->
                                new AtomicFormula.LongAtomicFormula(
                                AtomicFormula.VERSION_CODE, /* operator= */ -1, 0));
                                        AtomicFormula.VERSION_CODE, /* operator= */ -1, /* value= */
                                        0));
        assertThat(e.getMessage()).matches("Unknown operator: -1");
    }

    @Test
+28 −30
Original line number Diff line number Diff line
@@ -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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                "Connector AND must have at least 2 formulas",
                        () ->
                                new CompoundFormula(
                                CompoundFormula.AND, Collections.singletonList(ATOMIC_FORMULA_1)));
                                        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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */
                "Connector NOT must have 1 formula only",
                        () ->
                                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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown connector: -1",
                        () ->
                                new CompoundFormula(
                                        /* connector= */ -1,
                                        Arrays.asList(ATOMIC_FORMULA_1, ATOMIC_FORMULA_2)));
        assertThat(e.getMessage()).matches("Unknown connector: -1");
    }

    @Test
+16 −10
Original line number Diff line number Diff line
@@ -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(
    public void testInvalidHexDigest_mustHaveEvenLength() {
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                "must have even length",
                        () -> IntegrityUtils.getBytesFromHexDigest("ABC"));
        assertThat(e.getMessage()).containsMatch("must have even length");
    }

        TestUtils.assertExpectException(
    @Test
    public void testInvalidHexDigest_invalidHexChar() {
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                "Invalid hex char",
                        () -> IntegrityUtils.getBytesFromHexDigest("GH"));
        assertThat(e.getMessage()).containsMatch("Invalid hex char");
    }
}
+23 −28
Original line number Diff line number Diff line
@@ -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,15 +49,15 @@ 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(
        Exception e =
                expectThrows(
                        NullPointerException.class,
                /* expectedExceptionMessageRegex */ null,
                        () -> new Rule(null, DENY_EFFECT));
    }

@@ -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(
        assertThat(rule.toString())
                .isEqualTo(
                        String.format(
                                "Rule: (PACKAGE_NAME EQ %s) AND (APP_CERTIFICATE EQ %s), DENY",
                        PACKAGE_NAME, APP_CERTIFICATE),
                rule.toString());
                                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(
        Exception e =
                expectThrows(
                        IllegalArgumentException.class,
                /* expectedExceptionMessageRegex */ "Unknown effect: -1",
                        () -> new Rule(PACKAGE_NAME_ATOMIC_FORMULA, /* effect= */ -1));
        assertThat(e.getMessage()).isEqualTo("Unknown effect: -1");
    }
}
+0 −51
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 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");
    }
}