Merge "Give VPNs the INTERNET capability when they route most of the IP space" am: f477aedec5 am: 7506c131c1

am: 941de3f74c

Change-Id: Ifd77149ca2c45249b8776c878c6fac46ae81c2a4
This commit is contained in:
Chalard Jean
2018-03-02 12:54:29 +00:00
committed by android-build-merger
5 changed files with 350 additions and 12 deletions

View File

@@ -223,14 +223,14 @@ public class IpPrefixTest {
}
@Test
public void testContains() {
public void testContainsInetAddress() {
IpPrefix p = new IpPrefix("2001:db8:f00::ace:d00d/127");
assertTrue(p.contains(Address("2001:db8:f00::ace:d00c")));
assertTrue(p.contains(Address("2001:db8:f00::ace:d00d")));
assertFalse(p.contains(Address("2001:db8:f00::ace:d00e")));
assertFalse(p.contains(Address("2001:db8:f00::bad:d00d")));
assertFalse(p.contains(Address("2001:4868:4860::8888")));
assertFalse(p.contains(null));
assertFalse(p.contains((InetAddress)null));
assertFalse(p.contains(Address("8.8.8.8")));
p = new IpPrefix("192.0.2.0/23");
@@ -250,6 +250,53 @@ public class IpPrefixTest {
assertFalse(ipv4Default.contains(Address("2001:db8::f00")));
}
@Test
public void testContainsIpPrefix() {
assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("0.0.0.0/0")));
assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/0")));
assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/8")));
assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/24")));
assertTrue(new IpPrefix("0.0.0.0/0").containsPrefix(new IpPrefix("1.2.3.4/23")));
assertTrue(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("1.2.3.4/8")));
assertTrue(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("1.254.12.9/8")));
assertTrue(new IpPrefix("1.2.3.4/21").containsPrefix(new IpPrefix("1.2.3.4/21")));
assertTrue(new IpPrefix("1.2.3.4/32").containsPrefix(new IpPrefix("1.2.3.4/32")));
assertTrue(new IpPrefix("1.2.3.4/20").containsPrefix(new IpPrefix("1.2.3.0/24")));
assertFalse(new IpPrefix("1.2.3.4/32").containsPrefix(new IpPrefix("1.2.3.5/32")));
assertFalse(new IpPrefix("1.2.3.4/8").containsPrefix(new IpPrefix("2.2.3.4/8")));
assertFalse(new IpPrefix("0.0.0.0/16").containsPrefix(new IpPrefix("0.0.0.0/15")));
assertFalse(new IpPrefix("100.0.0.0/8").containsPrefix(new IpPrefix("99.0.0.0/8")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("::/0")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/1")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("3d8a:661:a0::770/8")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/8")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/64")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/113")));
assertTrue(new IpPrefix("::/0").containsPrefix(new IpPrefix("2001:db8::f00/128")));
assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix(
new IpPrefix("2001:db8:f00::ace:d00d/64")));
assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix(
new IpPrefix("2001:db8:f00::ace:d00d/120")));
assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix(
new IpPrefix("2001:db8:f00::ace:d00d/32")));
assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/64").containsPrefix(
new IpPrefix("2006:db8:f00::ace:d00d/96")));
assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/128").containsPrefix(
new IpPrefix("2001:db8:f00::ace:d00d/128")));
assertTrue(new IpPrefix("2001:db8:f00::ace:d00d/100").containsPrefix(
new IpPrefix("2001:db8:f00::ace:ccaf/110")));
assertFalse(new IpPrefix("2001:db8:f00::ace:d00d/128").containsPrefix(
new IpPrefix("2001:db8:f00::ace:d00e/128")));
assertFalse(new IpPrefix("::/30").containsPrefix(new IpPrefix("::/29")));
}
@Test
public void testHashCode() {
IpPrefix p = new IpPrefix(new byte[4], 0);

View File

@@ -19,8 +19,10 @@ package android.net;
import android.net.NetworkUtils;
import android.test.suitebuilder.annotation.SmallTest;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.TreeSet;
import junit.framework.TestCase;
@@ -67,4 +69,101 @@ public class NetworkUtilsTest extends TestCase {
assertInvalidNetworkMask(IPv4Address("255.255.255.253"));
assertInvalidNetworkMask(IPv4Address("255.255.0.255"));
}
@SmallTest
public void testRoutedIPv4AddressCount() {
final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator());
// No routes routes to no addresses.
assertEquals(0, NetworkUtils.routedIPv4AddressCount(set));
set.add(new IpPrefix("0.0.0.0/0"));
assertEquals(1l << 32, NetworkUtils.routedIPv4AddressCount(set));
set.add(new IpPrefix("20.18.0.0/16"));
set.add(new IpPrefix("20.18.0.0/24"));
set.add(new IpPrefix("20.18.0.0/8"));
// There is a default route, still covers everything
assertEquals(1l << 32, NetworkUtils.routedIPv4AddressCount(set));
set.clear();
set.add(new IpPrefix("20.18.0.0/24"));
set.add(new IpPrefix("20.18.0.0/8"));
// The 8-length includes the 24-length prefix
assertEquals(1l << 24, NetworkUtils.routedIPv4AddressCount(set));
set.add(new IpPrefix("10.10.10.126/25"));
// The 8-length does not include this 25-length prefix
assertEquals((1l << 24) + (1 << 7), NetworkUtils.routedIPv4AddressCount(set));
set.clear();
set.add(new IpPrefix("1.2.3.4/32"));
set.add(new IpPrefix("1.2.3.4/32"));
set.add(new IpPrefix("1.2.3.4/32"));
set.add(new IpPrefix("1.2.3.4/32"));
assertEquals(1l, NetworkUtils.routedIPv4AddressCount(set));
set.add(new IpPrefix("1.2.3.5/32"));
set.add(new IpPrefix("1.2.3.6/32"));
set.add(new IpPrefix("1.2.3.7/32"));
set.add(new IpPrefix("1.2.3.8/32"));
set.add(new IpPrefix("1.2.3.9/32"));
set.add(new IpPrefix("1.2.3.0/32"));
assertEquals(7l, NetworkUtils.routedIPv4AddressCount(set));
// 1.2.3.4/30 eats 1.2.3.{4-7}/32
set.add(new IpPrefix("1.2.3.4/30"));
set.add(new IpPrefix("6.2.3.4/28"));
set.add(new IpPrefix("120.2.3.4/16"));
assertEquals(7l - 4 + 4 + 16 + 65536, NetworkUtils.routedIPv4AddressCount(set));
}
@SmallTest
public void testRoutedIPv6AddressCount() {
final TreeSet<IpPrefix> set = new TreeSet<>(IpPrefix.lengthComparator());
// No routes routes to no addresses.
assertEquals(BigInteger.ZERO, NetworkUtils.routedIPv6AddressCount(set));
set.add(new IpPrefix("::/0"));
assertEquals(BigInteger.ONE.shiftLeft(128), NetworkUtils.routedIPv6AddressCount(set));
set.add(new IpPrefix("1234:622a::18/64"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6adb/96"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6adb/8"));
// There is a default route, still covers everything
assertEquals(BigInteger.ONE.shiftLeft(128), NetworkUtils.routedIPv6AddressCount(set));
set.clear();
set.add(new IpPrefix("add4:f00:80:f7:1111::6adb/96"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6adb/8"));
// The 8-length includes the 96-length prefix
assertEquals(BigInteger.ONE.shiftLeft(120), NetworkUtils.routedIPv6AddressCount(set));
set.add(new IpPrefix("10::26/64"));
// The 8-length does not include this 64-length prefix
assertEquals(BigInteger.ONE.shiftLeft(120).add(BigInteger.ONE.shiftLeft(64)),
NetworkUtils.routedIPv6AddressCount(set));
set.clear();
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad4/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad4/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad4/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad4/128"));
assertEquals(BigInteger.ONE, NetworkUtils.routedIPv6AddressCount(set));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad5/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad6/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad7/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad8/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad9/128"));
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad0/128"));
assertEquals(BigInteger.valueOf(7), NetworkUtils.routedIPv6AddressCount(set));
// add4:f00:80:f7:1111::6ad4/126 eats add4:f00:8[:f7:1111::6ad{4-7}/128
set.add(new IpPrefix("add4:f00:80:f7:1111::6ad4/126"));
set.add(new IpPrefix("d00d:f00:80:f7:1111::6ade/124"));
set.add(new IpPrefix("f00b:a33::/112"));
assertEquals(BigInteger.valueOf(7l - 4 + 4 + 16 + 65536),
NetworkUtils.routedIPv6AddressCount(set));
}
}

View File

@@ -57,9 +57,13 @@ import android.content.pm.ServiceInfo;
import android.content.pm.UserInfo;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.IConnectivityManager;
import android.net.IpPrefix;
import android.net.LinkProperties;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo.DetailedState;
import android.net.RouteInfo;
import android.net.UidRange;
import android.net.VpnService;
import android.os.Build.VERSION_CODES;
@@ -90,7 +94,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Tests for {@link Vpn}.
@@ -563,4 +568,75 @@ public class VpnTest {
return networks.get(network);
}).when(mConnectivityManager).getNetworkCapabilities(any());
}
// Need multiple copies of this, but Java's Stream objects can't be reused or
// duplicated.
private Stream<String> publicIpV4Routes() {
return Stream.of(
"0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4",
"32.0.0.0/3", "64.0.0.0/2", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/6",
"172.0.0.0/12", "172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9",
"173.0.0.0/8", "174.0.0.0/7", "176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11",
"192.160.0.0/13", "192.169.0.0/16", "192.170.0.0/15", "192.172.0.0/14",
"192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7",
"196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4");
}
private Stream<String> publicIpV6Routes() {
return Stream.of(
"::/1", "8000::/2", "c000::/3", "e000::/4", "f000::/5", "f800::/6",
"fe00::/8", "2605:ef80:e:af1d::/64");
}
@Test
public void testProvidesRoutesToMostDestinations() {
final LinkProperties lp = new LinkProperties();
// Default route provides routes to all IPv4 destinations.
lp.addRoute(new RouteInfo(new IpPrefix("0.0.0.0/0")));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
// Empty LP provides routes to no destination
lp.clear();
assertFalse(Vpn.providesRoutesToMostDestinations(lp));
// All IPv4 routes except for local networks. This is the case most relevant
// to this function. It provides routes to almost the entire space.
// (clone the stream so that we can reuse it later)
publicIpV4Routes().forEach(s -> lp.addRoute(new RouteInfo(new IpPrefix(s))));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
// Removing a 16-bit prefix, which is 65536 addresses. This is still enough to
// provide routes to "most" destinations.
lp.removeRoute(new RouteInfo(new IpPrefix("192.169.0.0/16")));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
// Remove the /2 route, which represent a quarter of the available routing space.
// This LP does not provides routes to "most" destinations any more.
lp.removeRoute(new RouteInfo(new IpPrefix("64.0.0.0/2")));
assertFalse(Vpn.providesRoutesToMostDestinations(lp));
lp.clear();
publicIpV6Routes().forEach(s -> lp.addRoute(new RouteInfo(new IpPrefix(s))));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
lp.removeRoute(new RouteInfo(new IpPrefix("::/1")));
assertFalse(Vpn.providesRoutesToMostDestinations(lp));
// V6 does not provide sufficient coverage but v4 does
publicIpV4Routes().forEach(s -> lp.addRoute(new RouteInfo(new IpPrefix(s))));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
// V4 still does
lp.removeRoute(new RouteInfo(new IpPrefix("192.169.0.0/16")));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
// V4 does not any more
lp.removeRoute(new RouteInfo(new IpPrefix("64.0.0.0/2")));
assertFalse(Vpn.providesRoutesToMostDestinations(lp));
// V4 does not, but V6 has sufficient coverage again
lp.addRoute(new RouteInfo(new IpPrefix("::/1")));
assertTrue(Vpn.providesRoutesToMostDestinations(lp));
}
}