Search in sources :

Example 96 with LinkProperties

use of android.net.LinkProperties in project android_frameworks_base by DirtyUnicorns.

the class LinkPropertiesTest method testIsReachable.

@SmallTest
// Failing.
@Suppress
public void testIsReachable() {
    final LinkProperties v4lp = new LinkProperties();
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Add an on-link route, making the on-link DNS server reachable,
    // but there is still no IPv4 address.
    assertTrue(v4lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("75.208.0.0"), 16))));
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding an IPv4 address (right now, any IPv4 address) means we use
    // the routes to compute likely reachability.
    assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16)));
    assertTrue(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1)));
    assertTrue(v4lp.isReachable(DNS1));
    assertTrue(v4lp.isReachable(DNS2));
    final LinkProperties v6lp = new LinkProperties();
    final InetAddress kLinkLocalDns = NetworkUtils.numericToInetAddress("fe80::6:1");
    final InetAddress kLinkLocalDnsWithScope = NetworkUtils.numericToInetAddress("fe80::6:2%43");
    final InetAddress kOnLinkDns = NetworkUtils.numericToInetAddress("2001:db8:85a3::53");
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local route, making the link-local DNS servers reachable. Because
    // we assume the presence of an IPv6 link-local address, link-local DNS servers
    // are considered reachable, but only those with a non-zero scope identifier.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("fe80::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local address--nothing changes.
    assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global route on link, but no global address yet. DNS servers reachable
    // via a route that doesn't require a gateway: give them the benefit of the
    // doubt and hope the link-local source address suffices for communication.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("2001:db8:85a3::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global address; the on-link global address DNS server is (still)
    // presumed reachable.
    assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertTrue(v6lp.isReachable(DNS6));
    // Check isReachable on stacked links. This requires that the source IP address be assigned
    // on the interface returned by the route lookup.
    LinkProperties stacked = new LinkProperties();
    // Can't add a stacked link without an interface name.
    stacked.setInterfaceName("v4-test0");
    v6lp.addStackedLink(stacked);
    InetAddress stackedAddress = Address("192.0.0.4");
    LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addLinkAddress(stackedLinkAddress);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addRoute(new RouteInfo(stackedLinkAddress));
    assertTrue(stacked.isReachable(stackedAddress));
    assertTrue(v6lp.isReachable(stackedAddress));
    assertFalse(v6lp.isReachable(DNS1));
    stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress));
    assertTrue(v6lp.isReachable(DNS1));
}
Also used : IpPrefix(android.net.IpPrefix) LinkAddress(android.net.LinkAddress) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress) SmallTest(android.test.suitebuilder.annotation.SmallTest) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 97 with LinkProperties

use of android.net.LinkProperties in project android_frameworks_base by DirtyUnicorns.

the class LinkPropertiesTest method testEqualsDifferentOrder.

@SmallTest
public void testEqualsDifferentOrder() {
    try {
        LinkProperties source = new LinkProperties();
        source.setInterfaceName(NAME);
        // set 2 link addresses
        source.addLinkAddress(LINKADDRV4);
        source.addLinkAddress(LINKADDRV6);
        // set 2 dnses
        source.addDnsServer(DNS1);
        source.addDnsServer(DNS2);
        // set 2 gateways
        source.addRoute(new RouteInfo(GATEWAY1));
        source.addRoute(new RouteInfo(GATEWAY2));
        source.setMtu(MTU);
        LinkProperties target = new LinkProperties();
        // Exchange order
        target.setInterfaceName(NAME);
        target.addLinkAddress(LINKADDRV6);
        target.addLinkAddress(LINKADDRV4);
        target.addDnsServer(DNS2);
        target.addDnsServer(DNS1);
        target.addRoute(new RouteInfo(GATEWAY2));
        target.addRoute(new RouteInfo(GATEWAY1));
        target.setMtu(MTU);
        assertLinkPropertiesEqual(source, target);
    } catch (Exception e) {
        fail();
    }
}
Also used : RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 98 with LinkProperties

use of android.net.LinkProperties in project android_frameworks_base by DirtyUnicorns.

the class LinkPropertiesTest method testAddressMethods.

@SmallTest
public void testAddressMethods() {
    LinkProperties lp = new LinkProperties();
    // No addresses.
    assertFalse(lp.hasIPv4Address());
    assertFalse(lp.hasGlobalIPv6Address());
    // Addresses on stacked links don't count.
    LinkProperties stacked = new LinkProperties();
    stacked.setInterfaceName("stacked");
    lp.addStackedLink(stacked);
    stacked.addLinkAddress(LINKADDRV4);
    stacked.addLinkAddress(LINKADDRV6);
    assertTrue(stacked.hasIPv4Address());
    assertTrue(stacked.hasGlobalIPv6Address());
    assertFalse(lp.hasIPv4Address());
    assertFalse(lp.hasGlobalIPv6Address());
    lp.removeStackedLink("stacked");
    assertFalse(lp.hasIPv4Address());
    assertFalse(lp.hasGlobalIPv6Address());
    // Addresses on the base link.
    // Check the return values of hasIPvXAddress and ensure the add/remove methods return true
    // iff something changes.
    assertEquals(0, lp.getLinkAddresses().size());
    assertTrue(lp.addLinkAddress(LINKADDRV6));
    assertEquals(1, lp.getLinkAddresses().size());
    assertFalse(lp.hasIPv4Address());
    assertTrue(lp.hasGlobalIPv6Address());
    assertTrue(lp.removeLinkAddress(LINKADDRV6));
    assertEquals(0, lp.getLinkAddresses().size());
    assertTrue(lp.addLinkAddress(LINKADDRV6LINKLOCAL));
    assertEquals(1, lp.getLinkAddresses().size());
    assertFalse(lp.hasGlobalIPv6Address());
    assertTrue(lp.addLinkAddress(LINKADDRV4));
    assertEquals(2, lp.getLinkAddresses().size());
    assertTrue(lp.hasIPv4Address());
    assertFalse(lp.hasGlobalIPv6Address());
    assertTrue(lp.addLinkAddress(LINKADDRV6));
    assertEquals(3, lp.getLinkAddresses().size());
    assertTrue(lp.hasIPv4Address());
    assertTrue(lp.hasGlobalIPv6Address());
    assertTrue(lp.removeLinkAddress(LINKADDRV6LINKLOCAL));
    assertEquals(2, lp.getLinkAddresses().size());
    assertTrue(lp.hasIPv4Address());
    assertTrue(lp.hasGlobalIPv6Address());
    // Adding an address twice has no effect.
    // Removing an address that's not present has no effect.
    assertFalse(lp.addLinkAddress(LINKADDRV4));
    assertEquals(2, lp.getLinkAddresses().size());
    assertTrue(lp.hasIPv4Address());
    assertTrue(lp.removeLinkAddress(LINKADDRV4));
    assertEquals(1, lp.getLinkAddresses().size());
    assertFalse(lp.hasIPv4Address());
    assertFalse(lp.removeLinkAddress(LINKADDRV4));
    assertEquals(1, lp.getLinkAddresses().size());
    // Adding an address that's already present but with different properties causes the
    // existing address to be updated and returns true.
    // Start with only LINKADDRV6.
    assertEquals(1, lp.getLinkAddresses().size());
    assertEquals(LINKADDRV6, getFirstLinkAddress(lp));
    // Create a LinkAddress object for the same address, but with different flags.
    LinkAddress deprecated = new LinkAddress(ADDRV6, 128, OsConstants.IFA_F_DEPRECATED, OsConstants.RT_SCOPE_UNIVERSE);
    assertTrue(deprecated.isSameAddressAs(LINKADDRV6));
    assertFalse(deprecated.equals(LINKADDRV6));
    // Check that adding it updates the existing address instead of adding a new one.
    assertTrue(lp.addLinkAddress(deprecated));
    assertEquals(1, lp.getLinkAddresses().size());
    assertEquals(deprecated, getFirstLinkAddress(lp));
    assertFalse(LINKADDRV6.equals(getFirstLinkAddress(lp)));
    // Removing LINKADDRV6 removes deprecated, because removing addresses ignores properties.
    assertTrue(lp.removeLinkAddress(LINKADDRV6));
    assertEquals(0, lp.getLinkAddresses().size());
}
Also used : LinkAddress(android.net.LinkAddress) LinkProperties(android.net.LinkProperties) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 99 with LinkProperties

use of android.net.LinkProperties in project android_frameworks_base by DirtyUnicorns.

the class LinkPropertiesTest method testStackedInterfaces.

@SmallTest
public void testStackedInterfaces() {
    LinkProperties rmnet0 = new LinkProperties();
    rmnet0.setInterfaceName("rmnet0");
    rmnet0.addLinkAddress(LINKADDRV6);
    LinkProperties clat4 = new LinkProperties();
    clat4.setInterfaceName("clat4");
    clat4.addLinkAddress(LINKADDRV4);
    assertEquals(0, rmnet0.getStackedLinks().size());
    assertEquals(1, rmnet0.getAddresses().size());
    assertEquals(1, rmnet0.getLinkAddresses().size());
    assertEquals(1, rmnet0.getAllAddresses().size());
    assertEquals(1, rmnet0.getAllLinkAddresses().size());
    rmnet0.addStackedLink(clat4);
    assertEquals(1, rmnet0.getStackedLinks().size());
    assertEquals(1, rmnet0.getAddresses().size());
    assertEquals(1, rmnet0.getLinkAddresses().size());
    assertEquals(2, rmnet0.getAllAddresses().size());
    assertEquals(2, rmnet0.getAllLinkAddresses().size());
    rmnet0.addStackedLink(clat4);
    assertEquals(1, rmnet0.getStackedLinks().size());
    assertEquals(1, rmnet0.getAddresses().size());
    assertEquals(1, rmnet0.getLinkAddresses().size());
    assertEquals(2, rmnet0.getAllAddresses().size());
    assertEquals(2, rmnet0.getAllLinkAddresses().size());
    assertEquals(0, clat4.getStackedLinks().size());
    // Modify an item in the returned collection to see what happens.
    for (LinkProperties link : rmnet0.getStackedLinks()) {
        if (link.getInterfaceName().equals("clat4")) {
            link.setInterfaceName("newname");
        }
    }
    for (LinkProperties link : rmnet0.getStackedLinks()) {
        assertFalse("newname".equals(link.getInterfaceName()));
    }
    assertTrue(rmnet0.removeStackedLink("clat4"));
    assertEquals(0, rmnet0.getStackedLinks().size());
    assertEquals(1, rmnet0.getAddresses().size());
    assertEquals(1, rmnet0.getLinkAddresses().size());
    assertEquals(1, rmnet0.getAllAddresses().size());
    assertEquals(1, rmnet0.getAllLinkAddresses().size());
    assertFalse(rmnet0.removeStackedLink("clat4"));
}
Also used : LinkProperties(android.net.LinkProperties) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 100 with LinkProperties

use of android.net.LinkProperties in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testRequestCallbackUpdates.

@LargeTest
public void testRequestCallbackUpdates() throws Exception {
    // File a network request for mobile.
    final TestNetworkCallback cellNetworkCallback = new TestRequestUpdateCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.requestNetwork(cellRequest, cellNetworkCallback);
    // Bring up the mobile network.
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    mCellNetworkAgent.connect(true);
    // We should get onAvailable().
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    // We should get onCapabilitiesChanged(), when the mobile network successfully validates.
    cellNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Update LinkProperties.
    final LinkProperties lp = new LinkProperties();
    lp.setInterfaceName("foonet_data0");
    mCellNetworkAgent.sendLinkProperties(lp);
    // We should get onLinkPropertiesChanged().
    cellNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    // Register a garden variety default network request.
    final TestNetworkCallback dfltNetworkCallback = new TestRequestUpdateCallback();
    mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
    // Only onAvailable() is called; no other information is delivered.
    dfltNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    dfltNetworkCallback.assertNoCallback();
    // Request a NetworkCapabilities update; only the requesting callback is notified.
    mCm.requestNetworkCapabilities(dfltNetworkCallback);
    dfltNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    dfltNetworkCallback.assertNoCallback();
    // Request a LinkProperties update; only the requesting callback is notified.
    mCm.requestLinkProperties(dfltNetworkCallback);
    dfltNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
    cellNetworkCallback.assertNoCallback();
    dfltNetworkCallback.assertNoCallback();
    mCm.unregisterNetworkCallback(dfltNetworkCallback);
    mCm.unregisterNetworkCallback(cellNetworkCallback);
}
Also used : NetworkRequest(android.net.NetworkRequest) LinkProperties(android.net.LinkProperties) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Aggregations

LinkProperties (android.net.LinkProperties)283 RouteInfo (android.net.RouteInfo)72 LinkAddress (android.net.LinkAddress)70 SmallTest (android.test.suitebuilder.annotation.SmallTest)66 InetAddress (java.net.InetAddress)52 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)40 NetworkInfo (android.net.NetworkInfo)38 NetworkState (android.net.NetworkState)36 RemoteException (android.os.RemoteException)36 NetworkCapabilities (android.net.NetworkCapabilities)23 Network (android.net.Network)21 ProvisioningChange (android.net.LinkProperties.ProvisioningChange)20 ArrayList (java.util.ArrayList)17 IpPrefix (android.net.IpPrefix)15 Inet6Address (java.net.Inet6Address)15 NetworkRequest (android.net.NetworkRequest)14 ApfFilter (android.net.apf.ApfFilter)12 LargeTest (android.test.suitebuilder.annotation.LargeTest)12 UnknownHostException (java.net.UnknownHostException)12 NetworkMisc (android.net.NetworkMisc)10