Minor changes to RouteInfo.
1. Realize that mDestination can never be null and update the code accordingly. 2. Simplify isDefaultRoute. 3. Provide two new hidden utility methods, isIPv4Default() and isIPv6Default(), that can be used by LinkProperties to to determine if the system has connectivity. 4. Update tests. Bug: 9180552 Change-Id: I85028d50556c888261d250925962bdedfe08e0c6
This commit is contained in:
@@ -63,7 +63,6 @@ public final class RouteInfo implements Parcelable {
|
|||||||
*/
|
*/
|
||||||
private final String mInterface;
|
private final String mInterface;
|
||||||
|
|
||||||
private final boolean mIsDefault;
|
|
||||||
private final boolean mIsHost;
|
private final boolean mIsHost;
|
||||||
private final boolean mHasGateway;
|
private final boolean mHasGateway;
|
||||||
|
|
||||||
@@ -128,7 +127,6 @@ public final class RouteInfo implements Parcelable {
|
|||||||
}
|
}
|
||||||
mGateway = gateway;
|
mGateway = gateway;
|
||||||
mInterface = iface;
|
mInterface = iface;
|
||||||
mIsDefault = isDefault();
|
|
||||||
mIsHost = isHost();
|
mIsHost = isHost();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,18 +213,6 @@ public final class RouteInfo implements Parcelable {
|
|||||||
mDestination.getPrefixLength() == 128);
|
mDestination.getPrefixLength() == 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isDefault() {
|
|
||||||
boolean val = false;
|
|
||||||
if (mGateway != null) {
|
|
||||||
if (mGateway instanceof Inet4Address) {
|
|
||||||
val = (mDestination == null || mDestination.getPrefixLength() == 0);
|
|
||||||
} else {
|
|
||||||
val = (mDestination == null || mDestination.getPrefixLength() == 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the destination address and prefix length in the form of an {@link IpPrefix}.
|
* Retrieves the destination address and prefix length in the form of an {@link IpPrefix}.
|
||||||
*
|
*
|
||||||
@@ -269,7 +255,23 @@ public final class RouteInfo implements Parcelable {
|
|||||||
* @return {@code true} if the destination has a prefix length of 0.
|
* @return {@code true} if the destination has a prefix length of 0.
|
||||||
*/
|
*/
|
||||||
public boolean isDefaultRoute() {
|
public boolean isDefaultRoute() {
|
||||||
return mIsDefault;
|
return mDestination.getPrefixLength() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if this route is an IPv4 default route.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public boolean isIPv4Default() {
|
||||||
|
return isDefaultRoute() && mDestination.getAddress() instanceof Inet4Address;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates if this route is an IPv6 default route.
|
||||||
|
* @hide
|
||||||
|
*/
|
||||||
|
public boolean isIPv6Default() {
|
||||||
|
return isDefaultRoute() && mDestination.getAddress() instanceof Inet6Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -370,10 +372,9 @@ public final class RouteInfo implements Parcelable {
|
|||||||
* Returns a hashcode for this <code>RouteInfo</code> object.
|
* Returns a hashcode for this <code>RouteInfo</code> object.
|
||||||
*/
|
*/
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return (mDestination == null ? 0 : mDestination.hashCode() * 41)
|
return (mDestination.hashCode() * 41)
|
||||||
+ (mGateway == null ? 0 :mGateway.hashCode() * 47)
|
+ (mGateway == null ? 0 :mGateway.hashCode() * 47)
|
||||||
+ (mInterface == null ? 0 :mInterface.hashCode() * 67)
|
+ (mInterface == null ? 0 :mInterface.hashCode() * 67);
|
||||||
+ (mIsDefault ? 3 : 7);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -387,13 +388,8 @@ public final class RouteInfo implements Parcelable {
|
|||||||
* Implement the Parcelable interface
|
* Implement the Parcelable interface
|
||||||
*/
|
*/
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
if (mDestination == null) {
|
dest.writeByteArray(mDestination.getAddress().getAddress());
|
||||||
dest.writeByte((byte) 0);
|
dest.writeInt(mDestination.getPrefixLength());
|
||||||
} else {
|
|
||||||
dest.writeByte((byte) 1);
|
|
||||||
dest.writeByteArray(mDestination.getAddress().getAddress());
|
|
||||||
dest.writeInt(mDestination.getPrefixLength());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mGateway == null) {
|
if (mGateway == null) {
|
||||||
dest.writeByte((byte) 0);
|
dest.writeByte((byte) 0);
|
||||||
@@ -415,17 +411,15 @@ public final class RouteInfo implements Parcelable {
|
|||||||
int prefix = 0;
|
int prefix = 0;
|
||||||
InetAddress gateway = null;
|
InetAddress gateway = null;
|
||||||
|
|
||||||
if (in.readByte() == 1) {
|
byte[] addr = in.createByteArray();
|
||||||
byte[] addr = in.createByteArray();
|
prefix = in.readInt();
|
||||||
prefix = in.readInt();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
destAddr = InetAddress.getByAddress(addr);
|
destAddr = InetAddress.getByAddress(addr);
|
||||||
} catch (UnknownHostException e) {}
|
} catch (UnknownHostException e) {}
|
||||||
}
|
|
||||||
|
|
||||||
if (in.readByte() == 1) {
|
if (in.readByte() == 1) {
|
||||||
byte[] addr = in.createByteArray();
|
addr = in.createByteArray();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
gateway = InetAddress.getByAddress(addr);
|
gateway = InetAddress.getByAddress(addr);
|
||||||
|
|||||||
@@ -150,38 +150,68 @@ public class RouteInfoTest extends TestCase {
|
|||||||
assertAreNotEqual(r1, r3);
|
assertAreNotEqual(r1, r3);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testHostRoute() {
|
public void testHostAndDefaultRoutes() {
|
||||||
RouteInfo r;
|
RouteInfo r;
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0");
|
r = new RouteInfo(Prefix("0.0.0.0/0"), Address("0.0.0.0"), "wlan0");
|
||||||
assertFalse(r.isHostRoute());
|
assertFalse(r.isHostRoute());
|
||||||
|
assertTrue(r.isDefaultRoute());
|
||||||
|
assertTrue(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0");
|
r = new RouteInfo(Prefix("::/0"), Address("::"), "wlan0");
|
||||||
assertFalse(r.isHostRoute());
|
assertFalse(r.isHostRoute());
|
||||||
|
assertTrue(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertTrue(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
|
r = new RouteInfo(Prefix("192.0.2.0/24"), null, "wlan0");
|
||||||
assertFalse(r.isHostRoute());
|
assertFalse(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0");
|
r = new RouteInfo(Prefix("2001:db8::/48"), null, "wlan0");
|
||||||
assertFalse(r.isHostRoute());
|
assertFalse(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0");
|
r = new RouteInfo(Prefix("192.0.2.0/32"), Address("0.0.0.0"), "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0");
|
r = new RouteInfo(Prefix("2001:db8::/128"), Address("::"), "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0");
|
r = new RouteInfo(Prefix("192.0.2.0/32"), null, "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0");
|
r = new RouteInfo(Prefix("2001:db8::/128"), null, "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0");
|
r = new RouteInfo(Prefix("::/128"), Address("fe80::"), "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
|
|
||||||
r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0");
|
r = new RouteInfo(Prefix("0.0.0.0/32"), Address("192.0.2.1"), "wlan0");
|
||||||
assertTrue(r.isHostRoute());
|
assertTrue(r.isHostRoute());
|
||||||
|
assertFalse(r.isDefaultRoute());
|
||||||
|
assertFalse(r.isIPv4Default());
|
||||||
|
assertFalse(r.isIPv6Default());
|
||||||
}
|
}
|
||||||
|
|
||||||
public RouteInfo passThroughParcel(RouteInfo r) {
|
public RouteInfo passThroughParcel(RouteInfo r) {
|
||||||
|
|||||||
Reference in New Issue
Block a user