Merge "Move some VPN logic out of ConnectivityService"
am: a3cdab05a8
Change-Id: I212c2d6de1676ab65e6bbf34148d66e183d60c62
This commit is contained in:
@@ -3737,8 +3737,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
vpn.setAlwaysOnPackage(null, false);
|
vpn.setAlwaysOnPackage(null, false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
vpn.saveAlwaysOnPackage();
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -3899,15 +3897,6 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
|||||||
}
|
}
|
||||||
userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, userId);
|
userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, userId);
|
||||||
mVpns.put(userId, userVpn);
|
mVpns.put(userId, userVpn);
|
||||||
|
|
||||||
final ContentResolver cr = mContext.getContentResolver();
|
|
||||||
String alwaysOnPackage = Settings.Secure.getStringForUser(cr,
|
|
||||||
Settings.Secure.ALWAYS_ON_VPN_APP, userId);
|
|
||||||
final boolean alwaysOnLockdown = Settings.Secure.getIntForUser(cr,
|
|
||||||
Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, /* default */ 0, userId) != 0;
|
|
||||||
if (alwaysOnPackage != null) {
|
|
||||||
userVpn.setAlwaysOnPackage(alwaysOnPackage, alwaysOnLockdown);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) {
|
if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) {
|
||||||
updateLockdownVpn();
|
updateLockdownVpn();
|
||||||
|
|||||||
@@ -27,10 +27,12 @@ import android.annotation.UserIdInt;
|
|||||||
import android.app.AppOpsManager;
|
import android.app.AppOpsManager;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.UserInfo;
|
import android.content.pm.UserInfo;
|
||||||
import android.net.NetworkInfo.DetailedState;
|
import android.net.NetworkInfo.DetailedState;
|
||||||
import android.net.UidRange;
|
import android.net.UidRange;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.INetworkManagementService;
|
import android.os.INetworkManagementService;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
@@ -45,6 +47,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mockito.Answers;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
@@ -87,12 +90,13 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Mock private Context mContext;
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext;
|
||||||
@Mock private UserManager mUserManager;
|
@Mock private UserManager mUserManager;
|
||||||
@Mock private PackageManager mPackageManager;
|
@Mock private PackageManager mPackageManager;
|
||||||
@Mock private INetworkManagementService mNetService;
|
@Mock private INetworkManagementService mNetService;
|
||||||
@Mock private AppOpsManager mAppOps;
|
@Mock private AppOpsManager mAppOps;
|
||||||
@Mock private NotificationManager mNotificationManager;
|
@Mock private NotificationManager mNotificationManager;
|
||||||
|
@Mock private Vpn.SystemServices mSystemServices;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
@@ -104,6 +108,12 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
when(mContext.getSystemService(eq(Context.APP_OPS_SERVICE))).thenReturn(mAppOps);
|
when(mContext.getSystemService(eq(Context.APP_OPS_SERVICE))).thenReturn(mAppOps);
|
||||||
when(mContext.getSystemService(eq(Context.NOTIFICATION_SERVICE)))
|
when(mContext.getSystemService(eq(Context.NOTIFICATION_SERVICE)))
|
||||||
.thenReturn(mNotificationManager);
|
.thenReturn(mNotificationManager);
|
||||||
|
|
||||||
|
// Used by {@link Notification.Builder}
|
||||||
|
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||||
|
applicationInfo.targetSdkVersion = Build.VERSION_CODES.CUR_DEVELOPMENT;
|
||||||
|
when(mContext.getApplicationInfo()).thenReturn(applicationInfo);
|
||||||
|
|
||||||
doNothing().when(mNetService).registerObserver(any());
|
doNothing().when(mNetService).registerObserver(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +121,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
public void testRestrictedProfilesAreAddedToVpn() {
|
public void testRestrictedProfilesAreAddedToVpn() {
|
||||||
setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB);
|
setMockedUsers(primaryUser, secondaryUser, restrictedProfileA, restrictedProfileB);
|
||||||
|
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
|
final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
|
||||||
null, null);
|
null, null);
|
||||||
|
|
||||||
@@ -125,7 +135,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
public void testManagedProfilesAreNotAddedToVpn() {
|
public void testManagedProfilesAreNotAddedToVpn() {
|
||||||
setMockedUsers(primaryUser, managedProfileA);
|
setMockedUsers(primaryUser, managedProfileA);
|
||||||
|
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
|
final Set<UidRange> ranges = vpn.createUserAndRestrictedProfilesRanges(primaryUser.id,
|
||||||
null, null);
|
null, null);
|
||||||
|
|
||||||
@@ -138,7 +148,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
public void testAddUserToVpnOnlyAddsOneUser() {
|
public void testAddUserToVpnOnlyAddsOneUser() {
|
||||||
setMockedUsers(primaryUser, restrictedProfileA, managedProfileA);
|
setMockedUsers(primaryUser, restrictedProfileA, managedProfileA);
|
||||||
|
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final Set<UidRange> ranges = new ArraySet<>();
|
final Set<UidRange> ranges = new ArraySet<>();
|
||||||
vpn.addUserToRanges(ranges, primaryUser.id, null, null);
|
vpn.addUserToRanges(ranges, primaryUser.id, null, null);
|
||||||
|
|
||||||
@@ -149,7 +159,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public void testUidWhiteAndBlacklist() throws Exception {
|
public void testUidWhiteAndBlacklist() throws Exception {
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRange user = UidRange.createForUser(primaryUser.id);
|
final UidRange user = UidRange.createForUser(primaryUser.id);
|
||||||
final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
|
final String[] packages = {PKGS[0], PKGS[1], PKGS[2]};
|
||||||
|
|
||||||
@@ -174,7 +184,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public void testLockdownChangingPackage() throws Exception {
|
public void testLockdownChangingPackage() throws Exception {
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
final UidRange user = UidRange.createForUser(primaryUser.id);
|
final UidRange user = UidRange.createForUser(primaryUser.id);
|
||||||
|
|
||||||
// Default state.
|
// Default state.
|
||||||
@@ -209,7 +219,7 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public void testLockdownAddingAProfile() throws Exception {
|
public void testLockdownAddingAProfile() throws Exception {
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
setMockedUsers(primaryUser);
|
setMockedUsers(primaryUser);
|
||||||
|
|
||||||
// Make a copy of the restricted profile, as we're going to mark it deleted halfway through.
|
// Make a copy of the restricted profile, as we're going to mark it deleted halfway through.
|
||||||
@@ -249,40 +259,41 @@ public class VpnTest extends AndroidTestCase {
|
|||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
public void testNotificationShownForAlwaysOnApp() {
|
public void testNotificationShownForAlwaysOnApp() {
|
||||||
final Vpn vpn = spyVpn(primaryUser.id);
|
final UserHandle userHandle = UserHandle.of(primaryUser.id);
|
||||||
final InOrder order = inOrder(vpn);
|
final Vpn vpn = createVpn(primaryUser.id);
|
||||||
setMockedUsers(primaryUser);
|
setMockedUsers(primaryUser);
|
||||||
|
|
||||||
|
final InOrder order = inOrder(mNotificationManager);
|
||||||
|
|
||||||
// Don't show a notification for regular disconnected states.
|
// Don't show a notification for regular disconnected states.
|
||||||
vpn.updateState(DetailedState.DISCONNECTED, TAG);
|
vpn.updateState(DetailedState.DISCONNECTED, TAG);
|
||||||
order.verify(vpn).updateAlwaysOnNotificationInternal(false);
|
order.verify(mNotificationManager, atLeastOnce())
|
||||||
|
.cancelAsUser(anyString(), anyInt(), eq(userHandle));
|
||||||
|
|
||||||
// Start showing a notification for disconnected once always-on.
|
// Start showing a notification for disconnected once always-on.
|
||||||
vpn.setAlwaysOnPackage(PKGS[0], false);
|
vpn.setAlwaysOnPackage(PKGS[0], false);
|
||||||
order.verify(vpn).updateAlwaysOnNotificationInternal(true);
|
order.verify(mNotificationManager)
|
||||||
|
.notifyAsUser(anyString(), anyInt(), any(), eq(userHandle));
|
||||||
|
|
||||||
// Stop showing the notification once connected.
|
// Stop showing the notification once connected.
|
||||||
vpn.updateState(DetailedState.CONNECTED, TAG);
|
vpn.updateState(DetailedState.CONNECTED, TAG);
|
||||||
order.verify(vpn).updateAlwaysOnNotificationInternal(false);
|
order.verify(mNotificationManager).cancelAsUser(anyString(), anyInt(), eq(userHandle));
|
||||||
|
|
||||||
// Show the notification if we disconnect again.
|
// Show the notification if we disconnect again.
|
||||||
vpn.updateState(DetailedState.DISCONNECTED, TAG);
|
vpn.updateState(DetailedState.DISCONNECTED, TAG);
|
||||||
order.verify(vpn).updateAlwaysOnNotificationInternal(true);
|
order.verify(mNotificationManager)
|
||||||
|
.notifyAsUser(anyString(), anyInt(), any(), eq(userHandle));
|
||||||
|
|
||||||
// Notification should be cleared after unsetting always-on package.
|
// Notification should be cleared after unsetting always-on package.
|
||||||
vpn.setAlwaysOnPackage(null, false);
|
vpn.setAlwaysOnPackage(null, false);
|
||||||
order.verify(vpn).updateAlwaysOnNotificationInternal(false);
|
order.verify(mNotificationManager).cancelAsUser(anyString(), anyInt(), eq(userHandle));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mock some methods of vpn object.
|
* Mock some methods of vpn object.
|
||||||
*/
|
*/
|
||||||
private Vpn spyVpn(@UserIdInt int userId) {
|
private Vpn createVpn(@UserIdInt int userId) {
|
||||||
final Vpn vpn = spy(new Vpn(Looper.myLooper(), mContext, mNetService, userId));
|
return new Vpn(Looper.myLooper(), mContext, mNetService, userId, mSystemServices);
|
||||||
|
|
||||||
// Block calls to the NotificationManager or PendingIntent#getActivity.
|
|
||||||
doNothing().when(vpn).updateAlwaysOnNotificationInternal(anyBoolean());
|
|
||||||
return vpn;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertBlocked(Vpn vpn, int... uids) {
|
private static void assertBlocked(Vpn vpn, int... uids) {
|
||||||
|
|||||||
Reference in New Issue
Block a user