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

Commit 6143f315 authored by Jeff Sharkey's avatar Jeff Sharkey Committed by Android (Google) Code Review
Browse files

Merge "NetworkPolicy multi-user tests."

parents ced11a5b cae04a29
Loading
Loading
Loading
Loading
+68 −11
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.content.pm.UserInfo;
import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
import android.net.INetworkManagementEventObserver;
@@ -69,6 +70,7 @@ import android.os.Binder;
import android.os.INetworkManagementService;
import android.os.IPowerManager;
import android.os.MessageQueue.IdleHandler;
import android.os.UserId;
import android.test.AndroidTestCase;
import android.test.mock.MockPackageManager;
import android.test.suitebuilder.annotation.LargeTest;
@@ -84,7 +86,9 @@ import org.easymock.EasyMock;
import org.easymock.IAnswer;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@@ -126,8 +130,16 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
    private long mStartTime;
    private long mElapsedRealtime;

    private static final int UID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
    private static final int UID_B = android.os.Process.FIRST_APPLICATION_UID + 801;
    private static final int USER_ID = 0;
    private static final int USER_ID_GUEST = 1;

    private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
    private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 801;

    private static final int UID_A = UserId.getUid(USER_ID, APP_ID_A);
    private static final int UID_B = UserId.getUid(USER_ID, APP_ID_B);
    private static final int UID_A_GUEST = UserId.getUid(USER_ID_GUEST, APP_ID_A);
    private static final int UID_B_GUEST = UserId.getUid(USER_ID_GUEST, APP_ID_B);

    private static final int PID_1 = 400;
    private static final int PID_2 = 401;
@@ -161,6 +173,14 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
                        info.signatures = new Signature[] { signature };
                        return info;
                    }

                    @Override
                    public List<UserInfo> getUsers() {
                        final ArrayList<UserInfo> users = new ArrayList<UserInfo>();
                        users.add(new UserInfo(USER_ID, "Primary", UserInfo.FLAG_PRIMARY));
                        users.add(new UserInfo(USER_ID_GUEST, "Guest", 0));
                        return users;
                    }
                };
            }

@@ -242,13 +262,13 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    @Suppress
    public void testPolicyChangeTriggersBroadcast() throws Exception {
        mService.setAppPolicy(UID_A, POLICY_NONE);
        mService.setAppPolicy(APP_ID_A, POLICY_NONE);

        // change background policy and expect broadcast
        final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent(
                ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);

        mService.setAppPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND);
        mService.setAppPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);

        backgroundChanged.get();
    }
@@ -302,6 +322,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    public void testScreenChangesRules() throws Exception {
        Future<Void> future;
        Future<Void> futureGuest;

        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, true);
@@ -314,10 +335,14 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
        // push strict policy for foreground uid, verify ALLOW rule
        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, true);
        expectSetUidNetworkRules(UID_A_GUEST, true);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_REJECT_METERED);
        replay();
        mService.setAppPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND);
        mService.setAppPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // now turn screen off and verify REJECT rule
@@ -343,6 +368,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    public void testPolicyNone() throws Exception {
        Future<Void> future;
        Future<Void> futureGuest;

        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, true);
@@ -355,10 +381,14 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
        // POLICY_NONE should RULE_ALLOW in foreground
        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, true);
        expectSetUidNetworkRules(UID_A_GUEST, false);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_ALLOW_ALL);
        replay();
        mService.setAppPolicy(UID_A, POLICY_NONE);
        mService.setAppPolicy(APP_ID_A, POLICY_NONE);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // POLICY_NONE should RULE_ALLOW in background
@@ -373,14 +403,19 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    public void testPolicyReject() throws Exception {
        Future<Void> future;
        Future<Void> futureGuest;

        // POLICY_REJECT should RULE_ALLOW in background
        expectSetUidNetworkRules(UID_A, true);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, true);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_REJECT_METERED);
        replay();
        mService.setAppPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND);
        mService.setAppPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // POLICY_REJECT should RULE_ALLOW in foreground
@@ -404,33 +439,46 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    public void testPolicyRejectAddRemove() throws Exception {
        Future<Void> future;
        Future<Void> futureGuest;

        // POLICY_NONE should have RULE_ALLOW in background
        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, false);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_ALLOW_ALL);
        replay();
        mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
        mService.setAppPolicy(UID_A, POLICY_NONE);
        mService.setAppPolicy(APP_ID_A, POLICY_NONE);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // adding POLICY_REJECT should cause RULE_REJECT
        expectSetUidNetworkRules(UID_A, true);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, true);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_REJECT_METERED);
        replay();
        mService.setAppPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND);
        mService.setAppPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // removing POLICY_REJECT should return us to RULE_ALLOW
        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, false);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_ALLOW_ALL);
        replay();
        mService.setAppPolicy(UID_A, POLICY_NONE);
        mService.setAppPolicy(APP_ID_A, POLICY_NONE);
        future.get();
        futureGuest.get();
        verifyAndReset();
    }

@@ -599,25 +647,34 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {

    public void testUidRemovedPolicyCleared() throws Exception {
        Future<Void> future;
        Future<Void> futureGuest;

        // POLICY_REJECT should RULE_REJECT in background
        expectSetUidNetworkRules(UID_A, true);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, true);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_REJECT_METERED);
        replay();
        mService.setAppPolicy(UID_A, POLICY_REJECT_METERED_BACKGROUND);
        mService.setAppPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
        future.get();
        futureGuest.get();
        verifyAndReset();

        // uninstall should clear RULE_REJECT
        expectSetUidNetworkRules(UID_A, false);
        expectSetUidForeground(UID_A, false);
        expectSetUidNetworkRules(UID_A_GUEST, false);
        expectSetUidForeground(UID_A_GUEST, false);
        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
        futureGuest = expectRulesChanged(UID_A_GUEST, RULE_ALLOW_ALL);
        replay();
        final Intent intent = new Intent(ACTION_UID_REMOVED);
        intent.putExtra(EXTRA_UID, UID_A);
        mServiceContext.sendBroadcast(intent);
        future.get();
        futureGuest.get();
        verifyAndReset();
    }