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));
}
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());
}
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);
}
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);
}
use of android.net.LinkAddress in project android_frameworks_base by DirtyUnicorns.
the class NetworkManagementServiceTest method testNetworkObservers.
/**
* Tests that network observers work properly.
*/
public void testNetworkObservers() throws Exception {
BaseNetworkObserver observer = mock(BaseNetworkObserver.class);
// Used by registerObserver.
doReturn(new Binder()).when(observer).asBinder();
mNMService.registerObserver(observer);
// Forget everything that happened to the mock so far, so we can explicitly verify
// everything that happens and does not happen to it from now on.
reset(observer);
// Now send NetworkManagementService messages and ensure that the observer methods are
// called. After every valid message we expect a callback soon after; to ensure that
// invalid messages don't cause any callbacks, we call verifyNoMoreInteractions at the end.
/**
* Interface changes.
*/
sendMessage("600 Iface added rmnet12");
expectSoon(observer).interfaceAdded("rmnet12");
sendMessage("600 Iface removed eth1");
expectSoon(observer).interfaceRemoved("eth1");
sendMessage("607 Iface removed eth1");
// Invalid code.
sendMessage("600 Iface borked lo down");
// Invalid event.
sendMessage("600 Iface changed clat4 up again");
// Extra tokens.
sendMessage("600 Iface changed clat4 up");
expectSoon(observer).interfaceStatusChanged("clat4", true);
sendMessage("600 Iface linkstate rmnet0 down");
expectSoon(observer).interfaceLinkStateChanged("rmnet0", false);
sendMessage("600 IFACE linkstate clat4 up");
// Invalid group.
/**
* Bandwidth control events.
*/
sendMessage("601 limit alert data rmnet_usb0");
expectSoon(observer).limitReached("data", "rmnet_usb0");
sendMessage("601 invalid alert data rmnet0");
// Invalid group.
sendMessage("601 limit increased data rmnet0");
// Invalid event.
/**
* Interface class activity.
*/
sendMessage("613 IfaceClass active rmnet0");
expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 0);
sendMessage("613 IfaceClass active rmnet0 1234");
expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", true, 1234);
sendMessage("613 IfaceClass idle eth0");
expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 0);
sendMessage("613 IfaceClass idle eth0 1234");
expectSoon(observer).interfaceClassDataActivityChanged("eth0", false, 1234);
sendMessage("613 IfaceClass reallyactive rmnet0 1234");
expectSoon(observer).interfaceClassDataActivityChanged("rmnet0", false, 1234);
sendMessage("613 InterfaceClass reallyactive rmnet0");
// Invalid group.
/**
* IP address changes.
*/
sendMessage("614 Address updated fe80::1/64 wlan0 128 253");
expectSoon(observer).addressUpdated("wlan0", new LinkAddress("fe80::1/64", 128, 253));
// There is no "added", so we take this as "removed".
sendMessage("614 Address added fe80::1/64 wlan0 128 253");
expectSoon(observer).addressRemoved("wlan0", new LinkAddress("fe80::1/64", 128, 253));
sendMessage("614 Address removed 2001:db8::1/64 wlan0 1 0");
expectSoon(observer).addressRemoved("wlan0", new LinkAddress("2001:db8::1/64", 1, 0));
sendMessage("614 Address removed 2001:db8::1/64 wlan0 1");
// Not enough arguments.
sendMessage("666 Address removed 2001:db8::1/64 wlan0 1 0");
// Invalid code.
/**
* DNS information broadcasts.
*/
sendMessage("615 DnsInfo servers rmnet_usb0 3600 2001:db8::1");
expectSoon(observer).interfaceDnsServerInfo("rmnet_usb0", 3600, new String[] { "2001:db8::1" });
sendMessage("615 DnsInfo servers wlan0 14400 2001:db8::1,2001:db8::2");
expectSoon(observer).interfaceDnsServerInfo("wlan0", 14400, new String[] { "2001:db8::1", "2001:db8::2" });
// We don't check for negative lifetimes, only for parse errors.
sendMessage("615 DnsInfo servers wlan0 -3600 ::1");
expectSoon(observer).interfaceDnsServerInfo("wlan0", -3600, new String[] { "::1" });
sendMessage("615 DnsInfo servers wlan0 SIXHUNDRED ::1");
// Non-numeric lifetime.
sendMessage("615 DnsInfo servers wlan0 2001:db8::1");
// Missing lifetime.
sendMessage("615 DnsInfo servers wlan0 3600");
// No servers.
sendMessage("615 DnsInfo servers 3600 wlan0 2001:db8::1,2001:db8::2");
// Non-numeric lifetime.
sendMessage("615 DnsInfo wlan0 7200 2001:db8::1,2001:db8::2");
// Invalid tokens.
sendMessage("666 DnsInfo servers wlan0 5400 2001:db8::1");
// Invalid code.
// No syntax checking on the addresses.
sendMessage("615 DnsInfo servers wlan0 600 ,::,,foo,::1,");
expectSoon(observer).interfaceDnsServerInfo("wlan0", 600, new String[] { "", "::", "", "foo", "::1" });
// Make sure nothing else was called.
verifyNoMoreInteractions(observer);
}
Aggregations