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

Commit 2d1ab90a authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Fix NetworkPolicyManager unit tests.

Bug: 137859686
Test: atest core/tests/coretests/src/android/net/NetworkPolicyManagerTest.java
Test: atest services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java
Change-Id: I38a935c71bfb1f78739d508b400e43dc1b52bef5
Merged-In: I38a935c71bfb1f78739d508b400e43dc1b52bef5
(cherry picked from commit 6ec3caf2)
parent 936fd348
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -251,7 +251,7 @@ public class DebugUtils {
                    if (value == 0 && flagsWasZero) {
                        return constNameWithoutPrefix(prefix, field);
                    }
                    if ((flags & value) == value) {
                    if (value != 0 && (flags & value) == value) {
                        flags &= ~value;
                        res.append(constNameWithoutPrefix(prefix, field)).append('|');
                    }
+5 −0
Original line number Diff line number Diff line
@@ -1509,6 +1509,11 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
        latch.await(5, TimeUnit.SECONDS);
    }

    @VisibleForTesting
    Handler getHandlerForTesting() {
        return mHandler;
    }

    /**
     * Update mobile policies with data cycle information from {@link CarrierConfigManager}
     * if necessary.
+30 −6
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ import android.net.NetworkStatsHistory;
import android.net.NetworkTemplate;
import android.net.StringNetworkSpecifier;
import android.os.Binder;
import android.os.Handler;
import android.os.INetworkManagementService;
import android.os.PersistableBundle;
import android.os.PowerManagerInternal;
@@ -1047,7 +1048,7 @@ public class NetworkPolicyManagerServiceTest {
        // Define simple data plan
        final SubscriptionPlan plan = buildMonthlyDataPlan(
                ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), DataUnit.MEGABYTES.toBytes(1800));
        mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan },
        setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan },
                mServiceContext.getOpPackageName());

        // We're 20% through the month (6 days)
@@ -1171,7 +1172,7 @@ public class NetworkPolicyManagerServiceTest {
        // Define simple data plan which gives us effectively 60MB/day
        final SubscriptionPlan plan = buildMonthlyDataPlan(
                ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), DataUnit.MEGABYTES.toBytes(1800));
        mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan },
        setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[] { plan },
                mServiceContext.getOpPackageName());

        // We're 20% through the month (6 days)
@@ -1387,6 +1388,8 @@ public class NetworkPolicyManagerServiceTest {
        when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]);
        when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID});
        when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID);
        when(mTelephonyManager.createForSubscriptionId(FAKE_SUB_ID))
                .thenReturn(mock(TelephonyManager.class));
        PersistableBundle bundle = CarrierConfigManager.getDefaultConfig();
        when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(bundle);
        setNetworkPolicies(buildDefaultFakeMobilePolicy());
@@ -1398,6 +1401,8 @@ public class NetworkPolicyManagerServiceTest {
        when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[0]);
        when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(new int[]{FAKE_SUB_ID});
        when(mTelephonyManager.getSubscriberId(FAKE_SUB_ID)).thenReturn(FAKE_SUBSCRIBER_ID);
        when(mTelephonyManager.createForSubscriptionId(FAKE_SUB_ID))
                .thenReturn(mock(TelephonyManager.class));
        when(mCarrierConfigManager.getConfigForSubId(FAKE_SUB_ID)).thenReturn(null);
        setNetworkPolicies(buildDefaultFakeMobilePolicy());
        // smoke test to make sure no errors are raised
@@ -1583,7 +1588,7 @@ public class NetworkPolicyManagerServiceTest {
            final SubscriptionPlan plan = buildMonthlyDataPlan(
                    ZonedDateTime.parse("2015-11-01T00:00:00.00Z"),
                    DataUnit.MEGABYTES.toBytes(1800));
            mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
            setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
                    mServiceContext.getOpPackageName());

            reset(mTelephonyManager, mNetworkManager, mNotifManager);
@@ -1604,7 +1609,7 @@ public class NetworkPolicyManagerServiceTest {
            final SubscriptionPlan plan = buildMonthlyDataPlan(
                    ZonedDateTime.parse("2015-11-01T00:00:00.00Z"),
                    DataUnit.MEGABYTES.toBytes(100));
            mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
            setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
                    mServiceContext.getOpPackageName());

            reset(mTelephonyManager, mNetworkManager, mNotifManager);
@@ -1620,7 +1625,7 @@ public class NetworkPolicyManagerServiceTest {
        {
            final SubscriptionPlan plan = buildMonthlyDataPlan(
                    ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), BYTES_UNLIMITED);
            mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
            setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
                    mServiceContext.getOpPackageName());

            reset(mTelephonyManager, mNetworkManager, mNotifManager);
@@ -1637,7 +1642,7 @@ public class NetworkPolicyManagerServiceTest {
        {
            final SubscriptionPlan plan = buildMonthlyDataPlan(
                    ZonedDateTime.parse("2015-11-01T00:00:00.00Z"), BYTES_UNLIMITED);
            mService.setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
            setSubscriptionPlans(TEST_SUB_ID, new SubscriptionPlan[]{plan},
                    mServiceContext.getOpPackageName());

            reset(mTelephonyManager, mNetworkManager, mNotifManager);
@@ -1853,6 +1858,8 @@ public class NetworkPolicyManagerServiceTest {
        when(mSubscriptionManager.getActiveSubscriptionIdList()).thenReturn(
                new int[] { TEST_SUB_ID });
        when(mTelephonyManager.getSubscriberId(TEST_SUB_ID)).thenReturn(TEST_IMSI);
        when(mTelephonyManager.createForSubscriptionId(TEST_SUB_ID))
                .thenReturn(mock(TelephonyManager.class));
        doNothing().when(mTelephonyManager).setPolicyDataEnabled(anyBoolean(), anyInt());
        expectNetworkState(false /* roaming */);
    }
@@ -1979,6 +1986,23 @@ public class NetworkPolicyManagerServiceTest {

    private FutureIntent mRestrictBackgroundChanged;

    private void postMsgAndWaitForCompletion() throws InterruptedException {
        final Handler handler = mService.getHandlerForTesting();
        final CountDownLatch latch = new CountDownLatch(1);
        mService.getHandlerForTesting().post(latch::countDown);
        if (!latch.await(5, TimeUnit.SECONDS)) {
            fail("Timed out waiting for the test msg to be handled");
        }
    }

    private void setSubscriptionPlans(int subId, SubscriptionPlan[] plans, String callingPackage)
            throws InterruptedException {
        mService.setSubscriptionPlans(subId, plans, callingPackage);
        // setSubscriptionPlans() triggers async events, wait for those to be completed before
        // moving forward as they could interfere with the tests later.
        postMsgAndWaitForCompletion();
    }

    private void setRestrictBackground(boolean flag) throws Exception {
        mService.setRestrictBackground(flag);
        // Sanity check.