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

Commit 26409bfe authored by Cody Kesting's avatar Cody Kesting
Browse files

Notify PolicyListeners to refresh their policy on VCN changes.

This CL updates VcnManagementService to notify all registered
VcnUnderlyingNetworkPolicyListeners to refresh their
UnderlyingNetworkPolicy when any VCN is added or removed.

Bug: 175914059
Test: atest FrameworksVcnTests
Change-Id: Ie87f4aa3401c6c4ba9f932130a1d475e35f59d4e
parent 50e9bf04
Loading
Loading
Loading
Loading
+27 −4
Original line number Diff line number Diff line
@@ -397,7 +397,7 @@ public class VcnManagementService extends IVcnManagementService.Stub {
                                // correct instance is torn down. This could happen as a result of a
                                // Carrier App manually removing/adding a VcnConfig.
                                if (mVcns.get(uuidToTeardown) == instanceToTeardown) {
                                    mVcns.remove(uuidToTeardown).teardownAsynchronously();
                                    stopVcnLocked(uuidToTeardown);
                                }
                            }
                        }, instanceToTeardown, CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS);
@@ -410,6 +410,27 @@ public class VcnManagementService extends IVcnManagementService.Stub {
        }
    }

    @GuardedBy("mLock")
    private void stopVcnLocked(@NonNull ParcelUuid uuidToTeardown) {
        final Vcn vcnToTeardown = mVcns.remove(uuidToTeardown);
        if (vcnToTeardown == null) {
            return;
        }

        vcnToTeardown.teardownAsynchronously();

        // Now that the VCN is removed, notify all registered listeners to refresh their
        // UnderlyingNetworkPolicy.
        notifyAllPolicyListenersLocked();
    }

    @GuardedBy("mLock")
    private void notifyAllPolicyListenersLocked() {
        for (final PolicyListenerBinderDeath policyListener : mRegisteredPolicyListeners.values()) {
            Binder.withCleanCallingIdentity(() -> policyListener.mListener.onPolicyChanged());
        }
    }

    @GuardedBy("mLock")
    private void startVcnLocked(@NonNull ParcelUuid subscriptionGroup, @NonNull VcnConfig config) {
        Slog.v(TAG, "Starting VCN config for subGrp: " + subscriptionGroup);
@@ -419,6 +440,10 @@ public class VcnManagementService extends IVcnManagementService.Stub {

        final Vcn newInstance = mDeps.newVcn(mVcnContext, subscriptionGroup, config, mLastSnapshot);
        mVcns.put(subscriptionGroup, newInstance);

        // Now that a new VCN has started, notify all registered listeners to refresh their
        // UnderlyingNetworkPolicy.
        notifyAllPolicyListenersLocked();
    }

    @GuardedBy("mLock")
@@ -481,9 +506,7 @@ public class VcnManagementService extends IVcnManagementService.Stub {
            synchronized (mLock) {
                mConfigs.remove(subscriptionGroup);

                if (mVcns.containsKey(subscriptionGroup)) {
                    mVcns.remove(subscriptionGroup).teardownAsynchronously();
                }
                stopVcnLocked(subscriptionGroup);

                writeConfigsToDiskLocked();
            }
+24 −0
Original line number Diff line number Diff line
@@ -314,6 +314,7 @@ public class VcnManagementServiceTest {
    public void testTelephonyNetworkTrackerCallbackStopsInstances() throws Exception {
        final TelephonySubscriptionTrackerCallback cb = getTelephonySubscriptionTrackerCallback();
        final Vcn vcn = startAndGetVcnInstance(TEST_UUID_2);
        mVcnMgmtSvc.addVcnUnderlyingNetworkPolicyListener(mMockPolicyListener);

        triggerSubscriptionTrackerCbAndGetSnapshot(Collections.emptySet());

@@ -321,6 +322,7 @@ public class VcnManagementServiceTest {
        mTestLooper.moveTimeForward(VcnManagementService.CARRIER_PRIVILEGES_LOST_TEARDOWN_DELAY_MS);
        mTestLooper.dispatchAll();
        verify(vcn).teardownAsynchronously();
        verify(mMockPolicyListener).onPolicyChanged();
    }

    @Test
@@ -391,6 +393,7 @@ public class VcnManagementServiceTest {
            mVcnMgmtSvc.setVcnConfig(TEST_UUID_1, TEST_VCN_CONFIG, TEST_PACKAGE_NAME);
            fail("Expected security exception for non system user");
        } catch (SecurityException expected) {
            verify(mMockPolicyListener, never()).onPolicyChanged();
        }
    }

@@ -402,6 +405,7 @@ public class VcnManagementServiceTest {
            mVcnMgmtSvc.setVcnConfig(TEST_UUID_1, TEST_VCN_CONFIG, TEST_PACKAGE_NAME);
            fail("Expected security exception for missing carrier privileges");
        } catch (SecurityException expected) {
            verify(mMockPolicyListener, never()).onPolicyChanged();
        }
    }

@@ -411,6 +415,7 @@ public class VcnManagementServiceTest {
            mVcnMgmtSvc.setVcnConfig(TEST_UUID_1, TEST_VCN_CONFIG, "IncorrectPackage");
            fail("Expected exception due to mismatched packages in config and method call");
        } catch (IllegalArgumentException expected) {
            verify(mMockPolicyListener, never()).onPolicyChanged();
        }
    }

@@ -610,4 +615,23 @@ public class VcnManagementServiceTest {

        verify(vcnInstance).updateSubscriptionSnapshot(eq(snapshot));
    }

    @Test
    public void testAddNewVcnUpdatesPolicyListener() throws Exception {
        mVcnMgmtSvc.addVcnUnderlyingNetworkPolicyListener(mMockPolicyListener);

        mVcnMgmtSvc.setVcnConfig(TEST_UUID_2, TEST_VCN_CONFIG, TEST_PACKAGE_NAME);

        verify(mMockPolicyListener).onPolicyChanged();
    }

    @Test
    public void testRemoveVcnUpdatesPolicyListener() throws Exception {
        mVcnMgmtSvc.setVcnConfig(TEST_UUID_2, TEST_VCN_CONFIG, TEST_PACKAGE_NAME);
        mVcnMgmtSvc.addVcnUnderlyingNetworkPolicyListener(mMockPolicyListener);

        mVcnMgmtSvc.clearVcnConfig(TEST_UUID_2);

        verify(mMockPolicyListener).onPolicyChanged();
    }
}