Expose public APIs for IP & static IP configuration
Create public API for IP and static IP configuration. Bug: 209840828 Test: atest android.net.cts.IpConfigurationTest Test: atest android.net.cts.StaticIpConfigurationTest Test: atest android.net.dhcp.DhcpResultsParcelableUtilTest Change-Id: I720f168d1023806970919ca5dd44239a276826b6
This commit is contained in:
@@ -205,6 +205,21 @@ package android.net {
|
|||||||
method @NonNull public static java.net.InetAddress parseNumericAddress(@NonNull String);
|
method @NonNull public static java.net.InetAddress parseNumericAddress(@NonNull String);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final class IpConfiguration implements android.os.Parcelable {
|
||||||
|
method public int describeContents();
|
||||||
|
method @Nullable public android.net.ProxyInfo getHttpProxy();
|
||||||
|
method @Nullable public android.net.StaticIpConfiguration getStaticIpConfiguration();
|
||||||
|
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||||
|
field @NonNull public static final android.os.Parcelable.Creator<android.net.IpConfiguration> CREATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class IpConfiguration.Builder {
|
||||||
|
ctor public IpConfiguration.Builder();
|
||||||
|
method @NonNull public android.net.IpConfiguration build();
|
||||||
|
method @NonNull public android.net.IpConfiguration.Builder setHttpProxy(@Nullable android.net.ProxyInfo);
|
||||||
|
method @NonNull public android.net.IpConfiguration.Builder setStaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
|
||||||
|
}
|
||||||
|
|
||||||
public final class IpPrefix implements android.os.Parcelable {
|
public final class IpPrefix implements android.os.Parcelable {
|
||||||
ctor public IpPrefix(@NonNull java.net.InetAddress, @IntRange(from=0, to=128) int);
|
ctor public IpPrefix(@NonNull java.net.InetAddress, @IntRange(from=0, to=128) int);
|
||||||
method public boolean contains(@NonNull java.net.InetAddress);
|
method public boolean contains(@NonNull java.net.InetAddress);
|
||||||
@@ -485,6 +500,25 @@ package android.net {
|
|||||||
method public void onStopped();
|
method public void onStopped();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final class StaticIpConfiguration implements android.os.Parcelable {
|
||||||
|
method public int describeContents();
|
||||||
|
method @NonNull public java.util.List<java.net.InetAddress> getDnsServers();
|
||||||
|
method @Nullable public String getDomains();
|
||||||
|
method @Nullable public java.net.InetAddress getGateway();
|
||||||
|
method @NonNull public android.net.LinkAddress getIpAddress();
|
||||||
|
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
||||||
|
field @NonNull public static final android.os.Parcelable.Creator<android.net.StaticIpConfiguration> CREATOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class StaticIpConfiguration.Builder {
|
||||||
|
ctor public StaticIpConfiguration.Builder();
|
||||||
|
method @NonNull public android.net.StaticIpConfiguration build();
|
||||||
|
method @NonNull public android.net.StaticIpConfiguration.Builder setDnsServers(@NonNull Iterable<java.net.InetAddress>);
|
||||||
|
method @NonNull public android.net.StaticIpConfiguration.Builder setDomains(@Nullable String);
|
||||||
|
method @NonNull public android.net.StaticIpConfiguration.Builder setGateway(@Nullable java.net.InetAddress);
|
||||||
|
method @NonNull public android.net.StaticIpConfiguration.Builder setIpAddress(@NonNull android.net.LinkAddress);
|
||||||
|
}
|
||||||
|
|
||||||
public interface TransportInfo {
|
public interface TransportInfo {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -133,17 +133,12 @@ package android.net {
|
|||||||
public final class IpConfiguration implements android.os.Parcelable {
|
public final class IpConfiguration implements android.os.Parcelable {
|
||||||
ctor public IpConfiguration();
|
ctor public IpConfiguration();
|
||||||
ctor public IpConfiguration(@NonNull android.net.IpConfiguration);
|
ctor public IpConfiguration(@NonNull android.net.IpConfiguration);
|
||||||
method public int describeContents();
|
|
||||||
method @Nullable public android.net.ProxyInfo getHttpProxy();
|
|
||||||
method @NonNull public android.net.IpConfiguration.IpAssignment getIpAssignment();
|
method @NonNull public android.net.IpConfiguration.IpAssignment getIpAssignment();
|
||||||
method @NonNull public android.net.IpConfiguration.ProxySettings getProxySettings();
|
method @NonNull public android.net.IpConfiguration.ProxySettings getProxySettings();
|
||||||
method @Nullable public android.net.StaticIpConfiguration getStaticIpConfiguration();
|
|
||||||
method public void setHttpProxy(@Nullable android.net.ProxyInfo);
|
method public void setHttpProxy(@Nullable android.net.ProxyInfo);
|
||||||
method public void setIpAssignment(@NonNull android.net.IpConfiguration.IpAssignment);
|
method public void setIpAssignment(@NonNull android.net.IpConfiguration.IpAssignment);
|
||||||
method public void setProxySettings(@NonNull android.net.IpConfiguration.ProxySettings);
|
method public void setProxySettings(@NonNull android.net.IpConfiguration.ProxySettings);
|
||||||
method public void setStaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
|
method public void setStaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
|
||||||
method public void writeToParcel(@NonNull android.os.Parcel, int);
|
|
||||||
field @NonNull public static final android.os.Parcelable.Creator<android.net.IpConfiguration> CREATOR;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum IpConfiguration.IpAssignment {
|
public enum IpConfiguration.IpAssignment {
|
||||||
@@ -484,23 +479,7 @@ package android.net {
|
|||||||
ctor public StaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
|
ctor public StaticIpConfiguration(@Nullable android.net.StaticIpConfiguration);
|
||||||
method public void addDnsServer(@NonNull java.net.InetAddress);
|
method public void addDnsServer(@NonNull java.net.InetAddress);
|
||||||
method public void clear();
|
method public void clear();
|
||||||
method public int describeContents();
|
|
||||||
method @NonNull public java.util.List<java.net.InetAddress> getDnsServers();
|
|
||||||
method @Nullable public String getDomains();
|
|
||||||
method @Nullable public java.net.InetAddress getGateway();
|
|
||||||
method @Nullable public android.net.LinkAddress getIpAddress();
|
|
||||||
method @NonNull public java.util.List<android.net.RouteInfo> getRoutes(@Nullable String);
|
method @NonNull public java.util.List<android.net.RouteInfo> getRoutes(@Nullable String);
|
||||||
method public void writeToParcel(android.os.Parcel, int);
|
|
||||||
field @NonNull public static final android.os.Parcelable.Creator<android.net.StaticIpConfiguration> CREATOR;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class StaticIpConfiguration.Builder {
|
|
||||||
ctor public StaticIpConfiguration.Builder();
|
|
||||||
method @NonNull public android.net.StaticIpConfiguration build();
|
|
||||||
method @NonNull public android.net.StaticIpConfiguration.Builder setDnsServers(@NonNull Iterable<java.net.InetAddress>);
|
|
||||||
method @NonNull public android.net.StaticIpConfiguration.Builder setDomains(@Nullable String);
|
|
||||||
method @NonNull public android.net.StaticIpConfiguration.Builder setGateway(@Nullable java.net.InetAddress);
|
|
||||||
method @NonNull public android.net.StaticIpConfiguration.Builder setIpAddress(@Nullable android.net.LinkAddress);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class TcpKeepalivePacketData extends android.net.KeepalivePacketData implements android.os.Parcelable {
|
public final class TcpKeepalivePacketData extends android.net.KeepalivePacketData implements android.os.Parcelable {
|
||||||
|
|||||||
@@ -28,16 +28,16 @@ import android.os.Parcelable;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class representing a configured network.
|
* A class representing the IP configuration of a network.
|
||||||
* @hide
|
|
||||||
*/
|
*/
|
||||||
@SystemApi
|
|
||||||
public final class IpConfiguration implements Parcelable {
|
public final class IpConfiguration implements Parcelable {
|
||||||
private static final String TAG = "IpConfiguration";
|
private static final String TAG = "IpConfiguration";
|
||||||
|
|
||||||
// This enum has been used by apps through reflection for many releases.
|
// This enum has been used by apps through reflection for many releases.
|
||||||
// Therefore they can't just be removed. Duplicating these constants to
|
// Therefore they can't just be removed. Duplicating these constants to
|
||||||
// give an alternate SystemApi is a worse option than exposing them.
|
// give an alternate SystemApi is a worse option than exposing them.
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
@SuppressLint("Enum")
|
@SuppressLint("Enum")
|
||||||
public enum IpAssignment {
|
public enum IpAssignment {
|
||||||
/* Use statically configured IP settings. Configuration can be accessed
|
/* Use statically configured IP settings. Configuration can be accessed
|
||||||
@@ -59,6 +59,8 @@ public final class IpConfiguration implements Parcelable {
|
|||||||
// This enum has been used by apps through reflection for many releases.
|
// This enum has been used by apps through reflection for many releases.
|
||||||
// Therefore they can't just be removed. Duplicating these constants to
|
// Therefore they can't just be removed. Duplicating these constants to
|
||||||
// give an alternate SystemApi is a worse option than exposing them.
|
// give an alternate SystemApi is a worse option than exposing them.
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
@SuppressLint("Enum")
|
@SuppressLint("Enum")
|
||||||
public enum ProxySettings {
|
public enum ProxySettings {
|
||||||
/* No proxy is to be used. Any existing proxy settings
|
/* No proxy is to be used. Any existing proxy settings
|
||||||
@@ -94,6 +96,8 @@ public final class IpConfiguration implements Parcelable {
|
|||||||
null : new ProxyInfo(httpProxy);
|
null : new ProxyInfo(httpProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public IpConfiguration() {
|
public IpConfiguration() {
|
||||||
init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
|
init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null);
|
||||||
}
|
}
|
||||||
@@ -107,6 +111,8 @@ public final class IpConfiguration implements Parcelable {
|
|||||||
init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
|
init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public IpConfiguration(@NonNull IpConfiguration source) {
|
public IpConfiguration(@NonNull IpConfiguration source) {
|
||||||
this();
|
this();
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
@@ -115,34 +121,58 @@ public final class IpConfiguration implements Parcelable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public @NonNull IpAssignment getIpAssignment() {
|
public @NonNull IpAssignment getIpAssignment() {
|
||||||
return ipAssignment;
|
return ipAssignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
|
public void setIpAssignment(@NonNull IpAssignment ipAssignment) {
|
||||||
this.ipAssignment = ipAssignment;
|
this.ipAssignment = ipAssignment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current static IP configuration (possibly null). Configured via
|
||||||
|
* {@link Builder#setStaticIpConfiguration(StaticIpConfiguration)}.
|
||||||
|
*
|
||||||
|
* @return Current static IP configuration.
|
||||||
|
*/
|
||||||
public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
|
public @Nullable StaticIpConfiguration getStaticIpConfiguration() {
|
||||||
return staticIpConfiguration;
|
return staticIpConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
|
public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) {
|
||||||
this.staticIpConfiguration = staticIpConfiguration;
|
this.staticIpConfiguration = staticIpConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public @NonNull ProxySettings getProxySettings() {
|
public @NonNull ProxySettings getProxySettings() {
|
||||||
return proxySettings;
|
return proxySettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public void setProxySettings(@NonNull ProxySettings proxySettings) {
|
public void setProxySettings(@NonNull ProxySettings proxySettings) {
|
||||||
this.proxySettings = proxySettings;
|
this.proxySettings = proxySettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The proxy configuration of this object.
|
||||||
|
*
|
||||||
|
* @return The proxy information of this object configured via
|
||||||
|
* {@link Builder#setHttpProxy(ProxyInfo)}.
|
||||||
|
*/
|
||||||
public @Nullable ProxyInfo getHttpProxy() {
|
public @Nullable ProxyInfo getHttpProxy() {
|
||||||
return httpProxy;
|
return httpProxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
|
public void setHttpProxy(@Nullable ProxyInfo httpProxy) {
|
||||||
this.httpProxy = httpProxy;
|
this.httpProxy = httpProxy;
|
||||||
}
|
}
|
||||||
@@ -220,4 +250,56 @@ public final class IpConfiguration implements Parcelable {
|
|||||||
return new IpConfiguration[size];
|
return new IpConfiguration[size];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builder used to construct {@link IpConfiguration} objects.
|
||||||
|
*/
|
||||||
|
public static final class Builder {
|
||||||
|
private StaticIpConfiguration mStaticIpConfiguration;
|
||||||
|
private ProxyInfo mProxyInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a static IP configuration.
|
||||||
|
*
|
||||||
|
* @param config Static IP configuration.
|
||||||
|
* @return A {@link Builder} object to allow chaining.
|
||||||
|
*/
|
||||||
|
public @NonNull Builder setStaticIpConfiguration(@Nullable StaticIpConfiguration config) {
|
||||||
|
mStaticIpConfiguration = config;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a proxy configuration.
|
||||||
|
*
|
||||||
|
* @param proxyInfo Proxy configuration.
|
||||||
|
* @return A {@link Builder} object to allow chaining.
|
||||||
|
*/
|
||||||
|
public @NonNull Builder setHttpProxy(@Nullable ProxyInfo proxyInfo) {
|
||||||
|
mProxyInfo = proxyInfo;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct an {@link IpConfiguration}.
|
||||||
|
*
|
||||||
|
* @return A new {@link IpConfiguration} object.
|
||||||
|
*/
|
||||||
|
public @NonNull IpConfiguration build() {
|
||||||
|
IpConfiguration config = new IpConfiguration();
|
||||||
|
config.setStaticIpConfiguration(mStaticIpConfiguration);
|
||||||
|
config.setIpAssignment(
|
||||||
|
mStaticIpConfiguration == null ? IpAssignment.DHCP : IpAssignment.STATIC);
|
||||||
|
|
||||||
|
config.setHttpProxy(mProxyInfo);
|
||||||
|
if (mProxyInfo == null) {
|
||||||
|
config.setProxySettings(ProxySettings.NONE);
|
||||||
|
} else {
|
||||||
|
config.setProxySettings(
|
||||||
|
mProxyInfo.getPacFileUrl() == null ? ProxySettings.STATIC
|
||||||
|
: ProxySettings.PAC);
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import android.os.Parcelable;
|
|||||||
|
|
||||||
import com.android.net.module.util.InetAddressUtils;
|
import com.android.net.module.util.InetAddressUtils;
|
||||||
|
|
||||||
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -33,24 +34,7 @@ import java.util.Objects;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that describes static IP configuration.
|
* Class that describes static IP configuration.
|
||||||
*
|
|
||||||
* <p>This class is different from {@link LinkProperties} because it represents
|
|
||||||
* configuration intent. The general contract is that if we can represent
|
|
||||||
* a configuration here, then we should be able to configure it on a network.
|
|
||||||
* The intent is that it closely match the UI we have for configuring networks.
|
|
||||||
*
|
|
||||||
* <p>In contrast, {@link LinkProperties} represents current state. It is much more
|
|
||||||
* expressive. For example, it supports multiple IP addresses, multiple routes,
|
|
||||||
* stacked interfaces, and so on. Because LinkProperties is so expressive,
|
|
||||||
* using it to represent configuration intent as well as current state causes
|
|
||||||
* problems. For example, we could unknowingly save a configuration that we are
|
|
||||||
* not in fact capable of applying, or we could save a configuration that the
|
|
||||||
* UI cannot display, which has the potential for malicious code to hide
|
|
||||||
* hostile or unexpected configuration from the user.
|
|
||||||
*
|
|
||||||
* @hide
|
|
||||||
*/
|
*/
|
||||||
@SystemApi
|
|
||||||
public final class StaticIpConfiguration implements Parcelable {
|
public final class StaticIpConfiguration implements Parcelable {
|
||||||
/** @hide */
|
/** @hide */
|
||||||
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
|
||||||
@@ -69,10 +53,14 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
@Nullable
|
@Nullable
|
||||||
public String domains;
|
public String domains;
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public StaticIpConfiguration() {
|
public StaticIpConfiguration() {
|
||||||
dnsServers = new ArrayList<>();
|
dnsServers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public StaticIpConfiguration(@Nullable StaticIpConfiguration source) {
|
public StaticIpConfiguration(@Nullable StaticIpConfiguration source) {
|
||||||
this();
|
this();
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
@@ -84,6 +72,8 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@SystemApi
|
||||||
public void clear() {
|
public void clear() {
|
||||||
ipAddress = null;
|
ipAddress = null;
|
||||||
gateway = null;
|
gateway = null;
|
||||||
@@ -94,7 +84,7 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
/**
|
/**
|
||||||
* Get the static IP address included in the configuration.
|
* Get the static IP address included in the configuration.
|
||||||
*/
|
*/
|
||||||
public @Nullable LinkAddress getIpAddress() {
|
public @NonNull LinkAddress getIpAddress() {
|
||||||
return ipAddress;
|
return ipAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,10 +120,15 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
private String mDomains;
|
private String mDomains;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the IP address to be included in the configuration; null by default.
|
* Set the IP address to be included in the configuration.
|
||||||
|
*
|
||||||
* @return The {@link Builder} for chaining.
|
* @return The {@link Builder} for chaining.
|
||||||
*/
|
*/
|
||||||
public @NonNull Builder setIpAddress(@Nullable LinkAddress ipAddress) {
|
public @NonNull Builder setIpAddress(@NonNull LinkAddress ipAddress) {
|
||||||
|
if (ipAddress != null && !(ipAddress.getAddress() instanceof Inet4Address)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Only IPv4 addresses can be used for the IP configuration");
|
||||||
|
}
|
||||||
mIpAddress = ipAddress;
|
mIpAddress = ipAddress;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -143,6 +138,10 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
* @return The {@link Builder} for chaining.
|
* @return The {@link Builder} for chaining.
|
||||||
*/
|
*/
|
||||||
public @NonNull Builder setGateway(@Nullable InetAddress gateway) {
|
public @NonNull Builder setGateway(@Nullable InetAddress gateway) {
|
||||||
|
if (gateway != null && !(gateway instanceof Inet4Address)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Only IPv4 addresses can be used for the gateway configuration");
|
||||||
|
}
|
||||||
mGateway = gateway;
|
mGateway = gateway;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -153,6 +152,12 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
*/
|
*/
|
||||||
public @NonNull Builder setDnsServers(@NonNull Iterable<InetAddress> dnsServers) {
|
public @NonNull Builder setDnsServers(@NonNull Iterable<InetAddress> dnsServers) {
|
||||||
Objects.requireNonNull(dnsServers);
|
Objects.requireNonNull(dnsServers);
|
||||||
|
for (InetAddress inetAddress: dnsServers) {
|
||||||
|
if (!(inetAddress instanceof Inet4Address)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Only IPv4 addresses can be used for the DNS server configuration");
|
||||||
|
}
|
||||||
|
}
|
||||||
mDnsServers = dnsServers;
|
mDnsServers = dnsServers;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -171,6 +176,8 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
/**
|
/**
|
||||||
* Create a {@link StaticIpConfiguration} from the parameters in this {@link Builder}.
|
* Create a {@link StaticIpConfiguration} from the parameters in this {@link Builder}.
|
||||||
* @return The newly created StaticIpConfiguration.
|
* @return The newly created StaticIpConfiguration.
|
||||||
|
* @throws IllegalArgumentException if an invalid configuration is attempted, e.g.
|
||||||
|
* if an IP Address was not configured via {@link #setIpAddress(LinkAddress)}.
|
||||||
*/
|
*/
|
||||||
public @NonNull StaticIpConfiguration build() {
|
public @NonNull StaticIpConfiguration build() {
|
||||||
final StaticIpConfiguration config = new StaticIpConfiguration();
|
final StaticIpConfiguration config = new StaticIpConfiguration();
|
||||||
@@ -188,7 +195,9 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a DNS server to this configuration.
|
* Add a DNS server to this configuration.
|
||||||
|
* @hide
|
||||||
*/
|
*/
|
||||||
|
@SystemApi
|
||||||
public void addDnsServer(@NonNull InetAddress server) {
|
public void addDnsServer(@NonNull InetAddress server) {
|
||||||
dnsServers.add(server);
|
dnsServers.add(server);
|
||||||
}
|
}
|
||||||
@@ -197,7 +206,9 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
* Returns the network routes specified by this object. Will typically include a
|
* Returns the network routes specified by this object. Will typically include a
|
||||||
* directly-connected route for the IP address's local subnet and a default route.
|
* directly-connected route for the IP address's local subnet and a default route.
|
||||||
* @param iface Interface to include in the routes.
|
* @param iface Interface to include in the routes.
|
||||||
|
* @hide
|
||||||
*/
|
*/
|
||||||
|
@SystemApi
|
||||||
public @NonNull List<RouteInfo> getRoutes(@Nullable String iface) {
|
public @NonNull List<RouteInfo> getRoutes(@Nullable String iface) {
|
||||||
List<RouteInfo> routes = new ArrayList<RouteInfo>(3);
|
List<RouteInfo> routes = new ArrayList<RouteInfo>(3);
|
||||||
if (ipAddress != null) {
|
if (ipAddress != null) {
|
||||||
@@ -305,7 +316,7 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
|
|
||||||
/** Implement the Parcelable interface */
|
/** Implement the Parcelable interface */
|
||||||
@Override
|
@Override
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(@NonNull Parcel dest, int flags) {
|
||||||
dest.writeParcelable(ipAddress, flags);
|
dest.writeParcelable(ipAddress, flags);
|
||||||
InetAddressUtils.parcelInetAddress(dest, gateway, flags);
|
InetAddressUtils.parcelInetAddress(dest, gateway, flags);
|
||||||
dest.writeInt(dnsServers.size());
|
dest.writeInt(dnsServers.size());
|
||||||
@@ -316,7 +327,7 @@ public final class StaticIpConfiguration implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @hide */
|
/** @hide */
|
||||||
public static StaticIpConfiguration readFromParcel(Parcel in) {
|
public static @NonNull StaticIpConfiguration readFromParcel(Parcel in) {
|
||||||
final StaticIpConfiguration s = new StaticIpConfiguration();
|
final StaticIpConfiguration s = new StaticIpConfiguration();
|
||||||
s.ipAddress = in.readParcelable(null);
|
s.ipAddress = in.readParcelable(null);
|
||||||
s.gateway = InetAddressUtils.unparcelInetAddress(in);
|
s.gateway = InetAddressUtils.unparcelInetAddress(in);
|
||||||
|
|||||||
@@ -25,12 +25,16 @@ import android.net.IpConfiguration;
|
|||||||
import android.net.LinkAddress;
|
import android.net.LinkAddress;
|
||||||
import android.net.ProxyInfo;
|
import android.net.ProxyInfo;
|
||||||
import android.net.StaticIpConfiguration;
|
import android.net.StaticIpConfiguration;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.android.testutils.DevSdkIgnoreRule;
|
||||||
|
|
||||||
import libcore.net.InetAddressUtils;
|
import libcore.net.InetAddressUtils;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@@ -50,6 +54,9 @@ public final class IpConfigurationTest {
|
|||||||
private StaticIpConfiguration mStaticIpConfig;
|
private StaticIpConfiguration mStaticIpConfig;
|
||||||
private ProxyInfo mProxy;
|
private ProxyInfo mProxy;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
dnsServers.add(DNS1);
|
dnsServers.add(DNS1);
|
||||||
@@ -99,6 +106,18 @@ public final class IpConfigurationTest {
|
|||||||
assertIpConfigurationEqual(ipConfig, new IpConfiguration(ipConfig));
|
assertIpConfigurationEqual(ipConfig, new IpConfiguration(ipConfig));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
|
||||||
|
@Test
|
||||||
|
public void testBuilder() {
|
||||||
|
final IpConfiguration c = new IpConfiguration.Builder()
|
||||||
|
.setStaticIpConfiguration(mStaticIpConfig)
|
||||||
|
.setHttpProxy(mProxy)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assertEquals(mStaticIpConfig, c.getStaticIpConfiguration());
|
||||||
|
assertEquals(mProxy, c.getHttpProxy());
|
||||||
|
}
|
||||||
|
|
||||||
private void checkEmpty(IpConfiguration config) {
|
private void checkEmpty(IpConfiguration config) {
|
||||||
assertEquals(IpConfiguration.IpAssignment.UNASSIGNED,
|
assertEquals(IpConfiguration.IpAssignment.UNASSIGNED,
|
||||||
config.getIpAssignment().UNASSIGNED);
|
config.getIpAssignment().UNASSIGNED);
|
||||||
|
|||||||
@@ -14,20 +14,30 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package android.net;
|
package android.net.cts;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import android.net.IpPrefix;
|
||||||
|
import android.net.LinkAddress;
|
||||||
|
import android.net.LinkProperties;
|
||||||
|
import android.net.RouteInfo;
|
||||||
|
import android.net.StaticIpConfiguration;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
|
|
||||||
import androidx.test.filters.SmallTest;
|
import androidx.test.filters.SmallTest;
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.android.testutils.DevSdkIgnoreRule;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
@@ -42,15 +52,20 @@ public class StaticIpConfigurationTest {
|
|||||||
|
|
||||||
private static final String ADDRSTR = "192.0.2.2/25";
|
private static final String ADDRSTR = "192.0.2.2/25";
|
||||||
private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
|
private static final LinkAddress ADDR = new LinkAddress(ADDRSTR);
|
||||||
private static final InetAddress GATEWAY = IpAddress("192.0.2.1");
|
private static final InetAddress GATEWAY = ipAddress("192.0.2.1");
|
||||||
private static final InetAddress OFFLINKGATEWAY = IpAddress("192.0.2.129");
|
private static final InetAddress OFFLINKGATEWAY = ipAddress("192.0.2.129");
|
||||||
private static final InetAddress DNS1 = IpAddress("8.8.8.8");
|
private static final InetAddress DNS1 = ipAddress("8.8.8.8");
|
||||||
private static final InetAddress DNS2 = IpAddress("8.8.4.4");
|
private static final InetAddress DNS2 = ipAddress("8.8.4.4");
|
||||||
private static final InetAddress DNS3 = IpAddress("4.2.2.2");
|
private static final InetAddress DNS3 = ipAddress("4.2.2.2");
|
||||||
|
private static final InetAddress IPV6_ADDRESS = ipAddress("2001:4860:800d::68");
|
||||||
|
private static final LinkAddress IPV6_LINK_ADDRESS = new LinkAddress("2001:db8::1/64");
|
||||||
private static final String IFACE = "eth0";
|
private static final String IFACE = "eth0";
|
||||||
private static final String FAKE_DOMAINS = "google.com";
|
private static final String FAKE_DOMAINS = "google.com";
|
||||||
|
|
||||||
private static InetAddress IpAddress(String addr) {
|
@Rule
|
||||||
|
public final DevSdkIgnoreRule mIgnoreRule = new DevSdkIgnoreRule();
|
||||||
|
|
||||||
|
private static InetAddress ipAddress(String addr) {
|
||||||
return InetAddress.parseNumericAddress(addr);
|
return InetAddress.parseNumericAddress(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,6 +256,29 @@ public class StaticIpConfigurationTest {
|
|||||||
assertEquals(DNS1, s.getDnsServers().get(0));
|
assertEquals(DNS1, s.getDnsServers().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
|
||||||
|
@Test
|
||||||
|
public void testIllegalBuilders() {
|
||||||
|
assertThrows("Can't set IP Address to IPv6!", IllegalArgumentException.class, () -> {
|
||||||
|
StaticIpConfiguration.Builder b = new StaticIpConfiguration.Builder().setIpAddress(
|
||||||
|
IPV6_LINK_ADDRESS);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows("Can't set gateway to IPv6!", IllegalArgumentException.class, () -> {
|
||||||
|
StaticIpConfiguration.Builder b = new StaticIpConfiguration.Builder().setGateway(
|
||||||
|
IPV6_ADDRESS);
|
||||||
|
});
|
||||||
|
|
||||||
|
assertThrows("Can't set DNS servers using IPv6!", IllegalArgumentException.class, () -> {
|
||||||
|
final ArrayList<InetAddress> dnsServers = new ArrayList<>();
|
||||||
|
dnsServers.add(DNS1);
|
||||||
|
dnsServers.add(IPV6_ADDRESS);
|
||||||
|
|
||||||
|
StaticIpConfiguration.Builder b = new StaticIpConfiguration.Builder().setDnsServers(
|
||||||
|
dnsServers);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddDnsServers() {
|
public void testAddDnsServers() {
|
||||||
final StaticIpConfiguration s = new StaticIpConfiguration((StaticIpConfiguration) null);
|
final StaticIpConfiguration s = new StaticIpConfiguration((StaticIpConfiguration) null);
|
||||||
Reference in New Issue
Block a user