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

Commit 5405dee1 authored by Eduard Dumitrescul's avatar Eduard Dumitrescul
Browse files

Implement isPolicyApplied() in PackageSetUnion

Checks if all the packages (stored as strings) from the policy 'value'
are found within the 'resolvedPolicy'

Bug: 285532044

Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_packageSetUnion_setIncluded_returnsTrue
Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_packageSetUnion_setDoesNotIntersect_returnsFalse
Test: atest com.android.server.devicePolicy.ResolutionMechanismTest#isPolicyApplied_packageSetUnion_setPartiallyIncluded_returnsFalse

Flag: TEST_ONLY

Change-Id: I64108c0f7672bb452343b0f27cb47d017008488d
parent fa45143b
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -43,6 +43,29 @@ final class PackageSetUnion extends ResolutionMechanism<Set<String>> {
                // Since it's union, all admins contribute to the final value.
                adminPolicies.keySet());
    }
    /**
     * Checks whether the given policy {@code value} is considered applied
     * based on the {@code resolvedPolicy} and the {@code PackageSetUnion} resolution
     * mechanism.
     *
     * <p> The check passes if all packages found in the {@code value} parameter are also found
     * in the {@code resolvedPolicy} set.
     *
     * @param value the policy value representing the set of packages to check for.
     * @param resolvedPolicy The current resolved policy value, represented by a set of packages.
     *
     * @return true if all packages in {@code value} are found within
     *         {@code resolvedPolicy}, false otherwise.
     */
    @Override
    public boolean isPolicyApplied(@NonNull PolicyValue<Set<String>> value,
            @NonNull PolicyValue<Set<String>> resolvedPolicy) {
        Objects.requireNonNull(value, "Input PolicyValue 'value' cannot be null.");
        Objects.requireNonNull(value, "Input PolicyValue 'resolvedPolicy' "
                + "cannot be null");

        return resolvedPolicy.getValue().containsAll(value.getValue());
    }

    @Override
    StringSetUnion getParcelableResolutionMechanism() {
+31 −0
Original line number Diff line number Diff line
@@ -194,6 +194,37 @@ class ResolutionMechanismTest {
            INT_POLICY_A) }
    }

    @Test
    fun isPolicyApplied_packageSetUnion_setIncluded_returnsTrue() {
        val resolutionMechanism = PackageSetUnion()

        assertTrue {
            resolutionMechanism.isPolicyApplied(PackageSetPolicyValue(setOf("package1")),
                PackageSetPolicyValue(setOf("package1", "package2")))
        }
    }

    @Test
    fun isPolicyApplied_packageSetUnion_setDoesNotIntersect_returnsFalse() {
        val resolutionMechanism = PackageSetUnion()

        assertFalse {
            resolutionMechanism.isPolicyApplied(PackageSetPolicyValue(setOf("package3")),
                PackageSetPolicyValue(setOf("package1", "package2")))
        }
    }

    @Test
    fun isPolicyApplied_packageSetUnion_setPartiallyIncluded_returnsFalse() {
        val resolutionMechanism = PackageSetUnion()

        assertFalse {
            resolutionMechanism.isPolicyApplied(
                PackageSetPolicyValue(setOf("package1", "package3")),
                PackageSetPolicyValue(setOf("package1", "package2")))
        }
    }

    companion object {
        private const val SYSTEM_USER_ID = UserHandle.USER_SYSTEM
        private val SYSTEM_ADMIN = EnforcingAdmin.createSystemEnforcingAdmin("system_entity")