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

Commit fa45143b authored by Eduard Dumitrescul's avatar Eduard Dumitrescul
Browse files

Implement method isPolicyApplied is FlagUnion

The isPolicyApplied method checks whether all the set bits int the given
policy value are also set in the resolved value.

Bug: 285532044

Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_flagUnion_flagSet_returnTrue
Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_flagUnion_flagNotSet_returnFalse
Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_flagUnion_someFlagsNotSet_returnFalse

Flag: TEST_ONLY

Change-Id: Id914a3f91be0e255550fb9b4a34092e8e15df127
parent 86531699
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -42,6 +42,31 @@ final class FlagUnion extends ResolutionMechanism<Integer> {
                adminPolicies.keySet());
    }

    /**
     * Checks whether the given policy {@code value} is considered applied
     * based on the {@code resolvedPolicy} and the {@code FlagUnion} resolution
     * mechanism.
     *
     * <p> This mechanism treats the Integer values as bitmasks. The check passes
     *     if all flags set in the {@code value} parameter are also set in the
     *     {@code resolvedPolicy}.
     *
     * @param value the policy value representing the flag(s) to check for.
     * @param resolvedPolicy The current resolved policy value, representing
     *                       the bitmask of all enabled flags.
     * @return true if all flags in {@code value} are set within
     *         {@code resolvedPolicy}, false otherwise.
     */
    @Override
    public boolean isPolicyApplied(@NonNull PolicyValue<Integer> value,
            @NonNull PolicyValue<Integer> resolvedPolicy) {
        Objects.requireNonNull(value, "Input PolicyValue 'value' cannot be null.");
        Objects.requireNonNull(resolvedPolicy, "Input PolicyValue 'resolvedPolicy'"
                + " cannot be null.");

        return ((resolvedPolicy.getValue() & value.getValue()) == value.getValue());
    }

    @Override
    android.app.admin.FlagUnion getParcelableResolutionMechanism() {
        return android.app.admin.FlagUnion.FLAG_UNION;
+26 −1
Original line number Diff line number Diff line
@@ -170,6 +170,30 @@ class ResolutionMechanismTest {
        }
    }

    @Test
    fun isPolicyApplied_flagUnion_flagSet_returnTrue() {
        val resolutionMechanism = FlagUnion()

        assertTrue { resolutionMechanism.isPolicyApplied(INT_POLICY_A,
            INT_POLICY_AB) }
    }

    @Test
    fun isPolicyApplied_flagUnion_flagNotSet_returnFalse() {
        val resolutionMechanism = FlagUnion()

        assertFalse { resolutionMechanism.isPolicyApplied(INT_POLICY_C,
            INT_POLICY_AB) }
    }

    @Test
    fun isPolicyApplied_flagUnion_someFlagsNotSet_returnFalse() {
        val resolutionMechanism = FlagUnion()

        assertFalse { resolutionMechanism.isPolicyApplied(INT_POLICY_AB,
            INT_POLICY_A) }
    }

    companion object {
        private const val SYSTEM_USER_ID = UserHandle.USER_SYSTEM
        private val SYSTEM_ADMIN = EnforcingAdmin.createSystemEnforcingAdmin("system_entity")
@@ -186,6 +210,7 @@ class ResolutionMechanismTest {

        private val INT_POLICY_A = IntegerPolicyValue(1 shl 7)
        private val INT_POLICY_B = IntegerPolicyValue(1 shl 8)
        private val INT_POLICY_AB = IntegerPolicyValue(1 shl 7 or 1 shl 8)
        private val INT_POLICY_C = IntegerPolicyValue(1 shl 9)
        private val INT_POLICY_AB = IntegerPolicyValue((1 shl 7) or (1 shl 8))
    }
}