Search in sources :

Example 41 with LinkAddress

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

the class LinkAddressTest method testHashCode.

public void testHashCode() {
    LinkAddress l;
    l = new LinkAddress(V4_ADDRESS, 23);
    assertEquals(-982787, l.hashCode());
    l = new LinkAddress(V4_ADDRESS, 23, 0, RT_SCOPE_HOST);
    assertEquals(-971865, l.hashCode());
    l = new LinkAddress(V4_ADDRESS, 27);
    assertEquals(-982743, l.hashCode());
    l = new LinkAddress(V6_ADDRESS, 64);
    assertEquals(1076522926, l.hashCode());
    l = new LinkAddress(V6_ADDRESS, 128);
    assertEquals(1076523630, l.hashCode());
    l = new LinkAddress(V6_ADDRESS, 128, IFA_F_TENTATIVE, RT_SCOPE_UNIVERSE);
    assertEquals(1076524846, l.hashCode());
}
Also used : LinkAddress(android.net.LinkAddress)

Example 42 with LinkAddress

use of android.net.LinkAddress 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 43 with LinkAddress

use of android.net.LinkAddress 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 44 with LinkAddress

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

the class LinkAddressTest method testEqualsAndSameAddressAs.

public void testEqualsAndSameAddressAs() {
    LinkAddress l1, l2, l3;
    l1 = new LinkAddress("2001:db8::1/64");
    l2 = new LinkAddress("2001:db8::1/64");
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress("2001:db8::1/65");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l2 = new LinkAddress("2001:db8::2/64");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l1 = new LinkAddress("192.0.2.1/24");
    l2 = new LinkAddress("192.0.2.1/24");
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress("192.0.2.1/23");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l2 = new LinkAddress("192.0.2.2/24");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    // Check equals() and isSameAddressAs() on identical addresses with different flags.
    l1 = new LinkAddress(V6_ADDRESS, 64);
    l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
    assertLinkAddressesNotEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    // Check equals() and isSameAddressAs() on identical addresses with different scope.
    l1 = new LinkAddress(V4_ADDRESS, 24);
    l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
    assertLinkAddressesNotEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    // Addresses with the same start or end bytes aren't equal between families.
    l1 = new LinkAddress("32.1.13.184/24");
    l2 = new LinkAddress("2001:db8::1/24");
    l3 = new LinkAddress("::2001:db8/24");
    byte[] ipv4Bytes = l1.getAddress().getAddress();
    byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4);
    byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16);
    assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
    assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    assertLinkAddressesNotEqual(l1, l3);
    assertIsNotSameAddressAs(l1, l3);
    // Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
    // TODO: Investigate fixing this.
    String addressString = V4 + "/24";
    l1 = new LinkAddress(addressString);
    l2 = new LinkAddress("::ffff:" + addressString);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
}
Also used : LinkAddress(android.net.LinkAddress)

Example 45 with LinkAddress

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

the class StaticIpConfigurationTest method testHashCodeAndEquals.

@SmallTest
public void testHashCodeAndEquals() {
    HashSet<Integer> hashCodes = new HashSet();
    hashCodes.add(0);
    StaticIpConfiguration s = new StaticIpConfiguration();
    // Check that this hash code is nonzero and different from all the ones seen so far.
    assertTrue(hashCodes.add(s.hashCode()));
    s.ipAddress = ADDR;
    assertTrue(hashCodes.add(s.hashCode()));
    s.gateway = GATEWAY;
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS1);
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS2);
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS3);
    assertTrue(hashCodes.add(s.hashCode()));
    s.domains = "example.com";
    assertTrue(hashCodes.add(s.hashCode()));
    assertFalse(s.equals(null));
    assertEquals(s, s);
    StaticIpConfiguration s2 = new StaticIpConfiguration(s);
    assertEquals(s, s2);
    s.ipAddress = new LinkAddress(DNS1, 32);
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.domains = "foo";
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.gateway = DNS2;
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.dnsServers.add(DNS3);
    assertNotEquals(s, s2);
}
Also used : LinkAddress(android.net.LinkAddress) StaticIpConfiguration(android.net.StaticIpConfiguration) HashSet(java.util.HashSet) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

LinkAddress (android.net.LinkAddress)210 LinkProperties (android.net.LinkProperties)70 InetAddress (java.net.InetAddress)70 RouteInfo (android.net.RouteInfo)52 SmallTest (android.test.suitebuilder.annotation.SmallTest)29 RemoteException (android.os.RemoteException)27 Inet4Address (java.net.Inet4Address)24 InterfaceConfiguration (android.net.InterfaceConfiguration)22 Inet6Address (java.net.Inet6Address)21 IpPrefix (android.net.IpPrefix)20 StaticIpConfiguration (android.net.StaticIpConfiguration)20 IOException (java.io.IOException)15 ApfFilter (android.net.apf.ApfFilter)12 ByteBuffer (java.nio.ByteBuffer)12 ProxyInfo (android.net.ProxyInfo)10 VpnConfig (com.android.internal.net.VpnConfig)10 DhcpResults (android.net.DhcpResults)9 LargeTest (android.test.suitebuilder.annotation.LargeTest)9 Command (com.android.server.NativeDaemonConnector.Command)9 EOFException (java.io.EOFException)6