Make LinkPropertiesTest pass, and fix an NPE.

aosp/973686 added the DHCP server address to LinkProperties, but
it did not have any tests, and would throw NPE when parceling a
LinkProperties that did not have a DHCP server.

Add tests and fix the bug.

Bug: 134098566
Test: unit test now passes
Change-Id: If28f0be650963e8d9af7eb53f6940824582a2db3
This commit is contained in:
Lorenzo Colitti
2020-01-06 19:43:59 +09:00
parent ca75bc897d
commit 279a1d6620
2 changed files with 23 additions and 5 deletions

View File

@@ -1674,7 +1674,7 @@ public final class LinkProperties implements Parcelable {
dest.writeString(mPrivateDnsServerName);
writeAddresses(dest, mPcscfs);
dest.writeString(mDomains);
dest.writeByteArray(mDhcpServerAddress.getAddress());
writeAddress(dest, mDhcpServerAddress);
dest.writeInt(mMtu);
dest.writeString(mTcpBufferSizes);
dest.writeInt(mRoutes.size());
@@ -1703,8 +1703,9 @@ public final class LinkProperties implements Parcelable {
}
}
private static void writeAddress(@NonNull Parcel dest, @NonNull InetAddress addr) {
dest.writeByteArray(addr.getAddress());
private static void writeAddress(@NonNull Parcel dest, @Nullable InetAddress addr) {
byte[] addressBytes = (addr == null ? null : addr.getAddress());
dest.writeByteArray(addressBytes);
if (addr instanceof Inet6Address) {
final Inet6Address v6Addr = (Inet6Address) addr;
final boolean hasScopeId = v6Addr.getScopeId() != 0;
@@ -1713,9 +1714,11 @@ public final class LinkProperties implements Parcelable {
}
}
@NonNull
@Nullable
private static InetAddress readAddress(@NonNull Parcel p) throws UnknownHostException {
final byte[] addr = p.createByteArray();
if (addr == null) return null;
if (addr.length == INET6_ADDR_LENGTH) {
final boolean hasScopeId = p.readBoolean();
final int scopeId = hasScopeId ? p.readInt() : 0;

View File

@@ -38,6 +38,7 @@ import androidx.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
@@ -65,6 +66,7 @@ public class LinkPropertiesTest {
private static final InetAddress GATEWAY62 = address("fe80::6:22%lo");
private static final InetAddress TESTIPV4ADDR = address("192.168.47.42");
private static final InetAddress TESTIPV6ADDR = address("fe80::7:33%43");
private static final Inet4Address DHCPSERVER = (Inet4Address) address("192.0.2.1");
private static final String NAME = "qmi0";
private static final String DOMAINS = "google.com";
private static final String PRIV_DNS_SERVER_NAME = "private.dns.com";
@@ -93,6 +95,7 @@ public class LinkPropertiesTest {
assertNull(lp.getHttpProxy());
assertNull(lp.getTcpBufferSizes());
assertNull(lp.getNat64Prefix());
assertNull(lp.getDhcpServerAddress());
assertFalse(lp.isProvisioned());
assertFalse(lp.isIpv4Provisioned());
assertFalse(lp.isIpv6Provisioned());
@@ -119,6 +122,7 @@ public class LinkPropertiesTest {
lp.setMtu(MTU);
lp.setTcpBufferSizes(TCP_BUFFER_SIZES);
lp.setNat64Prefix(new IpPrefix("2001:db8:0:64::/96"));
lp.setDhcpServerAddress(DHCPSERVER);
lp.setWakeOnLanSupported(true);
return lp;
}
@@ -960,11 +964,13 @@ public class LinkPropertiesTest {
source.setWakeOnLanSupported(true);
source.setDhcpServerAddress((Inet4Address) GATEWAY1);
final LinkProperties stacked = new LinkProperties();
stacked.setInterfaceName("test-stacked");
source.addStackedLink(stacked);
assertParcelSane(source, 15 /* fieldCount */);
assertParcelSane(source, 16 /* fieldCount */);
}
@Test
@@ -1090,6 +1096,15 @@ public class LinkPropertiesTest {
assertFalse(lp.isPrivateDnsActive());
}
@Test
public void testDhcpServerAddress() {
final LinkProperties lp = makeTestObject();
assertEquals(DHCPSERVER, lp.getDhcpServerAddress());
lp.clear();
assertNull(lp.getDhcpServerAddress());
}
@Test
public void testWakeOnLanSupported() {
final LinkProperties lp = makeTestObject();