Merge changes Ic5a3e169,I76daa3ab
* changes: Refactor applyUnderlyingCapabilities and its test. Move applyUnderlyingCapabilities to ConnectivityService.
This commit is contained in:
@@ -56,8 +56,10 @@ import static android.net.NetworkCapabilities.NET_CAPABILITY_IA;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_IMS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_MMS;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY;
|
||||
@@ -5397,6 +5399,106 @@ public class ConnectivityServiceTest {
|
||||
assertTrue(lp.getDnsServers().containsAll(dnsServers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApplyUnderlyingCapabilities() throws Exception {
|
||||
mCellNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_CELLULAR);
|
||||
mWiFiNetworkAgent = new TestNetworkAgentWrapper(TRANSPORT_WIFI);
|
||||
mCellNetworkAgent.connect(false /* validated */);
|
||||
mWiFiNetworkAgent.connect(false /* validated */);
|
||||
|
||||
final NetworkCapabilities cellNc = new NetworkCapabilities()
|
||||
.addTransportType(TRANSPORT_CELLULAR)
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NET_CAPABILITY_NOT_CONGESTED)
|
||||
.setLinkDownstreamBandwidthKbps(10);
|
||||
final NetworkCapabilities wifiNc = new NetworkCapabilities()
|
||||
.addTransportType(TRANSPORT_WIFI)
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NET_CAPABILITY_NOT_METERED)
|
||||
.addCapability(NET_CAPABILITY_NOT_ROAMING)
|
||||
.addCapability(NET_CAPABILITY_NOT_CONGESTED)
|
||||
.addCapability(NET_CAPABILITY_NOT_SUSPENDED)
|
||||
.setLinkUpstreamBandwidthKbps(20);
|
||||
mCellNetworkAgent.setNetworkCapabilities(cellNc, true /* sendToConnectivityService */);
|
||||
mWiFiNetworkAgent.setNetworkCapabilities(wifiNc, true /* sendToConnectivityService */);
|
||||
waitForIdle();
|
||||
|
||||
final Network mobile = mCellNetworkAgent.getNetwork();
|
||||
final Network wifi = mWiFiNetworkAgent.getNetwork();
|
||||
|
||||
final NetworkCapabilities initialCaps = new NetworkCapabilities();
|
||||
initialCaps.addCapability(NET_CAPABILITY_INTERNET);
|
||||
initialCaps.removeCapability(NET_CAPABILITY_NOT_VPN);
|
||||
|
||||
final NetworkCapabilities withNoUnderlying = new NetworkCapabilities();
|
||||
withNoUnderlying.addCapability(NET_CAPABILITY_INTERNET);
|
||||
withNoUnderlying.addCapability(NET_CAPABILITY_NOT_CONGESTED);
|
||||
withNoUnderlying.addCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
withNoUnderlying.addCapability(NET_CAPABILITY_NOT_SUSPENDED);
|
||||
withNoUnderlying.addTransportType(TRANSPORT_VPN);
|
||||
withNoUnderlying.removeCapability(NET_CAPABILITY_NOT_VPN);
|
||||
|
||||
final NetworkCapabilities withMobileUnderlying = new NetworkCapabilities(withNoUnderlying);
|
||||
withMobileUnderlying.addTransportType(TRANSPORT_CELLULAR);
|
||||
withMobileUnderlying.removeCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
withMobileUnderlying.removeCapability(NET_CAPABILITY_NOT_SUSPENDED);
|
||||
withMobileUnderlying.setLinkDownstreamBandwidthKbps(10);
|
||||
|
||||
final NetworkCapabilities withWifiUnderlying = new NetworkCapabilities(withNoUnderlying);
|
||||
withWifiUnderlying.addTransportType(TRANSPORT_WIFI);
|
||||
withWifiUnderlying.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
withWifiUnderlying.setLinkUpstreamBandwidthKbps(20);
|
||||
|
||||
final NetworkCapabilities withWifiAndMobileUnderlying =
|
||||
new NetworkCapabilities(withNoUnderlying);
|
||||
withWifiAndMobileUnderlying.addTransportType(TRANSPORT_CELLULAR);
|
||||
withWifiAndMobileUnderlying.addTransportType(TRANSPORT_WIFI);
|
||||
withWifiAndMobileUnderlying.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
withWifiAndMobileUnderlying.removeCapability(NET_CAPABILITY_NOT_ROAMING);
|
||||
withWifiAndMobileUnderlying.setLinkDownstreamBandwidthKbps(10);
|
||||
withWifiAndMobileUnderlying.setLinkUpstreamBandwidthKbps(20);
|
||||
|
||||
NetworkCapabilities caps = new NetworkCapabilities(initialCaps);
|
||||
final boolean notDeclaredMetered = false;
|
||||
mService.applyUnderlyingCapabilities(new Network[]{}, caps, notDeclaredMetered);
|
||||
assertEquals(withNoUnderlying, caps);
|
||||
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{null}, caps, notDeclaredMetered);
|
||||
assertEquals(withNoUnderlying, caps);
|
||||
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{mobile}, caps, notDeclaredMetered);
|
||||
assertEquals(withMobileUnderlying, caps);
|
||||
|
||||
mService.applyUnderlyingCapabilities(new Network[]{wifi}, caps, notDeclaredMetered);
|
||||
assertEquals(withWifiUnderlying, caps);
|
||||
|
||||
final boolean isDeclaredMetered = true;
|
||||
withWifiUnderlying.removeCapability(NET_CAPABILITY_NOT_METERED);
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{wifi}, caps, isDeclaredMetered);
|
||||
assertEquals(withWifiUnderlying, caps);
|
||||
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{mobile, wifi}, caps, isDeclaredMetered);
|
||||
assertEquals(withWifiAndMobileUnderlying, caps);
|
||||
|
||||
withWifiUnderlying.addCapability(NET_CAPABILITY_NOT_METERED);
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{null, mobile, null, wifi},
|
||||
caps, notDeclaredMetered);
|
||||
assertEquals(withWifiAndMobileUnderlying, caps);
|
||||
|
||||
caps = new NetworkCapabilities(initialCaps);
|
||||
mService.applyUnderlyingCapabilities(new Network[]{null, mobile, null, wifi},
|
||||
caps, notDeclaredMetered);
|
||||
assertEquals(withWifiAndMobileUnderlying, caps);
|
||||
|
||||
mService.applyUnderlyingCapabilities(null, caps, notDeclaredMetered);
|
||||
assertEquals(withWifiUnderlying, caps);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVpnConnectDisconnectUnderlyingNetwork() throws Exception {
|
||||
final TestNetworkCallback callback = new TestNetworkCallback();
|
||||
@@ -5947,17 +6049,28 @@ public class ConnectivityServiceTest {
|
||||
&& caps.hasTransport(TRANSPORT_VPN)
|
||||
&& caps.hasTransport(TRANSPORT_WIFI));
|
||||
|
||||
// Change the VPN's capabilities somehow (specifically, disconnect wifi).
|
||||
mWiFiNetworkAgent.disconnect();
|
||||
callback.expectCallback(CallbackEntry.LOST, mWiFiNetworkAgent);
|
||||
callback.expectCapabilitiesThat(mMockVpn, (caps)
|
||||
-> caps.getUids().size() == 2
|
||||
&& caps.getUids().contains(new UidRange(uid, uid))
|
||||
&& caps.getUids().contains(UidRange.createForUser(restrictedUserId))
|
||||
&& caps.hasTransport(TRANSPORT_VPN)
|
||||
&& !caps.hasTransport(TRANSPORT_WIFI));
|
||||
|
||||
// Send a USER_REMOVED broadcast and expect to lose the UID range for the restricted user.
|
||||
final Intent removedIntent = new Intent(ACTION_USER_REMOVED);
|
||||
removedIntent.putExtra(Intent.EXTRA_USER_HANDLE, restrictedUserId);
|
||||
handler.post(() -> mServiceContext.sendBroadcast(removedIntent));
|
||||
|
||||
// Expect that the VPN gains the UID range for the restricted user.
|
||||
// Expect that the VPN gains the UID range for the restricted user, and that the capability
|
||||
// change made just before that (i.e., loss of TRANSPORT_WIFI) is preserved.
|
||||
callback.expectCapabilitiesThat(mMockVpn, (caps)
|
||||
-> caps.getUids().size() == 1
|
||||
&& caps.getUids().contains(new UidRange(uid, uid))
|
||||
&& caps.hasTransport(TRANSPORT_VPN)
|
||||
&& caps.hasTransport(TRANSPORT_WIFI));
|
||||
&& !caps.hasTransport(TRANSPORT_WIFI));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -21,15 +21,6 @@ import static android.content.pm.UserInfo.FLAG_MANAGED_PROFILE;
|
||||
import static android.content.pm.UserInfo.FLAG_PRIMARY;
|
||||
import static android.content.pm.UserInfo.FLAG_RESTRICTED;
|
||||
import static android.net.ConnectivityManager.NetworkCallback;
|
||||
import static android.net.NetworkCapabilities.LINK_BANDWIDTH_UNSPECIFIED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING;
|
||||
import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_VPN;
|
||||
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -621,102 +612,6 @@ public class VpnTest {
|
||||
order.verify(mNotificationManager).cancel(anyString(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCapabilities() {
|
||||
setMockedUsers(primaryUser);
|
||||
|
||||
final Network mobile = new Network(1);
|
||||
final Network wifi = new Network(2);
|
||||
|
||||
final Map<Network, NetworkCapabilities> networks = new HashMap<>();
|
||||
networks.put(
|
||||
mobile,
|
||||
new NetworkCapabilities()
|
||||
.addTransportType(TRANSPORT_CELLULAR)
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NET_CAPABILITY_NOT_CONGESTED)
|
||||
.setLinkDownstreamBandwidthKbps(10));
|
||||
networks.put(
|
||||
wifi,
|
||||
new NetworkCapabilities()
|
||||
.addTransportType(TRANSPORT_WIFI)
|
||||
.addCapability(NET_CAPABILITY_INTERNET)
|
||||
.addCapability(NET_CAPABILITY_NOT_METERED)
|
||||
.addCapability(NET_CAPABILITY_NOT_ROAMING)
|
||||
.addCapability(NET_CAPABILITY_NOT_CONGESTED)
|
||||
.addCapability(NET_CAPABILITY_NOT_SUSPENDED)
|
||||
.setLinkUpstreamBandwidthKbps(20));
|
||||
setMockedNetworks(networks);
|
||||
|
||||
final NetworkCapabilities caps = new NetworkCapabilities();
|
||||
|
||||
Vpn.applyUnderlyingCapabilities(
|
||||
mConnectivityManager, new Network[] {}, caps, false /* isAlwaysMetered */);
|
||||
assertTrue(caps.hasTransport(TRANSPORT_VPN));
|
||||
assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
|
||||
assertFalse(caps.hasTransport(TRANSPORT_WIFI));
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkDownstreamBandwidthKbps());
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkUpstreamBandwidthKbps());
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
|
||||
|
||||
Vpn.applyUnderlyingCapabilities(
|
||||
mConnectivityManager,
|
||||
new Network[] {mobile},
|
||||
caps,
|
||||
false /* isAlwaysMetered */);
|
||||
assertTrue(caps.hasTransport(TRANSPORT_VPN));
|
||||
assertTrue(caps.hasTransport(TRANSPORT_CELLULAR));
|
||||
assertFalse(caps.hasTransport(TRANSPORT_WIFI));
|
||||
assertEquals(10, caps.getLinkDownstreamBandwidthKbps());
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkUpstreamBandwidthKbps());
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
|
||||
|
||||
Vpn.applyUnderlyingCapabilities(
|
||||
mConnectivityManager, new Network[] {wifi}, caps, false /* isAlwaysMetered */);
|
||||
assertTrue(caps.hasTransport(TRANSPORT_VPN));
|
||||
assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
|
||||
assertTrue(caps.hasTransport(TRANSPORT_WIFI));
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkDownstreamBandwidthKbps());
|
||||
assertEquals(20, caps.getLinkUpstreamBandwidthKbps());
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
|
||||
|
||||
Vpn.applyUnderlyingCapabilities(
|
||||
mConnectivityManager, new Network[] {wifi}, caps, true /* isAlwaysMetered */);
|
||||
assertTrue(caps.hasTransport(TRANSPORT_VPN));
|
||||
assertFalse(caps.hasTransport(TRANSPORT_CELLULAR));
|
||||
assertTrue(caps.hasTransport(TRANSPORT_WIFI));
|
||||
assertEquals(LINK_BANDWIDTH_UNSPECIFIED, caps.getLinkDownstreamBandwidthKbps());
|
||||
assertEquals(20, caps.getLinkUpstreamBandwidthKbps());
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
|
||||
|
||||
Vpn.applyUnderlyingCapabilities(
|
||||
mConnectivityManager,
|
||||
new Network[] {mobile, wifi},
|
||||
caps,
|
||||
false /* isAlwaysMetered */);
|
||||
assertTrue(caps.hasTransport(TRANSPORT_VPN));
|
||||
assertTrue(caps.hasTransport(TRANSPORT_CELLULAR));
|
||||
assertTrue(caps.hasTransport(TRANSPORT_WIFI));
|
||||
assertEquals(10, caps.getLinkDownstreamBandwidthKbps());
|
||||
assertEquals(20, caps.getLinkUpstreamBandwidthKbps());
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_METERED));
|
||||
assertFalse(caps.hasCapability(NET_CAPABILITY_NOT_ROAMING));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED));
|
||||
assertTrue(caps.hasCapability(NET_CAPABILITY_NOT_SUSPENDED));
|
||||
}
|
||||
|
||||
/**
|
||||
* The profile name should NOT change between releases for backwards compatibility
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user