Add convenience methods to IpPrefix and LinkAddress

Also moving relevant test files into tests/net as part of runtest
framworks-net.

Also removes testHashCode in LinkAddress() because this test relies on
the assumption that hashCode() is stable across releases or jdk
versions, which is absolutely not true.

This creates maintenance work for little benefit since hashCode is
already tested as part of the equality test.

For instance this test is now broken because hashing for InetAddress
changed.

Bug: 62988545
Bug: 62918393
Test: runtest frameworks-net, added coverage in tests
Change-Id: I695bc3f0e801bf13bc4fc0706565758f12b775b4
This commit is contained in:
Hugo Benichi 2017-08-08 13:06:04 +09:00
commit 8253be9fb7

View file

@ -76,7 +76,7 @@ public class LinkAddress implements Parcelable {
* RFC 6724 section 3.2.
* @hide
*/
static int scopeForUnicastAddress(InetAddress addr) {
private static int scopeForUnicastAddress(InetAddress addr) {
if (addr.isAnyLocalAddress()) {
return RT_SCOPE_HOST;
}
@ -101,13 +101,29 @@ public class LinkAddress implements Parcelable {
* Per RFC 4193 section 8, fc00::/7 identifies these addresses.
*/
private boolean isIPv6ULA() {
if (address instanceof Inet6Address) {
if (isIPv6()) {
byte[] bytes = address.getAddress();
return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
}
return false;
}
/**
* @return true if the address is IPv6.
* @hide
*/
public boolean isIPv6() {
return address instanceof Inet6Address;
}
/**
* @return true if the address is IPv4 or is a mapped IPv4 address.
* @hide
*/
public boolean isIPv4() {
return address instanceof Inet4Address;
}
/**
* Utility function for the constructors.
*/
@ -115,7 +131,7 @@ public class LinkAddress implements Parcelable {
if (address == null ||
address.isMulticastAddress() ||
prefixLength < 0 ||
((address instanceof Inet4Address) && prefixLength > 32) ||
(address instanceof Inet4Address && prefixLength > 32) ||
(prefixLength > 128)) {
throw new IllegalArgumentException("Bad LinkAddress params " + address +
"/" + prefixLength);
@ -184,6 +200,7 @@ public class LinkAddress implements Parcelable {
*/
public LinkAddress(String address, int flags, int scope) {
// This may throw an IllegalArgumentException; catching it is the caller's responsibility.
// TODO: consider rejecting mapped IPv4 addresses such as "::ffff:192.0.2.5/24".
Pair<InetAddress, Integer> ipAndMask = NetworkUtils.parseIpAndMask(address);
init(ipAndMask.first, ipAndMask.second, flags, scope);
}