Merge "Add varargs methods to build DhcpServingParams"

This commit is contained in:
Remi NGUYEN VAN
2018-08-28 08:08:07 +00:00
committed by Gerrit Code Review
2 changed files with 52 additions and 3 deletions

View File

@@ -28,6 +28,8 @@ import android.net.IpPrefix;
import android.net.LinkAddress;
import android.net.NetworkUtils;
import com.google.android.collect.Sets;
import java.net.Inet4Address;
import java.util.Collections;
import java.util.HashSet;
@@ -154,6 +156,25 @@ public class DhcpServingParams {
return this;
}
/**
* Set the default routers to be advertised to DHCP clients.
*
* <p>Each router must be inside the served prefix. This may be an empty list of routers,
* but it must always be set explicitly before building the {@link DhcpServingParams}.
*/
public Builder setDefaultRouters(@NonNull Inet4Address... defaultRouters) {
return setDefaultRouters(Sets.newArraySet(defaultRouters));
}
/**
* Convenience method to build the parameters with no default router.
*
* <p>Equivalent to calling {@link #setDefaultRouters(Inet4Address...)} with no address.
*/
public Builder withNoDefaultRouter() {
return setDefaultRouters();
}
/**
* Set the DNS servers to be advertised to DHCP clients.
*
@@ -165,6 +186,25 @@ public class DhcpServingParams {
return this;
}
/**
* Set the DNS servers to be advertised to DHCP clients.
*
* <p>This may be an empty list of servers, but it must always be set explicitly before
* building the {@link DhcpServingParams}.
*/
public Builder setDnsServers(@NonNull Inet4Address... dnsServers) {
return setDnsServers(Sets.newArraySet(dnsServers));
}
/**
* Convenience method to build the parameters with no DNS server.
*
* <p>Equivalent to calling {@link #setDnsServers(Inet4Address...)} with no address.
*/
public Builder withNoDnsServer() {
return setDnsServers();
}
/**
* Set excluded addresses that the DHCP server is not allowed to assign to clients.
*
@@ -176,6 +216,16 @@ public class DhcpServingParams {
return this;
}
/**
* Set excluded addresses that the DHCP server is not allowed to assign to clients.
*
* <p>This parameter is optional. DNS servers and default routers are always excluded
* and do not need to be set here.
*/
public Builder setExcludedAddrs(@NonNull Inet4Address... excludedAddrs) {
return setExcludedAddrs(Sets.newArraySet(excludedAddrs));
}
/**
* Set the lease time for leases assigned by the DHCP server.
*

View File

@@ -37,7 +37,6 @@ import org.junit.runner.RunWith;
import java.net.Inet4Address;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
@@ -150,14 +149,14 @@ public class DhcpServingParamsTest {
@Test(expected = InvalidParameterException.class)
public void testBuild_PrefixTooSmall() throws InvalidParameterException {
mBuilder.setDefaultRouters(Collections.singleton(parseAddr("192.168.0.254")))
mBuilder.setDefaultRouters(parseAddr("192.168.0.254"))
.setServerAddr(new LinkAddress(TEST_SERVER_ADDR, 31))
.build();
}
@Test(expected = InvalidParameterException.class)
public void testBuild_RouterNotInPrefix() throws InvalidParameterException {
mBuilder.setDefaultRouters(Collections.singleton(parseAddr("192.168.254.254"))).build();
mBuilder.setDefaultRouters(parseAddr("192.168.254.254")).build();
}
private static <T> void assertContains(@NonNull Set<T> set, @NonNull Set<T> subset) {