Merge "[Thread] resolve open comments for aosp/2737373" into main
diff --git a/Tethering/common/TetheringLib/api/lint-baseline.txt b/Tethering/common/TetheringLib/api/lint-baseline.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Tethering/common/TetheringLib/api/lint-baseline.txt
diff --git a/Tethering/common/TetheringLib/api/module-lib-lint-baseline.txt b/Tethering/common/TetheringLib/api/module-lib-lint-baseline.txt
new file mode 100644
index 0000000..1d09598
--- /dev/null
+++ b/Tethering/common/TetheringLib/api/module-lib-lint-baseline.txt
@@ -0,0 +1,23 @@
+// Baseline format: 1.0
+BroadcastBehavior: android.net.TetheringManager#ACTION_TETHER_STATE_CHANGED:
+ Field 'ACTION_TETHER_STATE_CHANGED' is missing @BroadcastBehavior
+
+
+RequiresPermission: android.net.TetheringManager#requestLatestTetheringEntitlementResult(int, boolean, java.util.concurrent.Executor, android.net.TetheringManager.OnTetheringEntitlementResultListener):
+ Method 'requestLatestTetheringEntitlementResult' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#startTethering(android.net.TetheringManager.TetheringRequest, java.util.concurrent.Executor, android.net.TetheringManager.StartTetheringCallback):
+ Method 'startTethering' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#startTethering(int, java.util.concurrent.Executor, android.net.TetheringManager.StartTetheringCallback):
+ Method 'startTethering' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#stopAllTethering():
+ Method 'stopAllTethering' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#stopTethering(int):
+ Method 'stopTethering' documentation mentions permissions already declared by @RequiresPermission
+
+
+SdkConstant: android.net.TetheringManager#ACTION_TETHER_STATE_CHANGED:
+ Field 'ACTION_TETHER_STATE_CHANGED' is missing @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
+
+
+Todo: android.net.TetheringConstants:
+ Documentation mentions 'TODO'
diff --git a/Tethering/common/TetheringLib/api/system-lint-baseline.txt b/Tethering/common/TetheringLib/api/system-lint-baseline.txt
new file mode 100644
index 0000000..e678ce1
--- /dev/null
+++ b/Tethering/common/TetheringLib/api/system-lint-baseline.txt
@@ -0,0 +1,17 @@
+// Baseline format: 1.0
+BroadcastBehavior: android.net.TetheringManager#ACTION_TETHER_STATE_CHANGED:
+ Field 'ACTION_TETHER_STATE_CHANGED' is missing @BroadcastBehavior
+
+
+RequiresPermission: android.net.TetheringManager#requestLatestTetheringEntitlementResult(int, boolean, java.util.concurrent.Executor, android.net.TetheringManager.OnTetheringEntitlementResultListener):
+ Method 'requestLatestTetheringEntitlementResult' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#startTethering(android.net.TetheringManager.TetheringRequest, java.util.concurrent.Executor, android.net.TetheringManager.StartTetheringCallback):
+ Method 'startTethering' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#stopAllTethering():
+ Method 'stopAllTethering' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.TetheringManager#stopTethering(int):
+ Method 'stopTethering' documentation mentions permissions already declared by @RequiresPermission
+
+
+SdkConstant: android.net.TetheringManager#ACTION_TETHER_STATE_CHANGED:
+ Field 'ACTION_TETHER_STATE_CHANGED' is missing @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
diff --git a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
index 2b14a42..4bd7e6a 100644
--- a/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
+++ b/Tethering/src/com/android/networkstack/tethering/BpfCoordinator.java
@@ -1597,10 +1597,8 @@
@Override
public int hashCode() {
- // TODO: if this is ever used in production code, don't pass ifindices
- // to Objects.hash() to avoid autoboxing overhead.
- return Objects.hash(upstreamIfindex, downstreamIfindex, sourcePrefix, inDstMac,
- outSrcMac, outDstMac);
+ return 13 * upstreamIfindex + 41 * downstreamIfindex
+ + Objects.hash(sourcePrefix, inDstMac, outSrcMac, outDstMac);
}
@Override
@@ -1725,9 +1723,8 @@
@Override
public int hashCode() {
- // TODO: if this is ever used in production code, don't pass ifindices
- // to Objects.hash() to avoid autoboxing overhead.
- return Objects.hash(upstreamIfindex, downstreamIfindex, address, srcMac, dstMac);
+ return 13 * upstreamIfindex + 41 * downstreamIfindex
+ + Objects.hash(address, srcMac, dstMac);
}
@Override
diff --git a/framework-t/api/OWNERS b/framework-t/api/OWNERS
index 607f85a..8ef735c 100644
--- a/framework-t/api/OWNERS
+++ b/framework-t/api/OWNERS
@@ -1,2 +1,3 @@
file:platform/packages/modules/Connectivity:main:/nearby/OWNERS
file:platform/packages/modules/Connectivity:main:/remoteauth/OWNERS
+file:platform/packages/modules/Connectivity:main:/thread/OWNERS
diff --git a/framework-t/api/module-lib-lint-baseline.txt b/framework-t/api/module-lib-lint-baseline.txt
index 3158bd4..6f954df 100644
--- a/framework-t/api/module-lib-lint-baseline.txt
+++ b/framework-t/api/module-lib-lint-baseline.txt
@@ -5,3 +5,17 @@
Methods must not mention RuntimeException subclasses in throws clauses (was `java.lang.SecurityException`)
BannedThrow: android.app.usage.NetworkStatsManager#queryTaggedSummary(android.net.NetworkTemplate, long, long):
Methods must not mention RuntimeException subclasses in throws clauses (was `java.lang.SecurityException`)
+
+
+MissingPermission: android.net.IpSecManager#startTunnelModeTransformMigration(android.net.IpSecTransform, java.net.InetAddress, java.net.InetAddress):
+ Feature field FEATURE_IPSEC_TUNNEL_MIGRATION required by method android.net.IpSecManager.startTunnelModeTransformMigration(android.net.IpSecTransform, java.net.InetAddress, java.net.InetAddress) is hidden or removed
+
+
+RequiresPermission: android.app.usage.NetworkStatsManager#registerUsageCallback(android.net.NetworkTemplate, long, java.util.concurrent.Executor, android.app.usage.NetworkStatsManager.UsageCallback):
+ Method 'registerUsageCallback' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.EthernetManager#disableInterface(String, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'disableInterface' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.EthernetManager#enableInterface(String, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'enableInterface' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.EthernetManager#updateConfiguration(String, android.net.EthernetNetworkUpdateRequest, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'updateConfiguration' documentation mentions permissions already declared by @RequiresPermission
diff --git a/framework-t/api/system-current.txt b/framework-t/api/system-current.txt
index 08c2e66..b285d85 100644
--- a/framework-t/api/system-current.txt
+++ b/framework-t/api/system-current.txt
@@ -418,7 +418,6 @@
package android.net.thread {
@FlaggedApi("com.android.net.thread.flags.thread_enabled") public final class ActiveOperationalDataset implements android.os.Parcelable {
- method @NonNull public static android.net.thread.ActiveOperationalDataset createRandomDataset();
method public int describeContents();
method @NonNull public static android.net.thread.ActiveOperationalDataset fromThreadTlvs(@NonNull byte[]);
method @NonNull public android.net.thread.OperationalDatasetTimestamp getActiveTimestamp();
@@ -493,6 +492,7 @@
}
@FlaggedApi("com.android.net.thread.flags.thread_enabled") public final class ThreadNetworkController {
+ method public void createRandomizedDataset(@NonNull String, @NonNull java.util.concurrent.Executor, @NonNull android.os.OutcomeReceiver<android.net.thread.ActiveOperationalDataset,android.net.thread.ThreadNetworkException>);
method public int getThreadVersion();
method public static boolean isAttached(int);
method @RequiresPermission("android.permission.THREAD_NETWORK_PRIVILEGED") public void join(@NonNull android.net.thread.ActiveOperationalDataset, @NonNull java.util.concurrent.Executor, @NonNull android.os.OutcomeReceiver<java.lang.Void,android.net.thread.ThreadNetworkException>);
diff --git a/framework-t/api/system-lint-baseline.txt b/framework-t/api/system-lint-baseline.txt
index c6055f6..4f7af87 100644
--- a/framework-t/api/system-lint-baseline.txt
+++ b/framework-t/api/system-lint-baseline.txt
@@ -7,6 +7,18 @@
Methods must not throw generic exceptions (`java.lang.Throwable`)
+MissingPermission: android.net.IpSecManager#startTunnelModeTransformMigration(android.net.IpSecTransform, java.net.InetAddress, java.net.InetAddress):
+ Feature field FEATURE_IPSEC_TUNNEL_MIGRATION required by method android.net.IpSecManager.startTunnelModeTransformMigration(android.net.IpSecTransform, java.net.InetAddress, java.net.InetAddress) is hidden or removed
+
+
+RequiresPermission: android.net.EthernetManager#disableInterface(String, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'disableInterface' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.EthernetManager#enableInterface(String, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'enableInterface' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.EthernetManager#updateConfiguration(String, android.net.EthernetNetworkUpdateRequest, java.util.concurrent.Executor, android.os.OutcomeReceiver<java.lang.String,android.net.EthernetNetworkManagementException>):
+ Method 'updateConfiguration' documentation mentions permissions already declared by @RequiresPermission
+
+
UnflaggedApi: android.nearby.CredentialElement#equals(Object):
New API must be flagged with @FlaggedApi: method android.nearby.CredentialElement.equals(Object)
UnflaggedApi: android.nearby.CredentialElement#hashCode():
diff --git a/framework/api/lint-baseline.txt b/framework/api/lint-baseline.txt
index 2f4004a..4465bcb 100644
--- a/framework/api/lint-baseline.txt
+++ b/framework/api/lint-baseline.txt
@@ -1,4 +1,19 @@
// Baseline format: 1.0
+BroadcastBehavior: android.net.ConnectivityManager#ACTION_BACKGROUND_DATA_SETTING_CHANGED:
+ Field 'ACTION_BACKGROUND_DATA_SETTING_CHANGED' is missing @BroadcastBehavior
+
+
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.app.PendingIntent):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities#getOwnerUid():
+ Method 'getOwnerUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.http.BidirectionalStream.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.http.UrlRequest.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
+
+
VisiblySynchronized: android.net.NetworkInfo#toString():
Internal locks must not be exposed (synchronizing on this or class is still
- externally observable): method android.net.NetworkInfo.toString()
diff --git a/framework/api/module-lib-current.txt b/framework/api/module-lib-current.txt
index bfb4981..026d8a9 100644
--- a/framework/api/module-lib-current.txt
+++ b/framework/api/module-lib-current.txt
@@ -235,6 +235,7 @@
public final class VpnTransportInfo implements android.os.Parcelable android.net.TransportInfo {
ctor @Deprecated public VpnTransportInfo(int, @Nullable String);
+ method public long getApplicableRedactions();
method @Nullable public String getSessionId();
method @NonNull public android.net.VpnTransportInfo makeCopy(long);
}
diff --git a/framework/api/module-lib-lint-baseline.txt b/framework/api/module-lib-lint-baseline.txt
new file mode 100644
index 0000000..53a8c5e
--- /dev/null
+++ b/framework/api/module-lib-lint-baseline.txt
@@ -0,0 +1,33 @@
+// Baseline format: 1.0
+BroadcastBehavior: android.net.ConnectivityManager#ACTION_BACKGROUND_DATA_SETTING_CHANGED:
+ Field 'ACTION_BACKGROUND_DATA_SETTING_CHANGED' is missing @BroadcastBehavior
+
+
+RequiresPermission: android.net.ConnectivityManager#isTetheringSupported():
+ Method 'isTetheringSupported' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.app.PendingIntent):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestRouteToHostAddress(int, java.net.InetAddress):
+ Method 'requestRouteToHostAddress' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.LinkProperties#getCaptivePortalApiUrl():
+ Method 'getCaptivePortalApiUrl' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.LinkProperties#getCaptivePortalData():
+ Method 'getCaptivePortalData' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities#getOwnerUid():
+ Method 'getOwnerUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities#getUnderlyingNetworks():
+ Method 'getUnderlyingNetworks' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities.Builder#setAllowedUids(java.util.Set<java.lang.Integer>):
+ Method 'setAllowedUids' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities.Builder#setSignalStrength(int):
+ Method 'setSignalStrength' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities.Builder#setUnderlyingNetworks(java.util.List<android.net.Network>):
+ Method 'setUnderlyingNetworks' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.NetworkRequest.Builder#setSignalStrength(int):
+ Method 'setSignalStrength' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.http.BidirectionalStream.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.http.UrlRequest.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
diff --git a/framework/api/system-lint-baseline.txt b/framework/api/system-lint-baseline.txt
index 9a97707..3ac97c0 100644
--- a/framework/api/system-lint-baseline.txt
+++ b/framework/api/system-lint-baseline.txt
@@ -1 +1,29 @@
// Baseline format: 1.0
+BroadcastBehavior: android.net.ConnectivityManager#ACTION_BACKGROUND_DATA_SETTING_CHANGED:
+ Field 'ACTION_BACKGROUND_DATA_SETTING_CHANGED' is missing @BroadcastBehavior
+
+
+RequiresPermission: android.net.ConnectivityManager#isTetheringSupported():
+ Method 'isTetheringSupported' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.app.PendingIntent):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.ConnectivityManager#requestNetwork(android.net.NetworkRequest, android.net.ConnectivityManager.NetworkCallback):
+ Method 'requestNetwork' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.LinkProperties#getCaptivePortalApiUrl():
+ Method 'getCaptivePortalApiUrl' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.LinkProperties#getCaptivePortalData():
+ Method 'getCaptivePortalData' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities#getOwnerUid():
+ Method 'getOwnerUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities#getUnderlyingNetworks():
+ Method 'getUnderlyingNetworks' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities.Builder#setSignalStrength(int):
+ Method 'setSignalStrength' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.NetworkCapabilities.Builder#setUnderlyingNetworks(java.util.List<android.net.Network>):
+ Method 'setUnderlyingNetworks' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.NetworkRequest.Builder#setSignalStrength(int):
+ Method 'setSignalStrength' documentation mentions permissions already declared by @RequiresPermission
+RequiresPermission: android.net.http.BidirectionalStream.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
+RequiresPermission: android.net.http.UrlRequest.Builder#setTrafficStatsUid(int):
+ Method 'setTrafficStatsUid' documentation mentions permissions without declaring @RequiresPermission
diff --git a/framework/src/android/net/LocalNetworkConfig.java b/framework/src/android/net/LocalNetworkConfig.java
index fca7fd1..17b1064 100644
--- a/framework/src/android/net/LocalNetworkConfig.java
+++ b/framework/src/android/net/LocalNetworkConfig.java
@@ -62,11 +62,19 @@
return mUpstreamSelector;
}
- public @NonNull MulticastRoutingConfig getUpstreamMulticastRoutingConfig() {
+ /**
+ * Get the upstream multicast routing config
+ */
+ @NonNull
+ public MulticastRoutingConfig getUpstreamMulticastRoutingConfig() {
return mUpstreamMulticastRoutingConfig;
}
- public @NonNull MulticastRoutingConfig getDownstreamMulticastRoutingConfig() {
+ /**
+ * Get the downstream multicast routing config
+ */
+ @NonNull
+ public MulticastRoutingConfig getDownstreamMulticastRoutingConfig() {
return mDownstreamMulticastRoutingConfig;
}
@@ -82,6 +90,15 @@
dest.writeParcelable(mDownstreamMulticastRoutingConfig, flags);
}
+ @Override
+ public String toString() {
+ return "LocalNetworkConfig{"
+ + "UpstreamSelector=" + mUpstreamSelector
+ + ", UpstreamMulticastConfig=" + mUpstreamMulticastRoutingConfig
+ + ", DownstreamMulticastConfig=" + mDownstreamMulticastRoutingConfig
+ + '}';
+ }
+
public static final @NonNull Creator<LocalNetworkConfig> CREATOR = new Creator<>() {
public LocalNetworkConfig createFromParcel(Parcel in) {
final NetworkRequest upstreamSelector = in.readParcelable(null);
@@ -100,13 +117,13 @@
public static final class Builder {
@Nullable
- NetworkRequest mUpstreamSelector;
+ private NetworkRequest mUpstreamSelector;
@Nullable
- MulticastRoutingConfig mUpstreamMulticastRoutingConfig;
+ private MulticastRoutingConfig mUpstreamMulticastRoutingConfig;
@Nullable
- MulticastRoutingConfig mDownstreamMulticastRoutingConfig;
+ private MulticastRoutingConfig mDownstreamMulticastRoutingConfig;
/**
* Create a Builder
diff --git a/framework/src/android/net/MulticastRoutingConfig.java b/framework/src/android/net/MulticastRoutingConfig.java
index ebd9fc5..4a3e1be 100644
--- a/framework/src/android/net/MulticastRoutingConfig.java
+++ b/framework/src/android/net/MulticastRoutingConfig.java
@@ -21,6 +21,7 @@
import android.annotation.Nullable;
import android.os.Parcel;
import android.os.Parcelable;
+import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Log;
@@ -30,7 +31,9 @@
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Objects;
import java.util.Set;
+import java.util.StringJoiner;
/**
* A class representing a configuration for multicast routing.
@@ -38,8 +41,8 @@
* Internal usage to Connectivity
* @hide
*/
-// TODO : @SystemApi
-public class MulticastRoutingConfig implements Parcelable {
+// @SystemApi(client = MODULE_LIBRARIES)
+public final class MulticastRoutingConfig implements Parcelable {
private static final String TAG = MulticastRoutingConfig.class.getSimpleName();
/** Do not forward any multicast packets. */
@@ -55,6 +58,7 @@
*/
public static final int FORWARD_WITH_MIN_SCOPE = 2;
+ /** @hide */
@Retention(RetentionPolicy.SOURCE)
@IntDef(prefix = { "FORWARD_" }, value = {
FORWARD_NONE,
@@ -68,6 +72,7 @@
*/
public static final int MULTICAST_SCOPE_NONE = -1;
+ /** @hide */
public static final MulticastRoutingConfig CONFIG_FORWARD_NONE =
new MulticastRoutingConfig(FORWARD_NONE, MULTICAST_SCOPE_NONE, null);
@@ -102,7 +107,7 @@
* Returns the minimal group address scope that is allowed for forwarding.
* If the forwarding mode is not FORWARD_WITH_MIN_SCOPE, will be MULTICAST_SCOPE_NONE.
*/
- public int getMinScope() {
+ public int getMinimumScope() {
return mMinScope;
}
@@ -111,7 +116,7 @@
* The list will be empty if the forwarding mode is not FORWARD_SELECTED.
*/
@NonNull
- public Set<Inet6Address> getMulticastListeningAddresses() {
+ public Set<Inet6Address> getListeningAddresses() {
return mListeningAddresses;
}
@@ -133,7 +138,7 @@
}
@Override
- public void writeToParcel(Parcel dest, int flags) {
+ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeInt(mForwardingMode);
dest.writeInt(mMinScope);
dest.writeInt(mListeningAddresses.size());
@@ -147,9 +152,10 @@
return 0;
}
+ @NonNull
public static final Creator<MulticastRoutingConfig> CREATOR = new Creator<>() {
@Override
- public MulticastRoutingConfig createFromParcel(Parcel in) {
+ public MulticastRoutingConfig createFromParcel(@NonNull Parcel in) {
return new MulticastRoutingConfig(in);
}
@@ -159,43 +165,67 @@
}
};
- public static class Builder {
+ private static String forwardingModeToString(final int forwardingMode) {
+ switch (forwardingMode) {
+ case FORWARD_NONE: return "NONE";
+ case FORWARD_SELECTED: return "SELECTED";
+ case FORWARD_WITH_MIN_SCOPE: return "WITH_MIN_SCOPE";
+ default: return "UNKNOWN";
+ }
+ }
+
+ public static final class Builder {
@MulticastForwardingMode
private final int mForwardingMode;
private int mMinScope;
private final ArraySet<Inet6Address> mListeningAddresses;
- private Builder(@MulticastForwardingMode final int mode, int scope) {
+ // The two constructors with runtime checks for the mode and scope are arguably
+ // less convenient than three static factory methods, but API guidelines mandates
+ // that Builders are built with a constructor and not factory methods.
+ /**
+ * Create a new builder for forwarding mode FORWARD_NONE or FORWARD_SELECTED.
+ *
+ * <p>On a Builder for FORWARD_NONE, no properties can be set.
+ * <p>On a Builder for FORWARD_SELECTED, listening addresses can be added and removed
+ * but the minimum scope can't be set.
+ *
+ * @param mode {@link #FORWARD_NONE} or {@link #FORWARD_SELECTED}. Any other
+ * value will result in IllegalArgumentException.
+ * @see #Builder(int, int)
+ */
+ public Builder(@MulticastForwardingMode final int mode) {
+ if (FORWARD_NONE != mode && FORWARD_SELECTED != mode) {
+ if (FORWARD_WITH_MIN_SCOPE == mode) {
+ throw new IllegalArgumentException("FORWARD_WITH_MIN_SCOPE requires "
+ + "passing the scope as a second argument");
+ } else {
+ throw new IllegalArgumentException("Unknown forwarding mode : " + mode);
+ }
+ }
mForwardingMode = mode;
- mMinScope = scope;
+ mMinScope = MULTICAST_SCOPE_NONE;
mListeningAddresses = new ArraySet<>();
}
/**
- * Create a builder that forwards nothing.
- * No properties can be set on such a builder.
- */
- public static Builder newBuilderForwardingNone() {
- return new Builder(FORWARD_NONE, MULTICAST_SCOPE_NONE);
- }
-
- /**
- * Create a builder that forwards packets above a certain scope
+ * Create a new builder for forwarding mode FORWARD_WITH_MIN_SCOPE.
*
- * The scope can be changed on this builder, but not the listening addresses.
- * @param scope the initial scope
- */
- public static Builder newBuilderWithMinScope(final int scope) {
- return new Builder(FORWARD_WITH_MIN_SCOPE, scope);
- }
-
- /**
- * Create a builder that forwards a specified list of listening addresses.
+ * <p>On this Builder the scope can be set with {@link #setMinimumScope}, but
+ * listening addresses can't be added or removed.
*
- * Addresses can be added and removed from this builder, but the scope can't be set.
+ * @param mode Must be {@link #FORWARD_WITH_MIN_SCOPE}.
+ * @param scope the minimum scope for this multicast routing config.
+ * @see Builder#Builder(int)
*/
- public static Builder newBuilderWithListeningAddresses() {
- return new Builder(FORWARD_SELECTED, MULTICAST_SCOPE_NONE);
+ public Builder(@MulticastForwardingMode final int mode, int scope) {
+ if (FORWARD_WITH_MIN_SCOPE != mode) {
+ throw new IllegalArgumentException("Forwarding with a min scope must "
+ + "use forward mode FORWARD_WITH_MIN_SCOPE");
+ }
+ mForwardingMode = mode;
+ mMinScope = scope;
+ mListeningAddresses = new ArraySet<>();
}
/**
@@ -203,6 +233,7 @@
* This is only meaningful (indeed, allowed) for configs in FORWARD_WITH_MIN_SCOPE mode.
* @return this builder
*/
+ @NonNull
public Builder setMinimumScope(final int scope) {
if (FORWARD_WITH_MIN_SCOPE != mForwardingMode) {
throw new IllegalArgumentException("Can't set the scope on a builder in mode "
@@ -219,6 +250,7 @@
* If this address was already added, this is a no-op.
* @return this builder
*/
+ @NonNull
public Builder addListeningAddress(@NonNull final Inet6Address address) {
if (FORWARD_SELECTED != mForwardingMode) {
throw new IllegalArgumentException("Can't add an address on a builder in mode "
@@ -236,7 +268,8 @@
* If this address was not added, or was already removed, this is a no-op.
* @return this builder
*/
- public Builder removeListeningAddress(@NonNull final Inet6Address address) {
+ @NonNull
+ public Builder clearListeningAddress(@NonNull final Inet6Address address) {
if (FORWARD_SELECTED != mForwardingMode) {
throw new IllegalArgumentException("Can't remove an address on a builder in mode "
+ modeToString(mForwardingMode));
@@ -248,6 +281,7 @@
/**
* Build the config.
*/
+ @NonNull
public MulticastRoutingConfig build() {
return new MulticastRoutingConfig(mForwardingMode, mMinScope, mListeningAddresses);
}
@@ -261,4 +295,41 @@
default: return "unknown multicast routing mode " + mode;
}
}
+
+ public boolean equals(Object other) {
+ if (other == this) {
+ return true;
+ } else if (!(other instanceof MulticastRoutingConfig)) {
+ return false;
+ } else {
+ final MulticastRoutingConfig otherConfig = (MulticastRoutingConfig) other;
+ return mForwardingMode == otherConfig.mForwardingMode
+ && mMinScope == otherConfig.mMinScope
+ && mListeningAddresses.equals(otherConfig.mListeningAddresses);
+ }
+ }
+
+ public int hashCode() {
+ return Objects.hash(mForwardingMode, mMinScope, mListeningAddresses);
+ }
+
+ public String toString() {
+ final StringJoiner resultJoiner = new StringJoiner(" ", "{", "}");
+
+ resultJoiner.add("ForwardingMode:");
+ resultJoiner.add(modeToString(mForwardingMode));
+
+ if (mForwardingMode == FORWARD_WITH_MIN_SCOPE) {
+ resultJoiner.add("MinScope:");
+ resultJoiner.add(Integer.toString(mMinScope));
+ }
+
+ if (mForwardingMode == FORWARD_SELECTED && !mListeningAddresses.isEmpty()) {
+ resultJoiner.add("ListeningAddresses: [");
+ resultJoiner.add(TextUtils.join(",", mListeningAddresses));
+ resultJoiner.add("]");
+ }
+
+ return resultJoiner.toString();
+ }
}
diff --git a/framework/src/android/net/QosSession.java b/framework/src/android/net/QosSession.java
index 25f3965..d1edae9 100644
--- a/framework/src/android/net/QosSession.java
+++ b/framework/src/android/net/QosSession.java
@@ -22,6 +22,9 @@
import android.os.Parcel;
import android.os.Parcelable;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
/**
* Provides identifying information of a QoS session. Sent to an application through
* {@link QosCallback}.
@@ -107,6 +110,7 @@
TYPE_EPS_BEARER,
TYPE_NR_BEARER,
})
+ @Retention(RetentionPolicy.SOURCE)
@interface QosSessionType {}
private QosSession(final Parcel in) {
diff --git a/nearby/framework/java/android/nearby/BroadcastRequest.java b/nearby/framework/java/android/nearby/BroadcastRequest.java
index 90f4d0f..6d6357d 100644
--- a/nearby/framework/java/android/nearby/BroadcastRequest.java
+++ b/nearby/framework/java/android/nearby/BroadcastRequest.java
@@ -88,6 +88,7 @@
* @hide
*/
@IntDef({MEDIUM_BLE})
+ @Retention(RetentionPolicy.SOURCE)
public @interface Medium {}
/**
diff --git a/nearby/framework/java/android/nearby/NearbyDevice.java b/nearby/framework/java/android/nearby/NearbyDevice.java
index e8fcc28..e7db0c5 100644
--- a/nearby/framework/java/android/nearby/NearbyDevice.java
+++ b/nearby/framework/java/android/nearby/NearbyDevice.java
@@ -25,6 +25,8 @@
import com.android.internal.util.Preconditions;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@@ -149,6 +151,7 @@
* @hide
*/
@IntDef({Medium.BLE, Medium.BLUETOOTH})
+ @Retention(RetentionPolicy.SOURCE)
public @interface Medium {
int BLE = 1;
int BLUETOOTH = 2;
diff --git a/nearby/framework/java/android/nearby/NearbyManager.java b/nearby/framework/java/android/nearby/NearbyManager.java
index a70b303..070a2b6 100644
--- a/nearby/framework/java/android/nearby/NearbyManager.java
+++ b/nearby/framework/java/android/nearby/NearbyManager.java
@@ -34,6 +34,8 @@
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.Objects;
import java.util.WeakHashMap;
@@ -63,6 +65,7 @@
ScanStatus.SUCCESS,
ScanStatus.ERROR,
})
+ @Retention(RetentionPolicy.SOURCE)
public @interface ScanStatus {
// The undetermined status, some modules may be initializing. Retry is suggested.
int UNKNOWN = 0;
diff --git a/service-t/src/com/android/server/NsdService.java b/service-t/src/com/android/server/NsdService.java
index c74f229..43357e4 100644
--- a/service-t/src/com/android/server/NsdService.java
+++ b/service-t/src/com/android/server/NsdService.java
@@ -1647,10 +1647,12 @@
mContext, MdnsFeatureFlags.INCLUDE_INET_ADDRESS_RECORDS_IN_PROBING))
.setIsExpiredServicesRemovalEnabled(mDeps.isTrunkStableFeatureEnabled(
MdnsFeatureFlags.NSD_EXPIRED_SERVICES_REMOVAL))
+ .setIsLabelCountLimitEnabled(mDeps.isTetheringFeatureNotChickenedOut(
+ mContext, MdnsFeatureFlags.NSD_LIMIT_LABEL_COUNT))
.build();
mMdnsSocketClient =
new MdnsMultinetworkSocketClient(handler.getLooper(), mMdnsSocketProvider,
- LOGGER.forSubComponent("MdnsMultinetworkSocketClient"));
+ LOGGER.forSubComponent("MdnsMultinetworkSocketClient"), flags);
mMdnsDiscoveryManager = deps.makeMdnsDiscoveryManager(new ExecutorProvider(),
mMdnsSocketClient, LOGGER.forSubComponent("MdnsDiscoveryManager"), flags);
handler.post(() -> mMdnsSocketClient.setCallback(mMdnsDiscoveryManager));
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsFeatureFlags.java b/service-t/src/com/android/server/connectivity/mdns/MdnsFeatureFlags.java
index 6f7645e..738c151 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsFeatureFlags.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsFeatureFlags.java
@@ -36,6 +36,11 @@
public static final String NSD_EXPIRED_SERVICES_REMOVAL =
"nsd_expired_services_removal";
+ /**
+ * A feature flag to control whether the label count limit should be enabled.
+ */
+ public static final String NSD_LIMIT_LABEL_COUNT = "nsd_limit_label_count";
+
// Flag for offload feature
public final boolean mIsMdnsOffloadFeatureEnabled;
@@ -45,14 +50,20 @@
// Flag for expired services removal
public final boolean mIsExpiredServicesRemovalEnabled;
+ // Flag for label count limit
+ public final boolean mIsLabelCountLimitEnabled;
+
/**
* The constructor for {@link MdnsFeatureFlags}.
*/
public MdnsFeatureFlags(boolean isOffloadFeatureEnabled,
- boolean includeInetAddressRecordsInProbing, boolean isExpiredServicesRemovalEnabled) {
+ boolean includeInetAddressRecordsInProbing,
+ boolean isExpiredServicesRemovalEnabled,
+ boolean isLabelCountLimitEnabled) {
mIsMdnsOffloadFeatureEnabled = isOffloadFeatureEnabled;
mIncludeInetAddressRecordsInProbing = includeInetAddressRecordsInProbing;
mIsExpiredServicesRemovalEnabled = isExpiredServicesRemovalEnabled;
+ mIsLabelCountLimitEnabled = isLabelCountLimitEnabled;
}
@@ -67,6 +78,7 @@
private boolean mIsMdnsOffloadFeatureEnabled;
private boolean mIncludeInetAddressRecordsInProbing;
private boolean mIsExpiredServicesRemovalEnabled;
+ private boolean mIsLabelCountLimitEnabled;
/**
* The constructor for {@link Builder}.
@@ -75,6 +87,7 @@
mIsMdnsOffloadFeatureEnabled = false;
mIncludeInetAddressRecordsInProbing = false;
mIsExpiredServicesRemovalEnabled = true; // Default enabled.
+ mIsLabelCountLimitEnabled = true; // Default enabled.
}
/**
@@ -109,11 +122,23 @@
}
/**
+ * Set whether the label count limit is enabled.
+ *
+ * @see #NSD_LIMIT_LABEL_COUNT
+ */
+ public Builder setIsLabelCountLimitEnabled(boolean isLabelCountLimitEnabled) {
+ mIsLabelCountLimitEnabled = isLabelCountLimitEnabled;
+ return this;
+ }
+
+ /**
* Builds a {@link MdnsFeatureFlags} with the arguments supplied to this builder.
*/
public MdnsFeatureFlags build() {
return new MdnsFeatureFlags(mIsMdnsOffloadFeatureEnabled,
- mIncludeInetAddressRecordsInProbing, mIsExpiredServicesRemovalEnabled);
+ mIncludeInetAddressRecordsInProbing,
+ mIsExpiredServicesRemovalEnabled,
+ mIsLabelCountLimitEnabled);
}
}
}
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiser.java b/service-t/src/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiser.java
index 42a6b0d..62c37ad 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiser.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsInterfaceAdvertiser.java
@@ -65,11 +65,12 @@
private final MdnsProber mProber;
@NonNull
private final MdnsReplySender mReplySender;
-
@NonNull
private final SharedLog mSharedLog;
@NonNull
private final byte[] mPacketCreationBuffer;
+ @NonNull
+ private final MdnsFeatureFlags mMdnsFeatureFlags;
/**
* Callbacks called by {@link MdnsInterfaceAdvertiser} to report status updates.
@@ -213,6 +214,7 @@
mProber = deps.makeMdnsProber(sharedLog.getTag(), looper, mReplySender, mProbingCallback,
sharedLog);
mSharedLog = sharedLog;
+ mMdnsFeatureFlags = mdnsFeatureFlags;
}
/**
@@ -351,7 +353,7 @@
public void handlePacket(byte[] recvbuf, int length, InetSocketAddress src) {
final MdnsPacket packet;
try {
- packet = MdnsPacket.parse(new MdnsPacketReader(recvbuf, length));
+ packet = MdnsPacket.parse(new MdnsPacketReader(recvbuf, length, mMdnsFeatureFlags));
} catch (MdnsPacket.ParseException e) {
mSharedLog.e("Error parsing mDNS packet", e);
if (DBG) {
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClient.java b/service-t/src/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClient.java
index 4ba6912..e7b0eaa 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClient.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClient.java
@@ -50,6 +50,7 @@
@NonNull private final Handler mHandler;
@NonNull private final MdnsSocketProvider mSocketProvider;
@NonNull private final SharedLog mSharedLog;
+ @NonNull private final MdnsFeatureFlags mMdnsFeatureFlags;
private final ArrayMap<MdnsServiceBrowserListener, InterfaceSocketCallback> mSocketRequests =
new ArrayMap<>();
@@ -58,11 +59,12 @@
private int mReceivedPacketNumber = 0;
public MdnsMultinetworkSocketClient(@NonNull Looper looper,
- @NonNull MdnsSocketProvider provider,
- @NonNull SharedLog sharedLog) {
+ @NonNull MdnsSocketProvider provider, @NonNull SharedLog sharedLog,
+ @NonNull MdnsFeatureFlags mdnsFeatureFlags) {
mHandler = new Handler(looper);
mSocketProvider = provider;
mSharedLog = sharedLog;
+ mMdnsFeatureFlags = mdnsFeatureFlags;
}
private class InterfaceSocketCallback implements MdnsSocketProvider.SocketCallback {
@@ -239,7 +241,7 @@
final MdnsPacket response;
try {
- response = MdnsResponseDecoder.parseResponse(recvbuf, length);
+ response = MdnsResponseDecoder.parseResponse(recvbuf, length, mMdnsFeatureFlags);
} catch (MdnsPacket.ParseException e) {
if (e.code != MdnsResponseErrorCode.ERROR_NOT_RESPONSE_MESSAGE) {
mSharedLog.e(e.getMessage(), e);
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsPacketReader.java b/service-t/src/com/android/server/connectivity/mdns/MdnsPacketReader.java
index aa38844..4917188 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsPacketReader.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsPacketReader.java
@@ -16,6 +16,7 @@
package com.android.server.connectivity.mdns;
+import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.SparseArray;
@@ -33,21 +34,23 @@
private final byte[] buf;
private final int count;
private final SparseArray<LabelEntry> labelDictionary;
+ private final MdnsFeatureFlags mMdnsFeatureFlags;
private int pos;
private int limit;
/** Constructs a reader for the given packet. */
public MdnsPacketReader(DatagramPacket packet) {
- this(packet.getData(), packet.getLength());
+ this(packet.getData(), packet.getLength(), MdnsFeatureFlags.newBuilder().build());
}
/** Constructs a reader for the given packet. */
- public MdnsPacketReader(byte[] buffer, int length) {
+ public MdnsPacketReader(byte[] buffer, int length, @NonNull MdnsFeatureFlags mdnsFeatureFlags) {
buf = buffer;
count = length;
pos = 0;
limit = -1;
labelDictionary = new SparseArray<>(16);
+ mMdnsFeatureFlags = mdnsFeatureFlags;
}
/**
@@ -269,4 +272,4 @@
this.label = label;
}
}
-}
\ No newline at end of file
+}
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsRecordRepository.java b/service-t/src/com/android/server/connectivity/mdns/MdnsRecordRepository.java
index 73c1758..e34778f 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsRecordRepository.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsRecordRepository.java
@@ -133,11 +133,6 @@
public final boolean isSharedName;
/**
- * Whether probing is still in progress for the record.
- */
- public boolean isProbing;
-
- /**
* Last time (as per SystemClock.elapsedRealtime) when advertised via multicast, 0 if never
*/
public long lastAdvertisedTimeMs;
@@ -148,12 +143,10 @@
*/
public long lastSentTimeMs;
- RecordInfo(NsdServiceInfo serviceInfo, T record, boolean sharedName,
- boolean probing) {
+ RecordInfo(NsdServiceInfo serviceInfo, T record, boolean sharedName) {
this.serviceInfo = serviceInfo;
this.record = record;
this.isSharedName = sharedName;
- this.isProbing = probing;
}
}
@@ -187,6 +180,11 @@
public int sentPacketCount = NO_PACKET;
/**
+ * Whether probing is still in progress.
+ */
+ private boolean isProbing;
+
+ /**
* Create a ServiceRegistration for dns-sd service registration (RFC6763).
*
* @param deviceHostname Hostname of the device (for the interface used)
@@ -209,7 +207,7 @@
false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS,
serviceName),
- true /* sharedName */, true /* probing */);
+ true /* sharedName */);
if (subtype == null) {
this.ptrRecords = Collections.singletonList(ptrRecord);
@@ -226,7 +224,7 @@
false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS,
serviceName),
- true /* sharedName */, true /* probing */);
+ true /* sharedName */);
this.ptrRecords = List.of(ptrRecord, subtypeRecord);
}
@@ -239,7 +237,7 @@
NAME_RECORDS_TTL_MILLIS, 0 /* servicePriority */, 0 /* serviceWeight */,
serviceInfo.getPort(),
deviceHostname),
- false /* sharedName */, true /* probing */);
+ false /* sharedName */);
txtRecord = new RecordInfo<>(
serviceInfo,
@@ -248,7 +246,7 @@
true /* cacheFlush */, // Service name is verified unique after probing
NON_NAME_RECORDS_TTL_MILLIS,
attrsToTextEntries(serviceInfo.getAttributes())),
- false /* sharedName */, true /* probing */);
+ false /* sharedName */);
final ArrayList<RecordInfo<?>> allRecords = new ArrayList<>(5);
allRecords.addAll(ptrRecords);
@@ -263,18 +261,18 @@
false /* cacheFlush */,
NON_NAME_RECORDS_TTL_MILLIS,
serviceType),
- true /* sharedName */, true /* probing */));
+ true /* sharedName */));
this.allRecords = Collections.unmodifiableList(allRecords);
this.repliedServiceCount = repliedServiceCount;
this.sentPacketCount = sentPacketCount;
+ this.isProbing = true;
}
void setProbing(boolean probing) {
- for (RecordInfo<?> info : allRecords) {
- info.isProbing = probing;
- }
+ this.isProbing = probing;
}
+
}
/**
@@ -292,7 +290,7 @@
true /* cacheFlush */,
NAME_RECORDS_TTL_MILLIS,
mDeviceHostname),
- false /* sharedName */, false /* probing */));
+ false /* sharedName */));
mGeneralRecords.add(new RecordInfo<>(
null /* serviceInfo */,
@@ -302,7 +300,7 @@
true /* cacheFlush */,
NAME_RECORDS_TTL_MILLIS,
addr.getAddress()),
- false /* sharedName */, false /* probing */));
+ false /* sharedName */));
}
}
@@ -485,7 +483,7 @@
// Add answers from each service
for (int i = 0; i < mServices.size(); i++) {
final ServiceRegistration registration = mServices.valueAt(i);
- if (registration.exiting) continue;
+ if (registration.exiting || registration.isProbing) continue;
if (addReplyFromService(question, registration.allRecords, registration.ptrRecords,
registration.srvRecord, registration.txtRecord, replyUnicast, now,
answerInfo, additionalAnswerRecords)) {
@@ -558,7 +556,6 @@
final int answersStartIndex = answerInfo.size();
for (RecordInfo<?> info : serviceRecords) {
- if (info.isProbing) continue;
/* RFC6762 6.: the record name must match the question name, the record rrtype
must match the question qtype unless the qtype is "ANY" (255) or the rrtype is
@@ -870,7 +867,7 @@
final ServiceRegistration registration = mServices.get(serviceId);
if (registration == null) return false;
- return registration.srvRecord.isProbing;
+ return registration.isProbing;
}
/**
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java b/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
index 050913f..b812bb4 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsResponseDecoder.java
@@ -84,9 +84,9 @@
* @throws MdnsPacket.ParseException if a response packet could not be parsed.
*/
@NonNull
- public static MdnsPacket parseResponse(@NonNull byte[] recvbuf, int length)
- throws MdnsPacket.ParseException {
- MdnsPacketReader reader = new MdnsPacketReader(recvbuf, length);
+ public static MdnsPacket parseResponse(@NonNull byte[] recvbuf, int length,
+ @NonNull MdnsFeatureFlags mdnsFeatureFlags) throws MdnsPacket.ParseException {
+ final MdnsPacketReader reader = new MdnsPacketReader(recvbuf, length, mdnsFeatureFlags);
final MdnsPacket mdnsPacket;
try {
diff --git a/service-t/src/com/android/server/connectivity/mdns/MdnsSocketClient.java b/service-t/src/com/android/server/connectivity/mdns/MdnsSocketClient.java
index d18a19b..82c8c5b 100644
--- a/service-t/src/com/android/server/connectivity/mdns/MdnsSocketClient.java
+++ b/service-t/src/com/android/server/connectivity/mdns/MdnsSocketClient.java
@@ -105,9 +105,10 @@
private AtomicInteger packetsCount;
@Nullable private Timer checkMulticastResponseTimer;
private final SharedLog sharedLog;
+ @NonNull private final MdnsFeatureFlags mdnsFeatureFlags;
public MdnsSocketClient(@NonNull Context context, @NonNull MulticastLock multicastLock,
- SharedLog sharedLog) {
+ SharedLog sharedLog, @NonNull MdnsFeatureFlags mdnsFeatureFlags) {
this.sharedLog = sharedLog;
this.context = context;
this.multicastLock = multicastLock;
@@ -116,6 +117,7 @@
} else {
unicastReceiverBuffer = null;
}
+ this.mdnsFeatureFlags = mdnsFeatureFlags;
}
@Override
@@ -454,7 +456,8 @@
final MdnsPacket response;
try {
- response = MdnsResponseDecoder.parseResponse(packet.getData(), packet.getLength());
+ response = MdnsResponseDecoder.parseResponse(
+ packet.getData(), packet.getLength(), mdnsFeatureFlags);
} catch (MdnsPacket.ParseException e) {
sharedLog.w(String.format("Error while decoding %s packet (%d): %d",
responseType, packetNumber, e.code));
diff --git a/service/Android.bp b/service/Android.bp
index 38c0110..82f64ba 100644
--- a/service/Android.bp
+++ b/service/Android.bp
@@ -114,7 +114,6 @@
],
srcs: [
":services.connectivity-netstats-jni-sources",
- "jni/com_android_server_BpfNetMaps.cpp",
"jni/com_android_server_connectivity_ClatCoordinator.cpp",
"jni/com_android_server_ServiceManagerWrapper.cpp",
"jni/com_android_server_TestNetworkService.cpp",
diff --git a/service/jni/com_android_server_BpfNetMaps.cpp b/service/jni/com_android_server_BpfNetMaps.cpp
deleted file mode 100644
index 29f6a60..0000000
--- a/service/jni/com_android_server_BpfNetMaps.cpp
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include "bpf/BpfUtils.h"
-
-#include <jni.h>
-#include <nativehelper/JNIHelp.h>
-
-namespace android {
-
-static jint native_synchronizeKernelRCU(JNIEnv* env, jobject self) {
- return -bpf::synchronizeKernelRCU();
-}
-
-/*
- * JNI registration.
- */
-// clang-format off
-static const JNINativeMethod gMethods[] = {
- /* name, signature, funcPtr */
- {"native_synchronizeKernelRCU", "()I",
- (void*)native_synchronizeKernelRCU},
-};
-// clang-format on
-
-int register_com_android_server_BpfNetMaps(JNIEnv* env) {
- return jniRegisterNativeMethods(env, "android/net/connectivity/com/android/server/BpfNetMaps",
- gMethods, NELEM(gMethods));
-}
-
-}; // namespace android
diff --git a/service/jni/onload.cpp b/service/jni/onload.cpp
index 5e89ad0..bb70d4f 100644
--- a/service/jni/onload.cpp
+++ b/service/jni/onload.cpp
@@ -22,7 +22,6 @@
namespace android {
int register_com_android_server_TestNetworkService(JNIEnv* env);
-int register_com_android_server_BpfNetMaps(JNIEnv* env);
int register_com_android_server_connectivity_ClatCoordinator(JNIEnv* env);
int register_android_server_net_NetworkStatsFactory(JNIEnv* env);
int register_android_server_net_NetworkStatsService(JNIEnv* env);
@@ -44,10 +43,6 @@
}
if (android::modules::sdklevel::IsAtLeastT()) {
- if (register_com_android_server_BpfNetMaps(env) < 0) {
- return JNI_ERR;
- }
-
if (register_com_android_server_connectivity_ClatCoordinator(env) < 0) {
return JNI_ERR;
}
diff --git a/service/src/com/android/server/BpfNetMaps.java b/service/src/com/android/server/BpfNetMaps.java
index b7e928d..ad9cfbe 100644
--- a/service/src/com/android/server/BpfNetMaps.java
+++ b/service/src/com/android/server/BpfNetMaps.java
@@ -106,9 +106,6 @@
// Use legacy netd for releases before T.
private static boolean sInitialized = false;
- private static final String BPF_NET_MAPS_FORCE_DISABLE_JAVA_BPF_MAP =
- "bpf_net_maps_force_disable_java_bpf_map";
-
// Lock for sConfigurationMap entry for UID_RULES_CONFIGURATION_KEY.
// This entry is not accessed by others.
// BpfNetMaps acquires this lock while sequence of read, modify, and write.
@@ -324,10 +321,16 @@
}
/**
- * Call synchronize_rcu()
+ * Synchronously call in to kernel to synchronize_rcu()
*/
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
public int synchronizeKernelRCU() {
- return native_synchronizeKernelRCU();
+ try {
+ BpfMap.synchronizeKernelRCU();
+ } catch (ErrnoException e) {
+ return -e.errno;
+ }
+ return 0;
}
/**
@@ -1061,7 +1064,4 @@
pw.decreaseIndent();
}
}
-
- @RequiresApi(Build.VERSION_CODES.TIRAMISU)
- private static native int native_synchronizeKernelRCU();
}
diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java
index 47e0bd9..7d1644e 100755
--- a/service/src/com/android/server/ConnectivityService.java
+++ b/service/src/com/android/server/ConnectivityService.java
@@ -9006,6 +9006,9 @@
return;
}
+ if (VDBG) {
+ Log.v(TAG, "Update local network config " + nai.network.netId + " : " + newConfig);
+ }
final LocalNetworkConfig.Builder configBuilder = new LocalNetworkConfig.Builder();
// TODO : apply the diff for multicast routing.
configBuilder.setUpstreamMulticastRoutingConfig(
diff --git a/staticlibs/Android.bp b/staticlibs/Android.bp
index 0bcb757..6325b46 100644
--- a/staticlibs/Android.bp
+++ b/staticlibs/Android.bp
@@ -96,12 +96,17 @@
srcs: [
"framework/**/DnsPacket.java",
"framework/**/DnsPacketUtils.java",
+ "framework/**/DnsSvcbPacket.java",
+ "framework/**/DnsSvcbRecord.java",
+ "framework/**/HexDump.java",
+ "framework/**/NetworkStackConstants.java",
],
sdk_version: "module_current",
visibility: [
"//packages/services/Iwlan:__subpackages__",
],
libs: [
+ "androidx.annotation_annotation",
"framework-annotations-lib",
"framework-connectivity.stubs.module_lib",
],
diff --git a/staticlibs/device/com/android/net/module/util/BpfMap.java b/staticlibs/device/com/android/net/module/util/BpfMap.java
index 595ac74..d622427 100644
--- a/staticlibs/device/com/android/net/module/util/BpfMap.java
+++ b/staticlibs/device/com/android/net/module/util/BpfMap.java
@@ -239,6 +239,11 @@
return Struct.parse(mValueClass, buffer);
}
+ /** Synchronize Kernel RCU */
+ public static void synchronizeKernelRCU() throws ErrnoException {
+ nativeSynchronizeKernelRCU();
+ }
+
private static native int nativeBpfFdGet(String path, int mode, int keySize, int valueSize)
throws ErrnoException, NullPointerException;
@@ -260,4 +265,6 @@
private native boolean nativeFindMapEntry(int fd, byte[] key, byte[] value)
throws ErrnoException;
+
+ private static native void nativeSynchronizeKernelRCU() throws ErrnoException;
}
diff --git a/staticlibs/framework/com/android/net/module/util/DnsPacket.java b/staticlibs/framework/com/android/net/module/util/DnsPacket.java
index 0dcdf1e..63106a1 100644
--- a/staticlibs/framework/com/android/net/module/util/DnsPacket.java
+++ b/staticlibs/framework/com/android/net/module/util/DnsPacket.java
@@ -56,6 +56,7 @@
*/
// TODO: Define the constant as a public constant in DnsResolver since it can never change.
private static final int TYPE_CNAME = 5;
+ public static final int TYPE_SVCB = 64;
/**
* Thrown when parsing packet failed.
@@ -282,7 +283,7 @@
* @param buf ByteBuffer input of record, must be in network byte order
* (which is the default).
*/
- private DnsRecord(@RecordType int rType, @NonNull ByteBuffer buf)
+ protected DnsRecord(@RecordType int rType, @NonNull ByteBuffer buf)
throws BufferUnderflowException, ParseException {
Objects.requireNonNull(buf);
this.rType = rType;
@@ -326,6 +327,8 @@
// Return a DnsRecord instance by default for backward compatibility, this is useful
// when a partner supports new type of DnsRecord but does not inherit DnsRecord.
switch (nsType) {
+ case TYPE_SVCB:
+ return new DnsSvcbRecord(rType, buf);
default:
return new DnsRecord(rType, buf);
}
diff --git a/staticlibs/framework/com/android/net/module/util/DnsSvcbPacket.java b/staticlibs/framework/com/android/net/module/util/DnsSvcbPacket.java
new file mode 100644
index 0000000..d298599
--- /dev/null
+++ b/staticlibs/framework/com/android/net/module/util/DnsSvcbPacket.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.net.module.util;
+
+import static android.net.DnsResolver.TYPE_A;
+import static android.net.DnsResolver.TYPE_AAAA;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.util.Log;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A class for a DNS SVCB response packet.
+ *
+ * @hide
+ */
+public class DnsSvcbPacket extends DnsPacket {
+ public static final int TYPE_SVCB = 64;
+
+ private static final String TAG = DnsSvcbPacket.class.getSimpleName();
+
+ /**
+ * Creates a DnsSvcbPacket object from the given wire-format DNS packet.
+ */
+ private DnsSvcbPacket(@NonNull byte[] data) throws DnsPacket.ParseException {
+ // If data is null, ParseException will be thrown.
+ super(data);
+
+ final int questions = mHeader.getRecordCount(QDSECTION);
+ if (questions != 1) {
+ throw new DnsPacket.ParseException("Unexpected question count " + questions);
+ }
+ final int nsType = mRecords[QDSECTION].get(0).nsType;
+ if (nsType != TYPE_SVCB) {
+ throw new DnsPacket.ParseException("Unexpected query type " + nsType);
+ }
+ }
+
+ /**
+ * Returns true if the DnsSvcbPacket is a DNS response.
+ */
+ public boolean isResponse() {
+ return mHeader.isResponse();
+ }
+
+ /**
+ * Returns whether the given protocol alpn is supported.
+ */
+ public boolean isSupported(@NonNull String alpn) {
+ return findSvcbRecord(alpn) != null;
+ }
+
+ /**
+ * Returns the TargetName associated with the given protocol alpn.
+ * If the alpn is not supported, a null is returned.
+ */
+ @Nullable
+ public String getTargetName(@NonNull String alpn) {
+ final DnsSvcbRecord record = findSvcbRecord(alpn);
+ return (record != null) ? record.getTargetName() : null;
+ }
+
+ /**
+ * Returns the TargetName that associated with the given protocol alpn.
+ * If the alpn is not supported, -1 is returned.
+ */
+ public int getPort(@NonNull String alpn) {
+ final DnsSvcbRecord record = findSvcbRecord(alpn);
+ return (record != null) ? record.getPort() : -1;
+ }
+
+ /**
+ * Returns the IP addresses that support the given protocol alpn.
+ * If the alpn is not supported, an empty list is returned.
+ */
+ @NonNull
+ public List<InetAddress> getAddresses(@NonNull String alpn) {
+ final DnsSvcbRecord record = findSvcbRecord(alpn);
+ if (record == null) return Collections.EMPTY_LIST;
+
+ // As per draft-ietf-dnsop-svcb-https-10#section-7.4 and draft-ietf-add-ddr-10#section-4,
+ // if A/AAAA records are available in the Additional section, use the IP addresses in
+ // those records instead of the IP addresses in ipv4hint/ipv6hint.
+ final List<InetAddress> out = getAddressesFromAdditionalSection();
+ if (out.size() > 0) return out;
+
+ return record.getAddresses();
+ }
+
+ /**
+ * Returns the value of SVCB key dohpath that associated with the given protocol alpn.
+ * If the alpn is not supported, a null is returned.
+ */
+ @Nullable
+ public String getDohPath(@NonNull String alpn) {
+ final DnsSvcbRecord record = findSvcbRecord(alpn);
+ return (record != null) ? record.getDohPath() : null;
+ }
+
+ /**
+ * Returns the DnsSvcbRecord associated with the given protocol alpn.
+ * If the alpn is not supported, a null is returned.
+ */
+ @Nullable
+ private DnsSvcbRecord findSvcbRecord(@NonNull String alpn) {
+ for (final DnsRecord record : mRecords[ANSECTION]) {
+ if (record instanceof DnsSvcbRecord) {
+ final DnsSvcbRecord svcbRecord = (DnsSvcbRecord) record;
+ if (svcbRecord.getAlpns().contains(alpn)) {
+ return svcbRecord;
+ }
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns the IP addresses in additional section.
+ */
+ @NonNull
+ private List<InetAddress> getAddressesFromAdditionalSection() {
+ final List<InetAddress> out = new ArrayList<InetAddress>();
+ if (mHeader.getRecordCount(ARSECTION) == 0) {
+ return out;
+ }
+ for (final DnsRecord record : mRecords[ARSECTION]) {
+ if (record.nsType != TYPE_A && record.nsType != TYPE_AAAA) {
+ Log.d(TAG, "Found type other than A/AAAA in Additional section: " + record.nsType);
+ continue;
+ }
+ try {
+ out.add(InetAddress.getByAddress(record.getRR()));
+ } catch (UnknownHostException e) {
+ Log.w(TAG, "Failed to parse address");
+ }
+ }
+ return out;
+ }
+
+ /**
+ * Creates a DnsSvcbPacket object from the given wire-format DNS answer.
+ */
+ public static DnsSvcbPacket fromResponse(@NonNull byte[] data) throws DnsPacket.ParseException {
+ DnsSvcbPacket out = new DnsSvcbPacket(data);
+ if (!out.isResponse()) {
+ throw new DnsPacket.ParseException("Not an answer packet");
+ }
+ return out;
+ }
+}
diff --git a/staticlibs/framework/com/android/net/module/util/DnsSvcbRecord.java b/staticlibs/framework/com/android/net/module/util/DnsSvcbRecord.java
new file mode 100644
index 0000000..935cdf6
--- /dev/null
+++ b/staticlibs/framework/com/android/net/module/util/DnsSvcbRecord.java
@@ -0,0 +1,539 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.net.module.util;
+
+import static android.net.DnsResolver.CLASS_IN;
+
+import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
+import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
+import static com.android.net.module.util.DnsPacket.ParseException;
+
+import android.annotation.NonNull;
+import android.text.TextUtils;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.nio.ShortBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.StringJoiner;
+
+/**
+ * A class for an SVCB record.
+ * https://www.iana.org/assignments/dns-svcb/dns-svcb.xhtml
+ * @hide
+ */
+@VisibleForTesting(visibility = PACKAGE)
+public final class DnsSvcbRecord extends DnsPacket.DnsRecord {
+ /**
+ * The following SvcParamKeys KEY_* are defined in
+ * https://www.iana.org/assignments/dns-svcb/dns-svcb.xhtml.
+ */
+
+ // The SvcParamKey "mandatory". The associated implementation of SvcParam is SvcParamMandatory.
+ private static final int KEY_MANDATORY = 0;
+
+ // The SvcParamKey "alpn". The associated implementation of SvcParam is SvcParamAlpn.
+ private static final int KEY_ALPN = 1;
+
+ // The SvcParamKey "no-default-alpn". The associated implementation of SvcParam is
+ // SvcParamNoDefaultAlpn.
+ private static final int KEY_NO_DEFAULT_ALPN = 2;
+
+ // The SvcParamKey "port". The associated implementation of SvcParam is SvcParamPort.
+ private static final int KEY_PORT = 3;
+
+ // The SvcParamKey "ipv4hint". The associated implementation of SvcParam is SvcParamIpv4Hint.
+ private static final int KEY_IPV4HINT = 4;
+
+ // The SvcParamKey "ech". The associated implementation of SvcParam is SvcParamEch.
+ private static final int KEY_ECH = 5;
+
+ // The SvcParamKey "ipv6hint". The associated implementation of SvcParam is SvcParamIpv6Hint.
+ private static final int KEY_IPV6HINT = 6;
+
+ // The SvcParamKey "dohpath". The associated implementation of SvcParam is SvcParamDohPath.
+ private static final int KEY_DOHPATH = 7;
+
+ // The minimal size of a SvcParam.
+ // https://www.ietf.org/archive/id/draft-ietf-dnsop-svcb-https-12.html#name-rdata-wire-format
+ private static final int MINSVCPARAMSIZE = 4;
+
+ private static final String TAG = DnsSvcbRecord.class.getSimpleName();
+
+ private final int mSvcPriority;
+
+ @NonNull
+ private final String mTargetName;
+
+ @NonNull
+ private final SparseArray<SvcParam> mAllSvcParams = new SparseArray<>();
+
+ @VisibleForTesting(visibility = PACKAGE)
+ public DnsSvcbRecord(@DnsPacket.RecordType int rType, @NonNull ByteBuffer buff)
+ throws IllegalStateException, ParseException {
+ super(rType, buff);
+ if (nsType != DnsPacket.TYPE_SVCB) {
+ throw new IllegalStateException("incorrect nsType: " + nsType);
+ }
+ if (nsClass != CLASS_IN) {
+ throw new ParseException("incorrect nsClass: " + nsClass);
+ }
+
+ // DNS Record in Question Section doesn't have Rdata.
+ if (rType == DnsPacket.QDSECTION) {
+ mSvcPriority = 0;
+ mTargetName = "";
+ return;
+ }
+
+ final byte[] rdata = getRR();
+ if (rdata == null) {
+ throw new ParseException("SVCB rdata is empty");
+ }
+
+ final ByteBuffer buf = ByteBuffer.wrap(rdata).asReadOnlyBuffer();
+ mSvcPriority = Short.toUnsignedInt(buf.getShort());
+ mTargetName = DnsPacketUtils.DnsRecordParser.parseName(buf, 0 /* Parse depth */,
+ false /* isNameCompressionSupported */);
+
+ if (mTargetName.length() > DnsPacket.DnsRecord.MAXNAMESIZE) {
+ throw new ParseException(
+ "Failed to parse SVCB target name, name size is too long: "
+ + mTargetName.length());
+ }
+ while (buf.remaining() >= MINSVCPARAMSIZE) {
+ final SvcParam svcParam = parseSvcParam(buf);
+ final int key = svcParam.getKey();
+ if (mAllSvcParams.get(key) != null) {
+ throw new ParseException("Invalid DnsSvcbRecord, key " + key + " is repeated");
+ }
+ mAllSvcParams.put(key, svcParam);
+ }
+ if (buf.hasRemaining()) {
+ throw new ParseException("Invalid DnsSvcbRecord. Got "
+ + buf.remaining() + " remaining bytes after parsing");
+ }
+ }
+
+ /**
+ * Returns the TargetName.
+ */
+ @VisibleForTesting(visibility = PACKAGE)
+ @NonNull
+ public String getTargetName() {
+ return mTargetName;
+ }
+
+ /**
+ * Returns an unmodifiable list of alpns from SvcParam alpn.
+ */
+ @VisibleForTesting(visibility = PACKAGE)
+ @NonNull
+ public List<String> getAlpns() {
+ final SvcParamAlpn sp = (SvcParamAlpn) mAllSvcParams.get(KEY_ALPN);
+ final List<String> list = (sp != null) ? sp.getValue() : Collections.EMPTY_LIST;
+ return Collections.unmodifiableList(list);
+ }
+
+ /**
+ * Returns the port number from SvcParam port.
+ */
+ @VisibleForTesting(visibility = PACKAGE)
+ public int getPort() {
+ final SvcParamPort sp = (SvcParamPort) mAllSvcParams.get(KEY_PORT);
+ return (sp != null) ? sp.getValue() : -1;
+ }
+
+ /**
+ * Returns a list of the IP addresses from both of SvcParam ipv4hint and ipv6hint.
+ */
+ @VisibleForTesting(visibility = PACKAGE)
+ @NonNull
+ public List<InetAddress> getAddresses() {
+ final List<InetAddress> out = new ArrayList<>();
+ final SvcParamIpHint sp4 = (SvcParamIpHint) mAllSvcParams.get(KEY_IPV4HINT);
+ if (sp4 != null) {
+ out.addAll(sp4.getValue());
+ }
+ final SvcParamIpHint sp6 = (SvcParamIpHint) mAllSvcParams.get(KEY_IPV6HINT);
+ if (sp6 != null) {
+ out.addAll(sp6.getValue());
+ }
+ return out;
+ }
+
+ /**
+ * Returns the doh path from SvcParam dohPath.
+ */
+ @VisibleForTesting(visibility = PACKAGE)
+ @NonNull
+ public String getDohPath() {
+ final SvcParamDohPath sp = (SvcParamDohPath) mAllSvcParams.get(KEY_DOHPATH);
+ return (sp != null) ? sp.getValue() : "";
+ }
+
+ @Override
+ public String toString() {
+ if (rType == DnsPacket.QDSECTION) {
+ return dName + " IN SVCB";
+ }
+
+ final StringJoiner sj = new StringJoiner(" ");
+ for (int i = 0; i < mAllSvcParams.size(); i++) {
+ sj.add(mAllSvcParams.valueAt(i).toString());
+ }
+ return dName + " " + ttl + " IN SVCB " + mSvcPriority + " " + mTargetName + " "
+ + sj.toString();
+ }
+
+ private static SvcParam parseSvcParam(@NonNull ByteBuffer buf) throws ParseException {
+ try {
+ final int key = Short.toUnsignedInt(buf.getShort());
+ switch (key) {
+ case KEY_MANDATORY: return new SvcParamMandatory(buf);
+ case KEY_ALPN: return new SvcParamAlpn(buf);
+ case KEY_NO_DEFAULT_ALPN: return new SvcParamNoDefaultAlpn(buf);
+ case KEY_PORT: return new SvcParamPort(buf);
+ case KEY_IPV4HINT: return new SvcParamIpv4Hint(buf);
+ case KEY_ECH: return new SvcParamEch(buf);
+ case KEY_IPV6HINT: return new SvcParamIpv6Hint(buf);
+ case KEY_DOHPATH: return new SvcParamDohPath(buf);
+ default: return new SvcParamGeneric(key, buf);
+ }
+ } catch (BufferUnderflowException e) {
+ throw new ParseException("Malformed packet", e);
+ }
+ }
+
+ /**
+ * The base class for all SvcParam.
+ */
+ private abstract static class SvcParam<T> {
+ private final int mKey;
+
+ SvcParam(int key) {
+ mKey = key;
+ }
+
+ int getKey() {
+ return mKey;
+ }
+
+ abstract T getValue();
+ }
+
+ private static class SvcParamMandatory extends SvcParam<short[]> {
+ private final short[] mValue;
+
+ private SvcParamMandatory(@NonNull ByteBuffer buf) throws BufferUnderflowException,
+ ParseException {
+ super(KEY_MANDATORY);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = Short.toUnsignedInt(buf.getShort());
+ final ByteBuffer svcParamValue = sliceAndAdvance(buf, len);
+ mValue = SvcParamValueUtil.toShortArray(svcParamValue);
+ if (mValue.length == 0) {
+ throw new ParseException("mandatory value must be non-empty");
+ }
+ }
+
+ @Override
+ short[] getValue() {
+ /* Not yet implemented */
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ final StringJoiner valueJoiner = new StringJoiner(",");
+ for (short key : mValue) {
+ valueJoiner.add(toKeyName(key));
+ }
+ return toKeyName(getKey()) + "=" + valueJoiner.toString();
+ }
+ }
+
+ private static class SvcParamAlpn extends SvcParam<List<String>> {
+ private final List<String> mValue;
+
+ SvcParamAlpn(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_ALPN);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = Short.toUnsignedInt(buf.getShort());
+ final ByteBuffer svcParamValue = sliceAndAdvance(buf, len);
+ mValue = SvcParamValueUtil.toStringList(svcParamValue);
+ if (mValue.isEmpty()) {
+ throw new ParseException("alpn value must be non-empty");
+ }
+ }
+
+ @Override
+ List<String> getValue() {
+ return Collections.unmodifiableList(mValue);
+ }
+
+ @Override
+ public String toString() {
+ return toKeyName(getKey()) + "=" + TextUtils.join(",", mValue);
+ }
+ }
+
+ private static class SvcParamNoDefaultAlpn extends SvcParam<Void> {
+ SvcParamNoDefaultAlpn(@NonNull ByteBuffer buf) throws BufferUnderflowException,
+ ParseException {
+ super(KEY_NO_DEFAULT_ALPN);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = buf.getShort();
+ if (len != 0) {
+ throw new ParseException("no-default-alpn value must be empty");
+ }
+ }
+
+ @Override
+ Void getValue() {
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ return toKeyName(getKey());
+ }
+ }
+
+ private static class SvcParamPort extends SvcParam<Integer> {
+ private final int mValue;
+
+ SvcParamPort(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_PORT);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = buf.getShort();
+ if (len != Short.BYTES) {
+ throw new ParseException("key port len is not 2 but " + len);
+ }
+ mValue = Short.toUnsignedInt(buf.getShort());
+ }
+
+ @Override
+ Integer getValue() {
+ return mValue;
+ }
+
+ @Override
+ public String toString() {
+ return toKeyName(getKey()) + "=" + mValue;
+ }
+ }
+
+ private static class SvcParamIpHint extends SvcParam<List<InetAddress>> {
+ private final List<InetAddress> mValue;
+
+ private SvcParamIpHint(int key, @NonNull ByteBuffer buf, int addrLen) throws
+ BufferUnderflowException, ParseException {
+ super(key);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = Short.toUnsignedInt(buf.getShort());
+ final ByteBuffer svcParamValue = sliceAndAdvance(buf, len);
+ mValue = SvcParamValueUtil.toInetAddressList(svcParamValue, addrLen);
+ if (mValue.isEmpty()) {
+ throw new ParseException(toKeyName(getKey()) + " value must be non-empty");
+ }
+ }
+
+ @Override
+ List<InetAddress> getValue() {
+ return Collections.unmodifiableList(mValue);
+ }
+
+ @Override
+ public String toString() {
+ final StringJoiner valueJoiner = new StringJoiner(",");
+ for (InetAddress ip : mValue) {
+ valueJoiner.add(ip.getHostAddress());
+ }
+ return toKeyName(getKey()) + "=" + valueJoiner.toString();
+ }
+ }
+
+ private static class SvcParamIpv4Hint extends SvcParamIpHint {
+ SvcParamIpv4Hint(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_IPV4HINT, buf, NetworkStackConstants.IPV4_ADDR_LEN);
+ }
+ }
+
+ private static class SvcParamIpv6Hint extends SvcParamIpHint {
+ SvcParamIpv6Hint(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_IPV6HINT, buf, NetworkStackConstants.IPV6_ADDR_LEN);
+ }
+ }
+
+ private static class SvcParamEch extends SvcParamGeneric {
+ SvcParamEch(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_ECH, buf);
+ }
+ }
+
+ private static class SvcParamDohPath extends SvcParam<String> {
+ private final String mValue;
+
+ SvcParamDohPath(@NonNull ByteBuffer buf) throws BufferUnderflowException, ParseException {
+ super(KEY_DOHPATH);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = Short.toUnsignedInt(buf.getShort());
+ final byte[] value = new byte[len];
+ buf.get(value);
+ mValue = new String(value, StandardCharsets.UTF_8);
+ }
+
+ @Override
+ String getValue() {
+ return mValue;
+ }
+
+ @Override
+ public String toString() {
+ return toKeyName(getKey()) + "=" + mValue;
+ }
+ }
+
+ // For other unrecognized and unimplemented SvcParams, they are stored as SvcParamGeneric.
+ private static class SvcParamGeneric extends SvcParam<byte[]> {
+ private final byte[] mValue;
+
+ SvcParamGeneric(int key, @NonNull ByteBuffer buf) throws BufferUnderflowException,
+ ParseException {
+ super(key);
+ // The caller already read 2 bytes for SvcParamKey.
+ final int len = Short.toUnsignedInt(buf.getShort());
+ mValue = new byte[len];
+ buf.get(mValue);
+ }
+
+ @Override
+ byte[] getValue() {
+ /* Not yet implemented */
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder out = new StringBuilder();
+ out.append(toKeyName(getKey()));
+ if (mValue != null && mValue.length > 0) {
+ out.append("=");
+ out.append(HexDump.toHexString(mValue));
+ }
+ return out.toString();
+ }
+ }
+
+ private static String toKeyName(int key) {
+ switch (key) {
+ case KEY_MANDATORY: return "mandatory";
+ case KEY_ALPN: return "alpn";
+ case KEY_NO_DEFAULT_ALPN: return "no-default-alpn";
+ case KEY_PORT: return "port";
+ case KEY_IPV4HINT: return "ipv4hint";
+ case KEY_ECH: return "ech";
+ case KEY_IPV6HINT: return "ipv6hint";
+ case KEY_DOHPATH: return "dohpath";
+ default: return "key" + key;
+ }
+ }
+
+ /**
+ * Returns a read-only ByteBuffer (with position = 0, limit = `length`, and capacity = `length`)
+ * sliced from `buf`'s current position, and moves the position of `buf` by `length`.
+ */
+ @VisibleForTesting(visibility = PRIVATE)
+ public static ByteBuffer sliceAndAdvance(@NonNull ByteBuffer buf, int length)
+ throws BufferUnderflowException {
+ if (buf.remaining() < length) {
+ throw new BufferUnderflowException();
+ }
+ final int pos = buf.position();
+
+ // `out` equals to `buf.slice(pos, length)` that is supported in API level 34.
+ final ByteBuffer out = ((ByteBuffer) buf.slice().limit(length)).slice();
+
+ buf.position(pos + length);
+ return out.asReadOnlyBuffer();
+ }
+
+ // A utility to convert the byte array of SvcParamValue to other types.
+ private static class SvcParamValueUtil {
+ // Refer to draft-ietf-dnsop-svcb-https-10#section-7.1 for the wire format of alpn.
+ @NonNull
+ private static List<String> toStringList(@NonNull ByteBuffer buf)
+ throws BufferUnderflowException, ParseException {
+ final List<String> out = new ArrayList<>();
+ while (buf.hasRemaining()) {
+ final int alpnLen = Byte.toUnsignedInt(buf.get());
+ if (alpnLen == 0) {
+ throw new ParseException("alpn should not be an empty string");
+ }
+ final byte[] alpn = new byte[alpnLen];
+ buf.get(alpn);
+ out.add(new String(alpn, StandardCharsets.UTF_8));
+ }
+ return out;
+ }
+
+ // Refer to draft-ietf-dnsop-svcb-https-10#section-7.5 for the wire format of SvcParamKey
+ // "mandatory".
+ @NonNull
+ private static short[] toShortArray(@NonNull ByteBuffer buf)
+ throws BufferUnderflowException, ParseException {
+ if (buf.remaining() % Short.BYTES != 0) {
+ throw new ParseException("Can't parse whole byte array");
+ }
+ final ShortBuffer sb = buf.asShortBuffer();
+ final short[] out = new short[sb.remaining()];
+ sb.get(out);
+ return out;
+ }
+
+ // Refer to draft-ietf-dnsop-svcb-https-10#section-7.4 for the wire format of ipv4hint and
+ // ipv6hint.
+ @NonNull
+ private static List<InetAddress> toInetAddressList(@NonNull ByteBuffer buf, int addrLen)
+ throws BufferUnderflowException, ParseException {
+ if (buf.remaining() % addrLen != 0) {
+ throw new ParseException("Can't parse whole byte array");
+ }
+
+ final List<InetAddress> out = new ArrayList<>();
+ final byte[] addr = new byte[addrLen];
+ while (buf.remaining() >= addrLen) {
+ buf.get(addr);
+ try {
+ out.add(InetAddress.getByAddress(addr));
+ } catch (UnknownHostException e) {
+ throw new ParseException("Can't parse byte array as an IP address");
+ }
+ }
+ return out;
+ }
+ }
+}
diff --git a/staticlibs/native/bpf_headers/BpfRingbufTest.cpp b/staticlibs/native/bpf_headers/BpfRingbufTest.cpp
index 6c0841c..e4de812 100644
--- a/staticlibs/native/bpf_headers/BpfRingbufTest.cpp
+++ b/staticlibs/native/bpf_headers/BpfRingbufTest.cpp
@@ -72,12 +72,15 @@
auto result = BpfRingbuf<uint64_t>::Create(mRingbufPath.c_str());
ASSERT_RESULT_OK(result);
+ EXPECT_TRUE(result.value()->isEmpty());
for (int i = 0; i < n; i++) {
RunProgram();
}
+ EXPECT_FALSE(result.value()->isEmpty());
EXPECT_THAT(result.value()->ConsumeAll(callback), HasValue(n));
+ EXPECT_TRUE(result.value()->isEmpty());
EXPECT_EQ(output, TEST_RINGBUF_MAGIC_NUM);
EXPECT_EQ(run_count, n);
}
diff --git a/staticlibs/native/bpf_headers/include/bpf/BpfMap.h b/staticlibs/native/bpf_headers/include/bpf/BpfMap.h
index 5d7eb0d..3fede3c 100644
--- a/staticlibs/native/bpf_headers/include/bpf/BpfMap.h
+++ b/staticlibs/native/bpf_headers/include/bpf/BpfMap.h
@@ -78,7 +78,7 @@
Result<Key> getFirstKey() const {
Key firstKey;
if (getFirstMapKey(mMapFd, &firstKey)) {
- return ErrnoErrorf("Get firstKey map {} failed", mMapFd.get());
+ return ErrnoErrorf("BpfMap::getFirstKey() failed");
}
return firstKey;
}
@@ -86,7 +86,7 @@
Result<Key> getNextKey(const Key& key) const {
Key nextKey;
if (getNextMapKey(mMapFd, &key, &nextKey)) {
- return ErrnoErrorf("Get next key of map {} failed", mMapFd.get());
+ return ErrnoErrorf("BpfMap::getNextKey() failed");
}
return nextKey;
}
@@ -94,7 +94,7 @@
Result<Value> readValue(const Key key) const {
Value value;
if (findMapEntry(mMapFd, &key, &value)) {
- return ErrnoErrorf("Read value of map {} failed", mMapFd.get());
+ return ErrnoErrorf("BpfMap::readValue() failed");
}
return value;
}
@@ -243,14 +243,14 @@
Result<void> writeValue(const Key& key, const Value& value, uint64_t flags) {
if (writeToMapEntry(mMapFd, &key, &value, flags)) {
- return ErrnoErrorf("Write to map {} failed", mMapFd.get());
+ return ErrnoErrorf("BpfMap::writeValue() failed");
}
return {};
}
Result<void> deleteValue(const Key& key) {
if (deleteMapEntry(mMapFd, &key)) {
- return ErrnoErrorf("Delete entry from map {} failed", mMapFd.get());
+ return ErrnoErrorf("BpfMap::deleteValue() failed");
}
return {};
}
@@ -280,7 +280,7 @@
if (map_flags & BPF_F_RDONLY) abort();
mMapFd.reset(createMap(map_type, sizeof(Key), sizeof(Value), max_entries,
map_flags));
- if (!mMapFd.ok()) return ErrnoErrorf("Unable to create map.");
+ if (!mMapFd.ok()) return ErrnoErrorf("BpfMap::resetMap() failed");
abortOnMismatch(/* writable */ true);
return {};
}
diff --git a/staticlibs/native/bpf_headers/include/bpf/BpfRingbuf.h b/staticlibs/native/bpf_headers/include/bpf/BpfRingbuf.h
index dd1504c..9aff790 100644
--- a/staticlibs/native/bpf_headers/include/bpf/BpfRingbuf.h
+++ b/staticlibs/native/bpf_headers/include/bpf/BpfRingbuf.h
@@ -39,6 +39,8 @@
mProducerPos = nullptr;
}
+ bool isEmpty(void);
+
protected:
// Non-initializing constructor, used by Create.
BpfRingbufBase(size_t value_size) : mValueSize(value_size) {}
@@ -197,6 +199,13 @@
return {};
}
+inline bool BpfRingbufBase::isEmpty(void) {
+ uint32_t prod_pos = mProducerPos->load(std::memory_order_acquire);
+ // Only userspace writes to mConsumerPos, so no need to use std::memory_order_acquire
+ uint64_t cons_pos = mConsumerPos->load(std::memory_order_relaxed);
+ return (cons_pos & 0xFFFFFFFF) == prod_pos;
+}
+
inline base::Result<int> BpfRingbufBase::ConsumeAll(
const std::function<void(const void*)>& callback) {
int64_t count = 0;
diff --git a/staticlibs/native/bpfmapjni/com_android_net_module_util_BpfMap.cpp b/staticlibs/native/bpfmapjni/com_android_net_module_util_BpfMap.cpp
index f93d6e1..b92f107 100644
--- a/staticlibs/native/bpfmapjni/com_android_net_module_util_BpfMap.cpp
+++ b/staticlibs/native/bpfmapjni/com_android_net_module_util_BpfMap.cpp
@@ -15,6 +15,8 @@
*/
#include <errno.h>
+#include <linux/pfkeyv2.h>
+#include <sys/socket.h>
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedLocalRef.h>
@@ -117,6 +119,22 @@
return throwIfNotEnoent(env, "nativeFindMapEntry", ret, errno);
}
+static void com_android_net_module_util_BpfMap_nativeSynchronizeKernelRCU(JNIEnv *env,
+ jclass clazz) {
+ const int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
+
+ if (pfSocket < 0) {
+ jniThrowErrnoException(env, "nativeSynchronizeKernelRCU:socket", errno);
+ return;
+ }
+
+ if (close(pfSocket)) {
+ jniThrowErrnoException(env, "nativeSynchronizeKernelRCU:close", errno);
+ return;
+ }
+ return;
+}
+
/*
* JNI registration.
*/
@@ -132,6 +150,8 @@
(void*) com_android_net_module_util_BpfMap_nativeGetNextMapKey },
{ "nativeFindMapEntry", "(I[B[B)Z",
(void*) com_android_net_module_util_BpfMap_nativeFindMapEntry },
+ { "nativeSynchronizeKernelRCU", "()V",
+ (void*) com_android_net_module_util_BpfMap_nativeSynchronizeKernelRCU },
};
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/DnsPacketTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/DnsPacketTest.java
index 28e183a..88d9e1e 100644
--- a/staticlibs/tests/unit/src/com/android/net/module/util/DnsPacketTest.java
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/DnsPacketTest.java
@@ -203,6 +203,30 @@
"test.com", CLASS_IN, 0 /* ttl */, "example.com"));
}
+ /** Verifies that the type of implementation returned from DnsRecord#parse is correct */
+ @Test
+ public void testDnsRecordParse() throws IOException {
+ final byte[] svcbQuestionRecord = new byte[] {
+ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00, /* Name */
+ 0x00, 0x40, /* Type */
+ 0x00, 0x01, /* Class */
+ };
+ assertTrue(DnsPacket.DnsRecord.parse(DnsPacket.QDSECTION,
+ ByteBuffer.wrap(svcbQuestionRecord)) instanceof DnsSvcbRecord);
+
+ final byte[] svcbAnswerRecord = new byte[] {
+ 0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00, /* Name */
+ 0x00, 0x40, /* Type */
+ 0x00, 0x01, /* Class */
+ 0x00, 0x00, 0x01, 0x2b, /* TTL */
+ 0x00, 0x0b, /* Data length */
+ 0x00, 0x01, /* SvcPriority */
+ 0x03, 'd', 'o', 't', 0x03, 'c', 'o', 'm', 0x00, /* TargetName */
+ };
+ assertTrue(DnsPacket.DnsRecord.parse(DnsPacket.ANSECTION,
+ ByteBuffer.wrap(svcbAnswerRecord)) instanceof DnsSvcbRecord);
+ }
+
/**
* Verifies ttl/rData error handling when parsing
* {@link DnsPacket.DnsRecord} from bytes.
diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/DnsSvcbPacketTest.java b/staticlibs/tests/unit/src/com/android/net/module/util/DnsSvcbPacketTest.java
new file mode 100644
index 0000000..d59795f
--- /dev/null
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/DnsSvcbPacketTest.java
@@ -0,0 +1,608 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.net.module.util;
+
+import static android.net.DnsResolver.CLASS_IN;
+import static android.net.DnsResolver.TYPE_A;
+import static android.net.DnsResolver.TYPE_AAAA;
+
+import static com.android.net.module.util.DnsPacket.TYPE_SVCB;
+import static com.android.testutils.MiscAsserts.assertThrows;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.annotation.NonNull;
+import android.net.InetAddresses;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+@RunWith(JUnit4.class)
+public class DnsSvcbPacketTest {
+ private static final short TEST_TRANSACTION_ID = 0x4321;
+ private static final byte[] TEST_DNS_RESPONSE_HEADER_FLAG = new byte[] { (byte) 0x81, 0x00 };
+
+ // A common DNS SVCB Question section with Name = "_dns.resolver.arpa".
+ private static final byte[] TEST_DNS_SVCB_QUESTION_SECTION = new byte[] {
+ 0x04, '_', 'd', 'n', 's', 0x08, 'r', 'e', 's', 'o', 'l', 'v', 'e', 'r',
+ 0x04, 'a', 'r', 'p', 'a', 0x00, 0x00, 0x40, 0x00, 0x01,
+ };
+
+ // mandatory=ipv4hint,alpn,key333
+ private static final byte[] TEST_SVC_PARAM_MANDATORY = new byte[] {
+ 0x00, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x01, 0x01, 0x4d,
+ };
+
+ // alpn=doq
+ private static final byte[] TEST_SVC_PARAM_ALPN_DOQ = new byte[] {
+ 0x00, 0x01, 0x00, 0x04, 0x03, 'd', 'o', 'q'
+ };
+
+ // alpn=h2,http/1.1
+ private static final byte[] TEST_SVC_PARAM_ALPN_HTTPS = new byte[] {
+ 0x00, 0x01, 0x00, 0x0c, 0x02, 'h', '2',
+ 0x08, 'h', 't', 't', 'p', '/', '1', '.', '1',
+ };
+
+ // no-default-alpn
+ private static final byte[] TEST_SVC_PARAM_NO_DEFAULT_ALPN = new byte[] {
+ 0x00, 0x02, 0x00, 0x00,
+ };
+
+ // port=5353
+ private static final byte[] TEST_SVC_PARAM_PORT = new byte[] {
+ 0x00, 0x03, 0x00, 0x02, 0x14, (byte) 0xe9,
+ };
+
+ // ipv4hint=1.2.3.4,6.7.8.9
+ private static final byte[] TEST_SVC_PARAM_IPV4HINT_1 = new byte[] {
+ 0x00, 0x04, 0x00, 0x08, 0x01, 0x02, 0x03, 0x04, 0x06, 0x07, 0x08, 0x09,
+ };
+
+ // ipv4hint=4.3.2.1
+ private static final byte[] TEST_SVC_PARAM_IPV4HINT_2 = new byte[] {
+ 0x00, 0x04, 0x00, 0x04, 0x04, 0x03, 0x02, 0x01,
+ };
+
+ // ech=aBcDe
+ private static final byte[] TEST_SVC_PARAM_ECH = new byte[] {
+ 0x00, 0x05, 0x00, 0x05, 'a', 'B', 'c', 'D', 'e',
+ };
+
+ // ipv6hint=2001:db8::1
+ private static final byte[] TEST_SVC_PARAM_IPV6HINT = new byte[] {
+ 0x00, 0x06, 0x00, 0x10, 0x20, 0x01, 0x0d, (byte) 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+ };
+
+ // dohpath=/some-path{?dns}
+ private static final byte[] TEST_SVC_PARAM_DOHPATH = new byte[] {
+ 0x00, 0x07, 0x00, 0x10,
+ '/', 's', 'o', 'm', 'e', '-', 'p', 'a', 't', 'h', '{', '?', 'd', 'n', 's', '}',
+ };
+
+ // key12345=1A2B0C
+ private static final byte[] TEST_SVC_PARAM_GENERIC_WITH_VALUE = new byte[] {
+ 0x30, 0x39, 0x00, 0x03, 0x1a, 0x2b, 0x0c,
+ };
+
+ // key12346
+ private static final byte[] TEST_SVC_PARAM_GENERIC_WITHOUT_VALUE = new byte[] {
+ 0x30, 0x3a, 0x00, 0x00,
+ };
+
+ private static byte[] makeDnsResponseHeaderAsByteArray(int qdcount, int ancount, int nscount,
+ int arcount) {
+ final ByteBuffer buffer = ByteBuffer.wrap(new byte[12]);
+ buffer.putShort(TEST_TRANSACTION_ID); /* Transaction ID */
+ buffer.put(TEST_DNS_RESPONSE_HEADER_FLAG); /* Flags */
+ buffer.putShort((short) qdcount);
+ buffer.putShort((short) ancount);
+ buffer.putShort((short) nscount);
+ buffer.putShort((short) arcount);
+ return buffer.array();
+ }
+
+ private static DnsSvcbRecord makeDnsSvcbRecordFromByteArray(@NonNull byte[] data)
+ throws IOException {
+ return new DnsSvcbRecord(DnsPacket.ANSECTION, ByteBuffer.wrap(data));
+ }
+
+ private static DnsSvcbRecord makeDnsSvcbRecordWithSingleSvcParam(@NonNull byte[] svcParam)
+ throws IOException {
+ return makeDnsSvcbRecordFromByteArray(new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .setTargetName("test.com")
+ .addRdata(svcParam)
+ .build());
+ }
+
+ // Converts a Short to a byte array in big endian.
+ private static byte[] shortToByteArray(short value) {
+ return new byte[] { (byte) (value >> 8), (byte) value };
+ }
+
+ private static byte[] getRemainingByteArray(@NonNull ByteBuffer buffer) {
+ final byte[] out = new byte[buffer.remaining()];
+ buffer.get(out);
+ return out;
+ }
+
+ // A utility to make a DNS record as byte array.
+ private static class TestDnsRecordByteArrayBuilder {
+ private static final byte[] NAME_COMPRESSION_POINTER = new byte[] { (byte) 0xc0, 0x0c };
+
+ private final String mRRName = "dns.com";
+ private short mRRType = 0;
+ private final short mRRClass = CLASS_IN;
+ private final int mRRTtl = 10;
+ private int mRdataLen = 0;
+ private final ArrayList<byte[]> mRdata = new ArrayList<>();
+ private String mTargetName = null;
+ private short mSvcPriority = 1;
+ private boolean mNameCompression = false;
+
+ TestDnsRecordByteArrayBuilder setNameCompression(boolean value) {
+ mNameCompression = value;
+ return this;
+ }
+
+ TestDnsRecordByteArrayBuilder setRRType(int value) {
+ mRRType = (short) value;
+ return this;
+ }
+
+ TestDnsRecordByteArrayBuilder setTargetName(@NonNull String value) throws IOException {
+ mTargetName = value;
+ return this;
+ }
+
+ TestDnsRecordByteArrayBuilder setSvcPriority(int value) {
+ mSvcPriority = (short) value;
+ return this;
+ }
+
+ TestDnsRecordByteArrayBuilder addRdata(@NonNull byte[] value) {
+ mRdata.add(value);
+ mRdataLen += value.length;
+ return this;
+ }
+
+ byte[] build() throws IOException {
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+ final byte[] name = mNameCompression ? NAME_COMPRESSION_POINTER
+ : DnsPacketUtils.DnsRecordParser.domainNameToLabels(mRRName);
+ os.write(name);
+ os.write(shortToByteArray(mRRType));
+ os.write(shortToByteArray(mRRClass));
+ os.write(HexDump.toByteArray(mRRTtl));
+ if (mTargetName == null) {
+ os.write(shortToByteArray((short) mRdataLen));
+ } else {
+ final byte[] targetNameLabels =
+ DnsPacketUtils.DnsRecordParser.domainNameToLabels(mTargetName);
+ mRdataLen += (Short.BYTES + targetNameLabels.length);
+ os.write(shortToByteArray((short) mRdataLen));
+ os.write(shortToByteArray(mSvcPriority));
+ os.write(targetNameLabels);
+ }
+ for (byte[] data : mRdata) {
+ os.write(data);
+ }
+ return os.toByteArray();
+ }
+ }
+
+ @Test
+ public void testSliceAndAdvance() throws Exception {
+ final ByteBuffer buffer = ByteBuffer.wrap(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9});
+ final ByteBuffer slice1 = DnsSvcbRecord.sliceAndAdvance(buffer, 3);
+ final ByteBuffer slice2 = DnsSvcbRecord.sliceAndAdvance(buffer, 4);
+ assertEquals(0, slice1.position());
+ assertEquals(3, slice1.capacity());
+ assertEquals(3, slice1.remaining());
+ assertTrue(slice1.isReadOnly());
+ assertArrayEquals(new byte[] {1, 2, 3}, getRemainingByteArray(slice1));
+ assertEquals(0, slice2.position());
+ assertEquals(4, slice2.capacity());
+ assertEquals(4, slice2.remaining());
+ assertTrue(slice2.isReadOnly());
+ assertArrayEquals(new byte[] {4, 5, 6, 7}, getRemainingByteArray(slice2));
+
+ // Nothing is read if out-of-bound access happens.
+ assertThrows(BufferUnderflowException.class,
+ () -> DnsSvcbRecord.sliceAndAdvance(buffer, 5));
+ assertEquals(7, buffer.position());
+ assertEquals(9, buffer.capacity());
+ assertEquals(2, buffer.remaining());
+ assertArrayEquals(new byte[] {8, 9}, getRemainingByteArray(buffer));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamMandatory() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_MANDATORY);
+ // Check the content returned from toString() for now because the getter function for
+ // this SvcParam hasn't been implemented.
+ // TODO(b/240259333): Consider adding DnsSvcbRecord.isMandatory(String alpn) when needed.
+ assertTrue(record.toString().contains("ipv4hint"));
+ assertTrue(record.toString().contains("alpn"));
+ assertTrue(record.toString().contains("key333"));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamAlpn() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_ALPN_HTTPS);
+ assertEquals(Arrays.asList("h2", "http/1.1"), record.getAlpns());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamNoDefaultAlpn() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(
+ TEST_SVC_PARAM_NO_DEFAULT_ALPN);
+ // Check the content returned from toString() for now because the getter function for
+ // this SvcParam hasn't been implemented.
+ // TODO(b/240259333): Consider adding DnsSvcbRecord.hasNoDefaultAlpn() when needed.
+ assertTrue(record.toString().contains("no-default-alpn"));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamPort() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_PORT);
+ assertEquals(5353, record.getPort());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamIpv4Hint() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_IPV4HINT_2);
+ assertEquals(Arrays.asList(InetAddresses.parseNumericAddress("4.3.2.1")),
+ record.getAddresses());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamEch() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_ECH);
+ // Check the content returned from toString() for now because the getter function for
+ // this SvcParam hasn't been implemented.
+ // TODO(b/240259333): Consider adding DnsSvcbRecord.getEch() when needed.
+ assertTrue(record.toString().contains("ech=6142634465"));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamIpv6Hint() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_IPV6HINT);
+ assertEquals(Arrays.asList(InetAddresses.parseNumericAddress("2001:db8::1")),
+ record.getAddresses());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamDohPath() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(TEST_SVC_PARAM_DOHPATH);
+ assertEquals("/some-path{?dns}", record.getDohPath());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamGeneric_withValue() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(
+ TEST_SVC_PARAM_GENERIC_WITH_VALUE);
+ // Check the content returned from toString() for now because the getter function for
+ // generic SvcParam hasn't been implemented.
+ // TODO(b/240259333): Consider adding DnsSvcbRecord.getValueFromGenericSvcParam(int key)
+ // when needed.
+ assertTrue(record.toString().contains("key12345=1A2B0C"));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_svcParamGeneric_withoutValue() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordWithSingleSvcParam(
+ TEST_SVC_PARAM_GENERIC_WITHOUT_VALUE);
+ // Check the content returned from toString() for now because the getter function for
+ // generic SvcParam hasn't been implemented.
+ // TODO(b/240259333): Consider adding DnsSvcbRecord.getValueFromGenericSvcParam(int key)
+ // when needed.
+ assertTrue(record.toString().contains("key12346"));
+ }
+
+ @Test
+ public void testDnsSvcbRecord() throws Exception {
+ final DnsSvcbRecord record = makeDnsSvcbRecordFromByteArray(
+ new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .setTargetName("doh.dns.com")
+ .addRdata(TEST_SVC_PARAM_ALPN_HTTPS)
+ .addRdata(TEST_SVC_PARAM_IPV4HINT_1)
+ .addRdata(TEST_SVC_PARAM_IPV6HINT)
+ .addRdata(TEST_SVC_PARAM_PORT)
+ .addRdata(TEST_SVC_PARAM_DOHPATH)
+ .build());
+ assertEquals("doh.dns.com", record.getTargetName());
+ assertEquals(Arrays.asList("h2", "http/1.1"), record.getAlpns());
+ assertEquals(5353, record.getPort());
+ assertEquals(Arrays.asList(
+ InetAddresses.parseNumericAddress("1.2.3.4"),
+ InetAddresses.parseNumericAddress("6.7.8.9"),
+ InetAddresses.parseNumericAddress("2001:db8::1")), record.getAddresses());
+ assertEquals("/some-path{?dns}", record.getDohPath());
+ }
+
+ @Test
+ public void testDnsSvcbRecord_createdFromNullObject() throws Exception {
+ assertThrows(NullPointerException.class, () -> makeDnsSvcbRecordFromByteArray(null));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_invalidDnsRecord() throws Exception {
+ // The type is not SVCB.
+ final byte[] bytes1 = new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_A)
+ .addRdata(InetAddresses.parseNumericAddress("1.2.3.4").getAddress())
+ .build();
+ assertThrows(IllegalStateException.class, () -> makeDnsSvcbRecordFromByteArray(bytes1));
+
+ // TargetName is missing.
+ final byte[] bytes2 = new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .addRdata(new byte[] { 0x01, 0x01 })
+ .build();
+ assertThrows(BufferUnderflowException.class, () -> makeDnsSvcbRecordFromByteArray(bytes2));
+
+ // Rdata is empty.
+ final byte[] bytes3 = new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .build();
+ assertThrows(BufferUnderflowException.class, () -> makeDnsSvcbRecordFromByteArray(bytes3));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_repeatedKeyIsInvalid() throws Exception {
+ final byte[] bytes = new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .addRdata(TEST_SVC_PARAM_ALPN_HTTPS)
+ .addRdata(TEST_SVC_PARAM_ALPN_DOQ)
+ .build();
+ assertThrows(DnsPacket.ParseException.class, () -> makeDnsSvcbRecordFromByteArray(bytes));
+ }
+
+ @Test
+ public void testDnsSvcbRecord_invalidContent() throws Exception {
+ final List<byte[]> invalidContents = Arrays.asList(
+ // Invalid SvcParamValue for "mandatory":
+ // - SvcParamValue must not be empty.
+ // - SvcParamValue has less data than expected.
+ // - SvcParamValue has more data than expected.
+ // - SvcParamValue must be multiple of 2.
+ new byte[] { 0x00, 0x00, 0x00, 0x00},
+ new byte[] { 0x00, 0x00, 0x00, 0x02, 0x00, 0x04, 0x00, 0x06 },
+ new byte[] { 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, },
+ new byte[] { 0x00, 0x00, 0x00, 0x03, 0x00, 0x04, 0x00 },
+
+ // Invalid SvcParamValue for "alpn":
+ // - SvcParamValue must not be empty.
+ // - SvcParamValue has less data than expected.
+ // - SvcParamValue has more data than expected.
+ // - Alpn length is less than the actual data size.
+ // - Alpn length is more than the actual data size.
+ // - Alpn must be a non-empty string.
+ new byte[] { 0x00, 0x01, 0x00, 0x00},
+ new byte[] { 0x00, 0x01, 0x00, 0x02, 0x02, 'h', '2' },
+ new byte[] { 0x00, 0x01, 0x00, 0x05, 0x02, 'h', '2' },
+ new byte[] { 0x00, 0x01, 0x00, 0x04, 0x02, 'd', 'o', 't' },
+ new byte[] { 0x00, 0x01, 0x00, 0x04, 0x08, 'd', 'o', 't' },
+ new byte[] { 0x00, 0x01, 0x00, 0x08, 0x02, 'h', '2', 0x00 },
+
+ // Invalid SvcParamValue for "no-default-alpn":
+ // - SvcParamValue must be empty.
+ // - SvcParamValue length must be 0.
+ new byte[] { 0x00, 0x02, 0x00, 0x04, 'd', 'a', 't', 'a' },
+ new byte[] { 0x00, 0x02, 0x00, 0x04 },
+
+ // Invalid SvcParamValue for "port":
+ // - SvcParamValue must not be empty.
+ // - SvcParamValue has less data than expected.
+ // - SvcParamValue has more data than expected.
+ // - SvcParamValue length must be multiple of 2.
+ new byte[] { 0x00, 0x03, 0x00, 0x00 },
+ new byte[] { 0x00, 0x03, 0x00, 0x02, 0x01 },
+ new byte[] { 0x00, 0x03, 0x00, 0x02, 0x01, 0x02, 0x03 },
+ new byte[] { 0x00, 0x03, 0x00, 0x03, 0x01, 0x02, 0x03 },
+
+ // Invalid SvcParamValue for "ipv4hint":
+ // - SvcParamValue must not be empty.
+ // - SvcParamValue has less data than expected.
+ // - SvcParamValue has more data than expected.
+ // - SvcParamValue must be multiple of 4.
+ new byte[] { 0x00, 0x04, 0x00, 0x00 },
+ new byte[] { 0x00, 0x04, 0x00, 0x04, 0x08 },
+ new byte[] { 0x00, 0x04, 0x00, 0x04, 0x08, 0x08, 0x08, 0x08, 0x08 },
+ new byte[] { 0x00, 0x04, 0x00, 0x05, 0x08, 0x08, 0x08, 0x08 },
+
+ // Invalid SvcParamValue for "ipv6hint":
+ // - SvcParamValue must not be empty.
+ // - SvcParamValue has less data than expected.
+ // - SvcParamValue has more data than expected.
+ // - SvcParamValue must be multiple of 16.
+ new byte[] { 0x00, 0x06, 0x00, 0x00 },
+ new byte[] { 0x00, 0x06, 0x00, 0x10, 0x01 },
+ new byte[] { 0x00, 0x06, 0x00, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
+ new byte[] { 0x00, 0x06, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+ 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }
+ );
+
+ for (byte[] content : invalidContents) {
+ final byte[] bytes = new TestDnsRecordByteArrayBuilder()
+ .setRRType(TYPE_SVCB)
+ .addRdata(content)
+ .build();
+ assertThrows(DnsPacket.ParseException.class,
+ () -> makeDnsSvcbRecordFromByteArray(bytes));
+ }
+ }
+
+ @Test
+ public void testDnsSvcbPacket_createdFromNullObject() throws Exception {
+ assertThrows(DnsPacket.ParseException.class, () -> DnsSvcbPacket.fromResponse(null));
+ }
+
+ @Test
+ public void testDnsSvcbPacket() throws Exception {
+ final String dohTargetName = "https.dns.com";
+ final String doqTargetName = "doq.dns.com";
+ final InetAddress[] expectedIpAddressesForHttps = new InetAddress[] {
+ InetAddresses.parseNumericAddress("1.2.3.4"),
+ InetAddresses.parseNumericAddress("6.7.8.9"),
+ InetAddresses.parseNumericAddress("2001:db8::1"),
+ };
+ final InetAddress[] expectedIpAddressesForDoq = new InetAddress[] {
+ InetAddresses.parseNumericAddress("4.3.2.1"),
+ };
+
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+ os.write(makeDnsResponseHeaderAsByteArray(1 /* qdcount */, 2 /* ancount */, 0 /* nscount */,
+ 0 /* arcount */));
+ os.write(TEST_DNS_SVCB_QUESTION_SECTION);
+ // Add answer for alpn h2 and http/1.1.
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_SVCB)
+ .setTargetName(dohTargetName)
+ .addRdata(TEST_SVC_PARAM_ALPN_HTTPS)
+ .addRdata(TEST_SVC_PARAM_IPV4HINT_1)
+ .addRdata(TEST_SVC_PARAM_IPV6HINT)
+ .addRdata(TEST_SVC_PARAM_PORT)
+ .addRdata(TEST_SVC_PARAM_DOHPATH)
+ .build());
+ // Add answer for alpn doq.
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_SVCB)
+ .setTargetName(doqTargetName)
+ .setSvcPriority(2)
+ .addRdata(TEST_SVC_PARAM_ALPN_DOQ)
+ .addRdata(TEST_SVC_PARAM_IPV4HINT_2)
+ .build());
+ final DnsSvcbPacket pkt = DnsSvcbPacket.fromResponse(os.toByteArray());
+
+ assertTrue(pkt.isSupported("http/1.1"));
+ assertTrue(pkt.isSupported("h2"));
+ assertTrue(pkt.isSupported("doq"));
+ assertFalse(pkt.isSupported("http"));
+ assertFalse(pkt.isSupported("h3"));
+ assertFalse(pkt.isSupported(""));
+
+ assertEquals(dohTargetName, pkt.getTargetName("http/1.1"));
+ assertEquals(dohTargetName, pkt.getTargetName("h2"));
+ assertEquals(doqTargetName, pkt.getTargetName("doq"));
+ assertEquals(null, pkt.getTargetName("http"));
+ assertEquals(null, pkt.getTargetName("h3"));
+ assertEquals(null, pkt.getTargetName(""));
+
+ assertEquals(5353, pkt.getPort("http/1.1"));
+ assertEquals(5353, pkt.getPort("h2"));
+ assertEquals(-1, pkt.getPort("doq"));
+ assertEquals(-1, pkt.getPort("http"));
+ assertEquals(-1, pkt.getPort("h3"));
+ assertEquals(-1, pkt.getPort(""));
+
+ assertArrayEquals(expectedIpAddressesForHttps, pkt.getAddresses("http/1.1").toArray());
+ assertArrayEquals(expectedIpAddressesForHttps, pkt.getAddresses("h2").toArray());
+ assertArrayEquals(expectedIpAddressesForDoq, pkt.getAddresses("doq").toArray());
+ assertTrue(pkt.getAddresses("http").isEmpty());
+ assertTrue(pkt.getAddresses("h3").isEmpty());
+ assertTrue(pkt.getAddresses("").isEmpty());
+
+ assertEquals("/some-path{?dns}", pkt.getDohPath("http/1.1"));
+ assertEquals("/some-path{?dns}", pkt.getDohPath("h2"));
+ assertEquals("", pkt.getDohPath("doq"));
+ assertEquals(null, pkt.getDohPath("http"));
+ assertEquals(null, pkt.getDohPath("h3"));
+ assertEquals(null, pkt.getDohPath(""));
+ }
+
+ @Test
+ public void testDnsSvcbPacket_noIpHint() throws Exception {
+ final String targetName = "doq.dns.com";
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+ os.write(makeDnsResponseHeaderAsByteArray(1 /* qdcount */, 1 /* ancount */, 0 /* nscount */,
+ 0 /* arcount */));
+ os.write(TEST_DNS_SVCB_QUESTION_SECTION);
+ // Add answer for alpn doq.
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_SVCB)
+ .setTargetName(targetName)
+ .addRdata(TEST_SVC_PARAM_ALPN_DOQ)
+ .build());
+ final DnsSvcbPacket pkt = DnsSvcbPacket.fromResponse(os.toByteArray());
+
+ assertTrue(pkt.isSupported("doq"));
+ assertEquals(targetName, pkt.getTargetName("doq"));
+ assertEquals(-1, pkt.getPort("doq"));
+ assertArrayEquals(new InetAddress[] {}, pkt.getAddresses("doq").toArray());
+ assertEquals("", pkt.getDohPath("doq"));
+ }
+
+ @Test
+ public void testDnsSvcbPacket_hasAnswerInAdditionalSection() throws Exception {
+ final InetAddress[] expectedIpAddresses = new InetAddress[] {
+ InetAddresses.parseNumericAddress("1.2.3.4"),
+ InetAddresses.parseNumericAddress("2001:db8::2"),
+ };
+
+ final ByteArrayOutputStream os = new ByteArrayOutputStream();
+ os.write(makeDnsResponseHeaderAsByteArray(1 /* qdcount */, 1 /* ancount */, 0 /* nscount */,
+ 2 /* arcount */));
+ os.write(TEST_DNS_SVCB_QUESTION_SECTION);
+ // Add SVCB record in the Answer section.
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_SVCB)
+ .setTargetName("doq.dns.com")
+ .addRdata(TEST_SVC_PARAM_ALPN_DOQ)
+ .addRdata(TEST_SVC_PARAM_IPV4HINT_2)
+ .addRdata(TEST_SVC_PARAM_IPV6HINT)
+ .build());
+ // Add A/AAAA records in the Additional section.
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_A)
+ .addRdata(InetAddresses.parseNumericAddress("1.2.3.4").getAddress())
+ .build());
+ os.write(new TestDnsRecordByteArrayBuilder()
+ .setNameCompression(true)
+ .setRRType(TYPE_AAAA)
+ .addRdata(InetAddresses.parseNumericAddress("2001:db8::2").getAddress())
+ .build());
+ final DnsSvcbPacket pkt = DnsSvcbPacket.fromResponse(os.toByteArray());
+
+ // If there are A/AAAA records in the Additional section, getAddresses() returns the IP
+ // addresses in those records instead of the IP addresses in ipv4hint/ipv6hint.
+ assertArrayEquals(expectedIpAddresses, pkt.getAddresses("doq").toArray());
+ }
+}
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
index bb32052..198b009 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java
@@ -62,6 +62,8 @@
import android.util.Log;
import android.util.Pair;
+import androidx.annotation.Nullable;
+
import com.android.compatibility.common.util.AmUtils;
import com.android.compatibility.common.util.BatteryUtils;
import com.android.compatibility.common.util.DeviceConfigStateHelper;
@@ -283,8 +285,30 @@
}
protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
+ assertBackgroundNetworkAccess(expectAllowed, null);
+ }
+
+ /**
+ * Asserts whether the active network is available or not for the background app. If the network
+ * is unavailable, also checks whether it is blocked by the expected error.
+ *
+ * @param expectAllowed expect background network access to be allowed or not.
+ * @param expectedUnavailableError the expected error when {@code expectAllowed} is false. It's
+ * meaningful only when the {@code expectAllowed} is 'false'.
+ * Throws an IllegalArgumentException when {@code expectAllowed}
+ * is true and this parameter is not null. When the
+ * {@code expectAllowed} is 'false' and this parameter is null,
+ * this function does not compare error type of the networking
+ * access failure.
+ */
+ protected void assertBackgroundNetworkAccess(boolean expectAllowed,
+ @Nullable final String expectedUnavailableError) throws Exception {
assertBackgroundState();
- assertNetworkAccess(expectAllowed /* expectAvailable */, false /* needScreenOn */);
+ if (expectAllowed && expectedUnavailableError != null) {
+ throw new IllegalArgumentException("expectedUnavailableError is not null");
+ }
+ assertNetworkAccess(expectAllowed /* expectAvailable */, false /* needScreenOn */,
+ expectedUnavailableError);
}
protected void assertForegroundNetworkAccess() throws Exception {
@@ -407,12 +431,17 @@
*/
private void assertNetworkAccess(boolean expectAvailable, boolean needScreenOn)
throws Exception {
+ assertNetworkAccess(expectAvailable, needScreenOn, null);
+ }
+
+ private void assertNetworkAccess(boolean expectAvailable, boolean needScreenOn,
+ @Nullable final String expectedUnavailableError) throws Exception {
final int maxTries = 5;
String error = null;
int timeoutMs = 500;
for (int i = 1; i <= maxTries; i++) {
- error = checkNetworkAccess(expectAvailable);
+ error = checkNetworkAccess(expectAvailable, expectedUnavailableError);
if (error == null) return;
@@ -479,12 +508,15 @@
*
* @return error message with the mismatch (or empty if assertion passed).
*/
- private String checkNetworkAccess(boolean expectAvailable) throws Exception {
+ private String checkNetworkAccess(boolean expectAvailable,
+ @Nullable final String expectedUnavailableError) throws Exception {
final String resultData = mServiceClient.checkNetworkStatus();
- return checkForAvailabilityInResultData(resultData, expectAvailable);
+ return checkForAvailabilityInResultData(resultData, expectAvailable,
+ expectedUnavailableError);
}
- private String checkForAvailabilityInResultData(String resultData, boolean expectAvailable) {
+ private String checkForAvailabilityInResultData(String resultData, boolean expectAvailable,
+ @Nullable final String expectedUnavailableError) {
if (resultData == null) {
assertNotNull("Network status from app2 is null", resultData);
}
@@ -516,6 +548,10 @@
if (expectedState != state || expectedDetailedState != detailedState) {
errors.append(String.format("Connection state mismatch: expected %s/%s, got %s/%s\n",
expectedState, expectedDetailedState, state, detailedState));
+ } else if (!expectAvailable && (expectedUnavailableError != null)
+ && !connectionCheckDetails.contains(expectedUnavailableError)) {
+ errors.append("Connection unavailable reason mismatch: expected "
+ + expectedUnavailableError + "\n");
}
if (errors.length() > 0) {
@@ -914,7 +950,7 @@
final String resultData = result.get(0).second;
if (resultCode == INetworkStateObserver.RESULT_SUCCESS_NETWORK_STATE_CHECKED) {
final String error = checkForAvailabilityInResultData(
- resultData, expectAvailable);
+ resultData, expectAvailable, null /* expectedUnavailableError */);
if (error != null) {
fail("Network is not available for activity in app2 (" + mUid + "): "
+ error);
@@ -949,7 +985,7 @@
final String resultData = result.get(0).second;
if (resultCode == INetworkStateObserver.RESULT_SUCCESS_NETWORK_STATE_CHECKED) {
final String error = checkForAvailabilityInResultData(
- resultData, expectAvailable);
+ resultData, expectAvailable, null /* expectedUnavailableError */);
if (error != null) {
Log.d(TAG, "Network state is unexpected, checking again. " + error);
// Right now we could end up in an unexpected state if expedited job
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java
index ab3cf14..82f4a65 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkCallbackTest.java
@@ -32,8 +32,11 @@
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
+import android.net.cts.util.CtsNetUtils;
import android.util.Log;
+import com.android.modules.utils.build.SdkLevel;
+
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@@ -46,6 +49,9 @@
public class NetworkCallbackTest extends AbstractRestrictBackgroundNetworkTestCase {
private Network mNetwork;
private final TestNetworkCallback mTestNetworkCallback = new TestNetworkCallback();
+ private CtsNetUtils mCtsNetUtils;
+ private static final String GOOGLE_PRIVATE_DNS_SERVER = "dns.google";
+
@Rule
public final MeterednessConfigurationRule mMeterednessConfiguration
= new MeterednessConfigurationRule();
@@ -218,6 +224,26 @@
mTestNetworkCallback.expectCapabilitiesCallbackEventually(mNetwork,
false /* hasCapability */, NET_CAPABILITY_NOT_METERED);
mTestNetworkCallback.expectBlockedStatusCallback(mNetwork, false);
+
+ // Before Android T, DNS queries over private DNS should be but are not restricted by Power
+ // Saver or Data Saver. The issue is fixed in mainline update and apps can no longer request
+ // DNS queries when its network is restricted by Power Saver. The fix takes effect backwards
+ // starting from Android T. But for Data Saver, the fix is not backward compatible since
+ // there are some platform changes involved. It is only available on devices that a specific
+ // trunk flag is enabled.
+ //
+ // This test can not only verify that the network traffic from apps is blocked at the right
+ // time, but also verify whether it is correctly blocked at the DNS stage, or at a later
+ // socket connection stage.
+ if (SdkLevel.isAtLeastT()) {
+ // Enable private DNS
+ mCtsNetUtils = new CtsNetUtils(mContext);
+ mCtsNetUtils.storePrivateDnsSetting();
+ mCtsNetUtils.setPrivateDnsStrictMode(GOOGLE_PRIVATE_DNS_SERVER);
+ mCtsNetUtils.awaitPrivateDnsSetting(
+ "NetworkCallbackTest wait private DNS setting timeout", mNetwork,
+ GOOGLE_PRIVATE_DNS_SERVER, true);
+ }
}
@After
@@ -227,6 +253,10 @@
setRestrictBackground(false);
setBatterySaverMode(false);
unregisterNetworkCallback();
+
+ if (SdkLevel.isAtLeastT() && (mCtsNetUtils != null)) {
+ mCtsNetUtils.restorePrivateDnsSetting();
+ }
}
@RequiredProperties({DATA_SAVER_MODE})
@@ -235,6 +265,8 @@
try {
// Enable restrict background
setRestrictBackground(true);
+ // TODO: Verify expectedUnavailableError when aconfig support mainline.
+ // (see go/aconfig-in-mainline-problems)
assertBackgroundNetworkAccess(false);
assertNetworkAccessBlockedByBpf(true, mUid, true /* metered */);
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
@@ -247,6 +279,7 @@
// Remove from whitelist
removeRestrictBackgroundWhitelist(mUid);
+ // TODO: Verify expectedUnavailableError when aconfig support mainline.
assertBackgroundNetworkAccess(false);
assertNetworkAccessBlockedByBpf(true, mUid, true /* metered */);
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
@@ -278,7 +311,11 @@
try {
// Enable Power Saver
setBatterySaverMode(true);
- assertBackgroundNetworkAccess(false);
+ if (SdkLevel.isAtLeastT()) {
+ assertBackgroundNetworkAccess(false, "java.net.UnknownHostException");
+ } else {
+ assertBackgroundNetworkAccess(false);
+ }
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
assertNetworkAccessBlockedByBpf(true, mUid, true /* metered */);
@@ -298,7 +335,11 @@
try {
// Enable Power Saver
setBatterySaverMode(true);
- assertBackgroundNetworkAccess(false);
+ if (SdkLevel.isAtLeastT()) {
+ assertBackgroundNetworkAccess(false, "java.net.UnknownHostException");
+ } else {
+ assertBackgroundNetworkAccess(false);
+ }
mTestNetworkCallback.expectBlockedStatusCallbackEventually(mNetwork, true);
assertNetworkAccessBlockedByBpf(true, mUid, false /* metered */);
diff --git a/tests/native/utilities/firewall.cpp b/tests/native/utilities/firewall.cpp
index 22f83e8..669b76a 100644
--- a/tests/native/utilities/firewall.cpp
+++ b/tests/native/utilities/firewall.cpp
@@ -59,9 +59,11 @@
Result<void> Firewall::addRule(uint32_t uid, UidOwnerMatchType match, uint32_t iif) {
// iif should be non-zero if and only if match == MATCH_IIF
if (match == IIF_MATCH && iif == 0) {
- return Errorf("Interface match {} must have nonzero interface index", match);
+ return Errorf("Interface match {} must have nonzero interface index",
+ static_cast<int>(match));
} else if (match != IIF_MATCH && iif != 0) {
- return Errorf("Non-interface match {} must have zero interface index", match);
+ return Errorf("Non-interface match {} must have zero interface index",
+ static_cast<int>(match));
}
std::lock_guard guard(mMutex);
diff --git a/tests/unit/java/android/net/MulticastRoutingConfigTest.kt b/tests/unit/java/android/net/MulticastRoutingConfigTest.kt
new file mode 100644
index 0000000..f01057b
--- /dev/null
+++ b/tests/unit/java/android/net/MulticastRoutingConfigTest.kt
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net
+
+import android.os.Build
+import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
+import com.android.testutils.DevSdkIgnoreRunner
+
+import org.junit.Test
+import org.junit.runner.RunWith
+import java.net.Inet6Address
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
+
+import android.net.MulticastRoutingConfig.Builder
+import android.net.MulticastRoutingConfig.FORWARD_NONE
+import android.net.MulticastRoutingConfig.FORWARD_SELECTED
+import android.net.MulticastRoutingConfig.FORWARD_WITH_MIN_SCOPE
+
+@RunWith(DevSdkIgnoreRunner::class)
+@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+class MulticastRoutingConfigTest {
+
+ val address1 = Inet6Address.getByName("2000::8888") as Inet6Address
+ val address2 = Inet6Address.getByName("2000::9999") as Inet6Address
+
+ private fun configNone() = Builder(FORWARD_NONE).build()
+ private fun configMinScope(scope: Int) = Builder(FORWARD_WITH_MIN_SCOPE, scope).build()
+ private fun configSelected() = Builder(FORWARD_SELECTED).build()
+ private fun configSelectedWithAddress1AndAddress2() =
+ Builder(FORWARD_SELECTED).addListeningAddress(address1)
+ .addListeningAddress(address2).build()
+ private fun configSelectedWithAddress2AndAddress1() =
+ Builder(FORWARD_SELECTED).addListeningAddress(address2)
+ .addListeningAddress(address1).build()
+
+ @Test
+ fun equalityTests() {
+
+ assertTrue(configNone().equals(configNone()))
+
+ assertTrue(configSelected().equals(configSelected()))
+
+ assertTrue(configMinScope(4).equals(configMinScope(4)))
+
+ assertTrue(configSelectedWithAddress1AndAddress2()
+ .equals(configSelectedWithAddress2AndAddress1()))
+ }
+
+ @Test
+ fun inequalityTests() {
+
+ assertFalse(configNone().equals(configSelected()))
+
+ assertFalse(configNone().equals(configMinScope(4)))
+
+ assertFalse(configSelected().equals(configMinScope(4)))
+
+ assertFalse(configMinScope(4).equals(configMinScope(5)))
+
+ assertFalse(configSelected().equals(configSelectedWithAddress1AndAddress2()))
+ }
+
+ @Test
+ fun toString_equalObjects_returnsEqualStrings() {
+ val config1 = configSelectedWithAddress1AndAddress2()
+ val config2 = configSelectedWithAddress2AndAddress1()
+
+ val str1 = config1.toString()
+ val str2 = config2.toString()
+
+ assertTrue(str1.equals(str2))
+ }
+
+ @Test
+ fun toString_unequalObjects_returnsUnequalStrings() {
+ val config1 = configSelected()
+ val config2 = configSelectedWithAddress1AndAddress2()
+
+ val str1 = config1.toString()
+ val str2 = config2.toString()
+
+ assertFalse(str1.equals(str2))
+ }
+}
diff --git a/tests/unit/java/com/android/server/connectivity/VpnTest.java b/tests/unit/java/com/android/server/connectivity/VpnTest.java
index 48cfe77..ff801e5 100644
--- a/tests/unit/java/com/android/server/connectivity/VpnTest.java
+++ b/tests/unit/java/com/android/server/connectivity/VpnTest.java
@@ -56,11 +56,9 @@
import static com.android.server.connectivity.Vpn.PREFERRED_IKE_PROTOCOL_IPV4_UDP;
import static com.android.server.connectivity.Vpn.PREFERRED_IKE_PROTOCOL_IPV6_ESP;
import static com.android.server.connectivity.Vpn.PREFERRED_IKE_PROTOCOL_IPV6_UDP;
-import static com.android.testutils.Cleanup.testAndCleanup;
import static com.android.testutils.HandlerUtils.waitForIdleSerialExecutor;
import static com.android.testutils.MiscAsserts.assertThrows;
-import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -149,7 +147,6 @@
import android.net.wifi.WifiInfo;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
-import android.os.ConditionVariable;
import android.os.INetworkManagementService;
import android.os.ParcelFileDescriptor;
import android.os.PersistableBundle;
@@ -1983,22 +1980,6 @@
// a subsequent CL.
}
- @Test
- public void testStartLegacyVpnIpv6() throws Exception {
- setMockedUsers(PRIMARY_USER);
- final Vpn vpn = createVpn(PRIMARY_USER.id);
- final LinkProperties lp = new LinkProperties();
- lp.setInterfaceName(EGRESS_IFACE);
- lp.addLinkAddress(new LinkAddress("2001:db8::1/64"));
- final RouteInfo defaultRoute = new RouteInfo(
- new IpPrefix(Inet6Address.ANY, 0), null, EGRESS_IFACE);
- lp.addRoute(defaultRoute);
-
- // IllegalStateException thrown since legacy VPN only supports IPv4.
- assertThrows(IllegalStateException.class,
- () -> vpn.startLegacyVpn(mVpnProfile, EGRESS_NETWORK, lp));
- }
-
private Vpn startLegacyVpn(final Vpn vpn, final VpnProfile vpnProfile) throws Exception {
setMockedUsers(PRIMARY_USER);
@@ -3112,23 +3093,15 @@
}
@Test
- public void testStartRacoonNumericAddress() throws Exception {
- startRacoon("1.2.3.4", "1.2.3.4");
- }
+ public void testStartLegacyVpnType() throws Exception {
+ setMockedUsers(PRIMARY_USER);
+ final Vpn vpn = createVpn(PRIMARY_USER.id);
+ final VpnProfile profile = new VpnProfile("testProfile" /* key */);
- @Test
- public void testStartRacoonHostname() throws Exception {
- startRacoon("hostname", "5.6.7.8"); // address returned by deps.resolve
- }
-
- @Test
- public void testStartPptp() throws Exception {
- startPptp(true /* useMppe */);
- }
-
- @Test
- public void testStartPptp_NoMppe() throws Exception {
- startPptp(false /* useMppe */);
+ profile.type = VpnProfile.TYPE_PPTP;
+ assertThrows(UnsupportedOperationException.class, () -> startLegacyVpn(vpn, profile));
+ profile.type = VpnProfile.TYPE_L2TP_IPSEC_PSK;
+ assertThrows(UnsupportedOperationException.class, () -> startLegacyVpn(vpn, profile));
}
private void assertTransportInfoMatches(NetworkCapabilities nc, int type) {
@@ -3138,125 +3111,6 @@
assertEquals(type, ti.getType());
}
- private void startPptp(boolean useMppe) throws Exception {
- final VpnProfile profile = new VpnProfile("testProfile" /* key */);
- profile.type = VpnProfile.TYPE_PPTP;
- profile.name = "testProfileName";
- profile.username = "userName";
- profile.password = "thePassword";
- profile.server = "192.0.2.123";
- profile.mppe = useMppe;
-
- doReturn(new Network[] { new Network(101) }).when(mConnectivityManager).getAllNetworks();
- doReturn(new Network(102)).when(mConnectivityManager).registerNetworkAgent(
- any(), // INetworkAgent
- any(), // NetworkInfo
- any(), // LinkProperties
- any(), // NetworkCapabilities
- any(), // LocalNetworkConfig
- any(), // NetworkScore
- any(), // NetworkAgentConfig
- anyInt()); // provider ID
-
- final Vpn vpn = startLegacyVpn(createVpn(PRIMARY_USER.id), profile);
- final TestDeps deps = (TestDeps) vpn.mDeps;
-
- testAndCleanup(() -> {
- final String[] mtpdArgs = deps.mtpdArgs.get(10, TimeUnit.SECONDS);
- final String[] argsPrefix = new String[]{
- EGRESS_IFACE, "pptp", profile.server, "1723", "name", profile.username,
- "password", profile.password, "linkname", "vpn", "refuse-eap", "nodefaultroute",
- "usepeerdns", "idle", "1800", "mtu", "1270", "mru", "1270"
- };
- assertArrayEquals(argsPrefix, Arrays.copyOf(mtpdArgs, argsPrefix.length));
- if (useMppe) {
- assertEquals(argsPrefix.length + 2, mtpdArgs.length);
- assertEquals("+mppe", mtpdArgs[argsPrefix.length]);
- assertEquals("-pap", mtpdArgs[argsPrefix.length + 1]);
- } else {
- assertEquals(argsPrefix.length + 1, mtpdArgs.length);
- assertEquals("nomppe", mtpdArgs[argsPrefix.length]);
- }
-
- verify(mConnectivityManager, timeout(10_000)).registerNetworkAgent(
- any(), // INetworkAgent
- any(), // NetworkInfo
- any(), // LinkProperties
- any(), // NetworkCapabilities
- any(), // LocalNetworkConfig
- any(), // NetworkScore
- any(), // NetworkAgentConfig
- anyInt()); // provider ID
- }, () -> { // Cleanup
- vpn.mVpnRunner.exitVpnRunner();
- deps.getStateFile().delete(); // set to delete on exit, but this deletes it earlier
- vpn.mVpnRunner.join(10_000); // wait for up to 10s for the runner to die and cleanup
- });
- }
-
- public void startRacoon(final String serverAddr, final String expectedAddr)
- throws Exception {
- final ConditionVariable legacyRunnerReady = new ConditionVariable();
- final VpnProfile profile = new VpnProfile("testProfile" /* key */);
- profile.type = VpnProfile.TYPE_L2TP_IPSEC_PSK;
- profile.name = "testProfileName";
- profile.username = "userName";
- profile.password = "thePassword";
- profile.server = serverAddr;
- profile.ipsecIdentifier = "id";
- profile.ipsecSecret = "secret";
- profile.l2tpSecret = "l2tpsecret";
-
- when(mConnectivityManager.getAllNetworks())
- .thenReturn(new Network[] { new Network(101) });
-
- when(mConnectivityManager.registerNetworkAgent(any(), any(), any(), any(),
- any(), any(), any(), anyInt())).thenAnswer(invocation -> {
- // The runner has registered an agent and is now ready.
- legacyRunnerReady.open();
- return new Network(102);
- });
- final Vpn vpn = startLegacyVpn(createVpn(PRIMARY_USER.id), profile);
- final TestDeps deps = (TestDeps) vpn.mDeps;
- try {
- // udppsk and 1701 are the values for TYPE_L2TP_IPSEC_PSK
- assertArrayEquals(
- new String[] { EGRESS_IFACE, expectedAddr, "udppsk",
- profile.ipsecIdentifier, profile.ipsecSecret, "1701" },
- deps.racoonArgs.get(10, TimeUnit.SECONDS));
- // literal values are hardcoded in Vpn.java for mtpd args
- assertArrayEquals(
- new String[] { EGRESS_IFACE, "l2tp", expectedAddr, "1701", profile.l2tpSecret,
- "name", profile.username, "password", profile.password,
- "linkname", "vpn", "refuse-eap", "nodefaultroute", "usepeerdns",
- "idle", "1800", "mtu", "1270", "mru", "1270" },
- deps.mtpdArgs.get(10, TimeUnit.SECONDS));
-
- // Now wait for the runner to be ready before testing for the route.
- ArgumentCaptor<LinkProperties> lpCaptor = ArgumentCaptor.forClass(LinkProperties.class);
- ArgumentCaptor<NetworkCapabilities> ncCaptor =
- ArgumentCaptor.forClass(NetworkCapabilities.class);
- verify(mConnectivityManager, timeout(10_000)).registerNetworkAgent(any(), any(),
- lpCaptor.capture(), ncCaptor.capture(), any(), any(), any(), anyInt());
-
- // In this test the expected address is always v4 so /32.
- // Note that the interface needs to be specified because RouteInfo objects stored in
- // LinkProperties objects always acquire the LinkProperties' interface.
- final RouteInfo expectedRoute = new RouteInfo(new IpPrefix(expectedAddr + "/32"),
- null, EGRESS_IFACE, RouteInfo.RTN_THROW);
- final List<RouteInfo> actualRoutes = lpCaptor.getValue().getRoutes();
- assertTrue("Expected throw route (" + expectedRoute + ") not found in " + actualRoutes,
- actualRoutes.contains(expectedRoute));
-
- assertTransportInfoMatches(ncCaptor.getValue(), VpnManager.TYPE_VPN_LEGACY);
- } finally {
- // Now interrupt the thread, unblock the runner and clean up.
- vpn.mVpnRunner.exitVpnRunner();
- deps.getStateFile().delete(); // set to delete on exit, but this deletes it earlier
- vpn.mVpnRunner.join(10_000); // wait for up to 10s for the runner to die and cleanup
- }
- }
-
// Make it public and un-final so as to spy it
public class TestDeps extends Vpn.Dependencies {
public final CompletableFuture<String[]> racoonArgs = new CompletableFuture();
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java
index 8917ed3..ad30ce0 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsMultinetworkSocketClientTest.java
@@ -82,8 +82,8 @@
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
mSocketKey = new SocketKey(1000 /* interfaceIndex */);
- mSocketClient = new MdnsMultinetworkSocketClient(
- mHandlerThread.getLooper(), mProvider, mSharedLog);
+ mSocketClient = new MdnsMultinetworkSocketClient(mHandlerThread.getLooper(), mProvider,
+ mSharedLog, MdnsFeatureFlags.newBuilder().build());
mHandler.post(() -> mSocketClient.setCallback(mCallback));
}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketReaderTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketReaderTests.java
index 19d8a00..37588b5 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketReaderTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketReaderTests.java
@@ -75,7 +75,7 @@
+ "the packet length");
} catch (IOException e) {
// Expected
- } catch (Exception e) {
+ } catch (RuntimeException e) {
fail(String.format(
Locale.ROOT,
"Should not have thrown any other exception except " + "for IOException: %s",
@@ -83,4 +83,4 @@
}
assertEquals(data.length, packetReader.getRemaining());
}
-}
\ No newline at end of file
+}
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt
index 28ea4b6..0877b68 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsPacketTest.kt
@@ -27,6 +27,9 @@
@RunWith(DevSdkIgnoreRunner::class)
class MdnsPacketTest {
+ private fun makeFlags(isLabelCountLimitEnabled: Boolean = false): MdnsFeatureFlags =
+ MdnsFeatureFlags.newBuilder()
+ .setIsLabelCountLimitEnabled(isLabelCountLimitEnabled).build()
@Test
fun testParseQuery() {
// Probe packet with 1 question for Android.local, and 4 additionalRecords with 4 addresses
@@ -38,7 +41,7 @@
"010db8000000000000000000000789"
val bytes = HexDump.hexStringToByteArray(packetHex)
- val reader = MdnsPacketReader(bytes, bytes.size)
+ val reader = MdnsPacketReader(bytes, bytes.size, makeFlags())
val packet = MdnsPacket.parse(reader)
assertEquals(123, packet.transactionId)
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
index 3fc656a..a22e8c6 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsResponseDecoderTests.java
@@ -17,8 +17,10 @@
package com.android.server.connectivity.mdns;
import static android.net.InetAddresses.parseNumericAddress;
+
import static com.android.server.connectivity.mdns.util.MdnsUtils.Clock;
import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
+
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -337,7 +339,8 @@
packet.setSocketAddress(
new InetSocketAddress(MdnsConstants.getMdnsIPv6Address(), MdnsConstants.MDNS_PORT));
- final MdnsPacket parsedPacket = MdnsResponseDecoder.parseResponse(data6, data6.length);
+ final MdnsPacket parsedPacket = MdnsResponseDecoder.parseResponse(
+ data6, data6.length, MdnsFeatureFlags.newBuilder().build());
assertNotNull(parsedPacket);
final Network network = mock(Network.class);
@@ -636,7 +639,8 @@
private ArraySet<MdnsResponse> decode(MdnsResponseDecoder decoder, byte[] data,
Collection<MdnsResponse> existingResponses) throws MdnsPacket.ParseException {
- final MdnsPacket parsedPacket = MdnsResponseDecoder.parseResponse(data, data.length);
+ final MdnsPacket parsedPacket = MdnsResponseDecoder.parseResponse(
+ data, data.length, MdnsFeatureFlags.newBuilder().build());
assertNotNull(parsedPacket);
return new ArraySet<>(decoder.augmentResponses(parsedPacket,
diff --git a/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketClientTests.java b/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketClientTests.java
index 74f1c37..8b7ab71 100644
--- a/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketClientTests.java
+++ b/tests/unit/java/com/android/server/connectivity/mdns/MdnsSocketClientTests.java
@@ -78,6 +78,7 @@
@Mock private SharedLog sharedLog;
private MdnsSocketClient mdnsClient;
+ private MdnsFeatureFlags flags = MdnsFeatureFlags.newBuilder().build();
@Before
public void setup() throws RuntimeException, IOException {
@@ -86,7 +87,7 @@
when(mockWifiManager.createMulticastLock(ArgumentMatchers.anyString()))
.thenReturn(mockMulticastLock);
- mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog) {
+ mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog, flags) {
@Override
MdnsSocket createMdnsSocket(int port, SharedLog sharedLog) throws IOException {
if (port == MdnsConstants.MDNS_PORT) {
@@ -515,7 +516,7 @@
//MdnsConfigsFlagsImpl.allowNetworkInterfaceIndexPropagation.override(true);
when(mockMulticastSocket.getInterfaceIndex()).thenReturn(21);
- mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog) {
+ mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog, flags) {
@Override
MdnsSocket createMdnsSocket(int port, SharedLog sharedLog) {
if (port == MdnsConstants.MDNS_PORT) {
@@ -538,7 +539,7 @@
//MdnsConfigsFlagsImpl.allowNetworkInterfaceIndexPropagation.override(false);
when(mockMulticastSocket.getInterfaceIndex()).thenReturn(21);
- mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog) {
+ mdnsClient = new MdnsSocketClient(mContext, mockMulticastLock, sharedLog, flags) {
@Override
MdnsSocket createMdnsSocket(int port, SharedLog sharedLog) {
if (port == MdnsConstants.MDNS_PORT) {
diff --git a/tests/unit/java/com/android/server/connectivityservice/CSLocalAgentTests.kt b/tests/unit/java/com/android/server/connectivityservice/CSLocalAgentTests.kt
index 235f7de..1dab548 100644
--- a/tests/unit/java/com/android/server/connectivityservice/CSLocalAgentTests.kt
+++ b/tests/unit/java/com/android/server/connectivityservice/CSLocalAgentTests.kt
@@ -29,6 +29,7 @@
import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED
import android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED
import android.net.NetworkCapabilities.TRANSPORT_CELLULAR
+import android.net.NetworkCapabilities.TRANSPORT_THREAD
import android.net.NetworkCapabilities.TRANSPORT_WIFI
import android.net.NetworkRequest
import android.net.NetworkScore
@@ -472,4 +473,55 @@
cb.expect<Lost>(localAgent.network)
cb.assertNoCallback()
}
+
+ @Test
+ fun testLocalNetworkUnwanted_withUpstream() {
+ doTestLocalNetworkUnwanted(true)
+ }
+
+ @Test
+ fun testLocalNetworkUnwanted_withoutUpstream() {
+ doTestLocalNetworkUnwanted(false)
+ }
+
+ fun doTestLocalNetworkUnwanted(haveUpstream: Boolean) {
+ deps.setBuildSdk(VERSION_V)
+
+ val nr = NetworkRequest.Builder().addCapability(NET_CAPABILITY_LOCAL_NETWORK).build()
+ val requestCb = TestableNetworkCallback()
+ cm.requestNetwork(nr, requestCb)
+ val listenCb = TestableNetworkCallback()
+ cm.registerNetworkCallback(nr, listenCb)
+
+ val upstream = if (haveUpstream) {
+ Agent(score = keepScore(), lp = lp("wifi0"),
+ nc = nc(TRANSPORT_WIFI)).also { it.connect() }
+ } else {
+ null
+ }
+
+ // Set up a local agent.
+ val lnc = LocalNetworkConfig.Builder().apply {
+ if (haveUpstream) {
+ setUpstreamSelector(NetworkRequest.Builder()
+ .addTransportType(TRANSPORT_WIFI)
+ .build())
+ }
+ }.build()
+ val localAgent = Agent(nc = nc(TRANSPORT_THREAD, NET_CAPABILITY_LOCAL_NETWORK),
+ lp = lp("local0"),
+ lnc = lnc,
+ score = FromS(NetworkScore.Builder().build())
+ )
+ localAgent.connect()
+
+ requestCb.expectAvailableCallbacks(localAgent.network,
+ validated = false, upstream = upstream?.network)
+ listenCb.expectAvailableCallbacks(localAgent.network,
+ validated = false, upstream = upstream?.network)
+
+ cm.unregisterNetworkCallback(requestCb)
+
+ listenCb.expect<Lost>()
+ }
}
diff --git a/thread/TEST_MAPPING b/thread/TEST_MAPPING
index 3eaebfa..30aeca5 100644
--- a/thread/TEST_MAPPING
+++ b/thread/TEST_MAPPING
@@ -3,5 +3,10 @@
{
"name": "CtsThreadNetworkTestCases"
}
+ ],
+ "postsubmit": [
+ {
+ "name": "ThreadNetworkUnitTests"
+ }
]
}
diff --git a/thread/framework/java/android/net/thread/ActiveOperationalDataset.java b/thread/framework/java/android/net/thread/ActiveOperationalDataset.java
index c9b047a..b74a15a 100644
--- a/thread/framework/java/android/net/thread/ActiveOperationalDataset.java
+++ b/thread/framework/java/android/net/thread/ActiveOperationalDataset.java
@@ -16,8 +16,6 @@
package android.net.thread;
-import static android.net.thread.ActiveOperationalDataset.SecurityPolicy.DEFAULT_ROTATION_TIME_HOURS;
-
import static com.android.internal.util.Preconditions.checkArgument;
import static com.android.internal.util.Preconditions.checkState;
import static com.android.net.module.util.HexDump.dumpHexString;
@@ -41,26 +39,25 @@
import java.io.ByteArrayOutputStream;
import java.net.Inet6Address;
import java.net.UnknownHostException;
-import java.security.SecureRandom;
-import java.time.Instant;
import java.util.Arrays;
-import java.util.Random;
/**
* Data interface for managing a Thread Active Operational Dataset.
*
- * <p>An example usage of creating an Active Operational Dataset with random parameters:
+ * <p>An example usage of creating an Active Operational Dataset with randomized parameters:
*
* <pre>{@code
- * ActiveOperationalDataset activeDataset = ActiveOperationalDataset.createRandomDataset();
+ * ActiveOperationalDataset activeDataset = controller.createRandomizedDataset("MyNet");
* }</pre>
*
- * <p>or random Dataset with customized Network Name:
+ * <p>or randomized Dataset with customized channel:
*
* <pre>{@code
* ActiveOperationalDataset activeDataset =
- * new ActiveOperationalDataset.Builder(ActiveOperationalDataset.createRandomDataset())
- * .setNetworkName("MyThreadNet").build();
+ * new ActiveOperationalDataset.Builder(controller.createRandomizedDataset("MyNet"))
+ * .setChannel(CHANNEL_PAGE_24_GHZ, 17)
+ * .setActiveTimestamp(OperationalDatasetTimestamp.fromInstant(Instant.now()))
+ * .build();
* }</pre>
*
* <p>If the Active Operational Dataset is already known as <a
@@ -116,7 +113,9 @@
/** @hide */
@VisibleForTesting public static final int TYPE_CHANNEL_MASK = 53;
- private static final byte MESH_LOCAL_PREFIX_FIRST_BYTE = (byte) 0xfd;
+ /** @hide */
+ public static final byte MESH_LOCAL_PREFIX_FIRST_BYTE = (byte) 0xfd;
+
private static final int LENGTH_CHANNEL = 3;
private static final int LENGTH_PAN_ID = 2;
@@ -344,86 +343,6 @@
outputStream.write(entries, 0, entries.length);
}
- /**
- * Creates a new {@link ActiveOperationalDataset} object with randomized or default parameters.
- *
- * <p>The randomized (or default) value for each parameter:
- *
- * <ul>
- * <li>{@code Active Timestamp} defaults to {@code new OperationalDatasetTimestamp(1, 0,
- * false)}
- * <li>{@code Network Name} defaults to "THREAD-PAN-<PAN ID decimal>", for example
- * "THREAD-PAN-12345"
- * <li>{@code Extended PAN ID} filled with randomly generated bytes
- * <li>{@code PAN ID} randomly generated integer in range of [0, 0xfffe]
- * <li>{@code Channel Page} defaults to {@link #CHANNEL_PAGE_24_GHZ}
- * <li>{@code Channel} randomly selected channel in range of [{@link #CHANNEL_MIN_24_GHZ},
- * {@link #CHANNEL_MAX_24_GHZ}]
- * <li>{@code Channel Mask} all bits from {@link #CHANNEL_MIN_24_GHZ} to {@link
- * #CHANNEL_MAX_24_GHZ} are set to {@code true}
- * <li>{@code PSKc} filled with bytes generated by secure random generator
- * <li>{@code Network Key} filled with bytes generated by secure random generator
- * <li>{@code Mesh-local Prefix} filled with randomly generated bytes except that the first
- * byte is always set to {@code 0xfd}
- * <li>{@code Security Policy} defaults to {@code new SecurityPolicy(
- * DEFAULT_ROTATION_TIME_HOURS, new byte[]{(byte)0xff, (byte)0xf8})}. This is the default
- * values required by the Thread 1.2 specification
- * </ul>
- *
- * <p>This method is the recommended way to create a randomized operational dataset for a new
- * Thread network. It may be desired to change one or more of the generated value(s). For
- * example, to use a more meaningful Network Name. To do that, create a new {@link Builder}
- * object from this dataset with {@link Builder#Builder(ActiveOperationalDataset)} and override
- * the value with the setters of {@link Builder}.
- *
- * <p>Note that it's highly discouraged to change the randomly generated Extended PAN ID,
- * Network Key or PSKc, as it will compromise the security of a Thread network.
- */
- @NonNull
- public static ActiveOperationalDataset createRandomDataset() {
- return createRandomDataset(new Random(Instant.now().toEpochMilli()), new SecureRandom());
- }
-
- /** @hide */
- @VisibleForTesting
- public static ActiveOperationalDataset createRandomDataset(
- Random random, SecureRandom secureRandom) {
- int panId = random.nextInt(/* bound= */ 0xffff);
- byte[] meshLocalPrefix = newRandomBytes(random, LENGTH_MESH_LOCAL_PREFIX_BITS / 8);
- meshLocalPrefix[0] = MESH_LOCAL_PREFIX_FIRST_BYTE;
-
- SparseArray<byte[]> channelMask = new SparseArray<>(1);
- channelMask.put(CHANNEL_PAGE_24_GHZ, new byte[] {0x00, 0x1f, (byte) 0xff, (byte) 0xe0});
-
- return new Builder()
- .setActiveTimestamp(
- new OperationalDatasetTimestamp(
- /* seconds= */ 1,
- /* ticks= */ 0,
- /* isAuthoritativeSource= */ false))
- .setExtendedPanId(newRandomBytes(random, LENGTH_EXTENDED_PAN_ID))
- .setPanId(panId)
- .setNetworkName("THREAD-PAN-" + panId)
- .setChannel(
- CHANNEL_PAGE_24_GHZ,
- random.nextInt(CHANNEL_MAX_24_GHZ - CHANNEL_MIN_24_GHZ + 1)
- + CHANNEL_MIN_24_GHZ)
- .setChannelMask(channelMask)
- .setPskc(newRandomBytes(secureRandom, LENGTH_PSKC))
- .setNetworkKey(newRandomBytes(secureRandom, LENGTH_NETWORK_KEY))
- .setMeshLocalPrefix(meshLocalPrefix)
- .setSecurityPolicy(
- new SecurityPolicy(
- DEFAULT_ROTATION_TIME_HOURS, new byte[] {(byte) 0xff, (byte) 0xf8}))
- .build();
- }
-
- private static byte[] newRandomBytes(Random random, int length) {
- byte[] result = new byte[length];
- random.nextBytes(result);
- return result;
- }
-
private static boolean areByteSparseArraysEqual(
@NonNull SparseArray<byte[]> first, @NonNull SparseArray<byte[]> second) {
if (first == second) {
@@ -683,6 +602,20 @@
return sb.toString();
}
+ static String checkNetworkName(@NonNull String networkName) {
+ requireNonNull(networkName, "networkName cannot be null");
+
+ int nameLength = networkName.getBytes(UTF_8).length;
+ checkArgument(
+ nameLength >= LENGTH_MIN_NETWORK_NAME_BYTES
+ && nameLength <= LENGTH_MAX_NETWORK_NAME_BYTES,
+ "Invalid network name (length = %d, expectedLengthRange = [%d, %d])",
+ nameLength,
+ LENGTH_MIN_NETWORK_NAME_BYTES,
+ LENGTH_MAX_NETWORK_NAME_BYTES);
+ return networkName;
+ }
+
/** The builder for creating {@link ActiveOperationalDataset} objects. */
public static final class Builder {
private OperationalDatasetTimestamp mActiveTimestamp;
@@ -748,7 +681,7 @@
* @param networkName the name of the Thread network
* @throws IllegalArgumentException if length of the UTF-8 representation of {@code
* networkName} isn't in range of [{@link #LENGTH_MIN_NETWORK_NAME_BYTES}, {@link
- * #LENGTH_MAX_NETWORK_NAME_BYTES}].
+ * #LENGTH_MAX_NETWORK_NAME_BYTES}]
*/
@NonNull
public Builder setNetworkName(
@@ -757,26 +690,16 @@
min = LENGTH_MIN_NETWORK_NAME_BYTES,
max = LENGTH_MAX_NETWORK_NAME_BYTES)
String networkName) {
- requireNonNull(networkName, "networkName cannot be null");
-
- int nameLength = networkName.getBytes(UTF_8).length;
- checkArgument(
- nameLength >= LENGTH_MIN_NETWORK_NAME_BYTES
- && nameLength <= LENGTH_MAX_NETWORK_NAME_BYTES,
- "Invalid network name (length = %d, expectedLengthRange = [%d, %d])",
- nameLength,
- LENGTH_MIN_NETWORK_NAME_BYTES,
- LENGTH_MAX_NETWORK_NAME_BYTES);
- this.mNetworkName = networkName;
+ this.mNetworkName = checkNetworkName(networkName);
return this;
}
/**
* Sets the Extended PAN ID.
*
- * <p>Use with caution. A randomly generated Extended PAN ID should be used for real Thread
+ * <p>Use with caution. A randomized Extended PAN ID should be used for real Thread
* networks. It's discouraged to call this method to override the default value created by
- * {@link ActiveOperationalDataset#createRandomDataset} in production.
+ * {@link ThreadNetworkController#createRandomizedDataset} in production.
*
* @throws IllegalArgumentException if length of {@code extendedPanId} is not {@link
* #LENGTH_EXTENDED_PAN_ID}.
@@ -867,7 +790,7 @@
*
* <p>Use with caution. A randomly generated PSKc should be used for real Thread networks.
* It's discouraged to call this method to override the default value created by {@link
- * ActiveOperationalDataset#createRandomDataset} in production.
+ * ThreadNetworkController#createRandomizedDataset} in production.
*
* @param pskc the key stretched version of the Commissioning Credential for the network
* @throws IllegalArgumentException if length of {@code pskc} is not {@link #LENGTH_PSKC}
@@ -889,7 +812,7 @@
*
* <p>Use with caution, randomly generated Network Key should be used for real Thread
* networks. It's discouraged to call this method to override the default value created by
- * {@link ActiveOperationalDataset#createRandomDataset} in production.
+ * {@link ThreadNetworkController#createRandomizedDataset} in production.
*
* @param networkKey a 128-bit security key-derivation key for the Thread Network
* @throws IllegalArgumentException if length of {@code networkKey} is not {@link
@@ -930,8 +853,16 @@
return this;
}
+ /**
+ * Sets the Mesh-Local Prefix.
+ *
+ * @param meshLocalPrefix the prefix used for realm-local traffic within the mesh
+ * @throws IllegalArgumentException if {@code meshLocalPrefix} doesn't start with {@code
+ * 0xfd} or has length other than {@code LENGTH_MESH_LOCAL_PREFIX_BITS / 8}
+ * @hide
+ */
@NonNull
- private Builder setMeshLocalPrefix(byte[] meshLocalPrefix) {
+ public Builder setMeshLocalPrefix(byte[] meshLocalPrefix) {
final int prefixLength = meshLocalPrefix.length * 8;
checkArgument(
prefixLength == LENGTH_MESH_LOCAL_PREFIX_BITS,
diff --git a/thread/framework/java/android/net/thread/IThreadNetworkController.aidl b/thread/framework/java/android/net/thread/IThreadNetworkController.aidl
index 0e62b0b..51e4d88 100644
--- a/thread/framework/java/android/net/thread/IThreadNetworkController.aidl
+++ b/thread/framework/java/android/net/thread/IThreadNetworkController.aidl
@@ -40,4 +40,5 @@
void leave(in IOperationReceiver receiver);
int getThreadVersion();
+ void createRandomizedDataset(String networkName, IActiveOperationalDatasetReceiver receiver);
}
diff --git a/thread/framework/java/android/net/thread/PendingOperationalDataset.java b/thread/framework/java/android/net/thread/PendingOperationalDataset.java
index 4762d7f..9cfd0b8 100644
--- a/thread/framework/java/android/net/thread/PendingOperationalDataset.java
+++ b/thread/framework/java/android/net/thread/PendingOperationalDataset.java
@@ -69,7 +69,15 @@
@NonNull private final OperationalDatasetTimestamp mPendingTimestamp;
@NonNull private final Duration mDelayTimer;
- /** Creates a new {@link PendingOperationalDataset} object. */
+ /**
+ * Creates a new {@link PendingOperationalDataset} object.
+ *
+ * @param activeOpDataset the included Active Operational Dataset
+ * @param pendingTimestamp the Pending Timestamp which represents the version of this Pending
+ * Dataset
+ * @param delayTimer the delay after when {@code activeOpDataset} will be committed on this
+ * device
+ */
public PendingOperationalDataset(
@NonNull ActiveOperationalDataset activeOpDataset,
@NonNull OperationalDatasetTimestamp pendingTimestamp,
diff --git a/thread/framework/java/android/net/thread/ThreadNetworkController.java b/thread/framework/java/android/net/thread/ThreadNetworkController.java
index 9d6a257..ec39db4 100644
--- a/thread/framework/java/android/net/thread/ThreadNetworkController.java
+++ b/thread/framework/java/android/net/thread/ThreadNetworkController.java
@@ -113,6 +113,33 @@
}
}
+ /**
+ * Creates a new Active Operational Dataset with randomized parameters.
+ *
+ * <p>This method is the recommended way to create a randomized dataset which can be used with
+ * {@link #join} to securely join this device to the specified network . It's highly discouraged
+ * to change the randomly generated Extended PAN ID, Network Key or PSKc, as it will compromise
+ * the security of a Thread network.
+ *
+ * @throws IllegalArgumentException if length of the UTF-8 representation of {@code networkName}
+ * isn't in range of [{@link #LENGTH_MIN_NETWORK_NAME_BYTES}, {@link
+ * #LENGTH_MAX_NETWORK_NAME_BYTES}]
+ */
+ public void createRandomizedDataset(
+ @NonNull String networkName,
+ @NonNull @CallbackExecutor Executor executor,
+ @NonNull OutcomeReceiver<ActiveOperationalDataset, ThreadNetworkException> receiver) {
+ ActiveOperationalDataset.checkNetworkName(networkName);
+ requireNonNull(executor, "executor cannot be null");
+ requireNonNull(receiver, "receiver cannot be null");
+ try {
+ mControllerService.createRandomizedDataset(
+ networkName, new ActiveDatasetReceiverProxy(executor, receiver));
+ } catch (RemoteException e) {
+ e.rethrowFromSystemServer();
+ }
+ }
+
/** Returns {@code true} if {@code deviceRole} indicates an attached state. */
public static boolean isAttached(@DeviceRole int deviceRole) {
return deviceRole == DEVICE_ROLE_CHILD
@@ -449,6 +476,29 @@
executor.execute(() -> receiver.onError(new ThreadNetworkException(errorCode, errorMsg)));
}
+ private static final class ActiveDatasetReceiverProxy
+ extends IActiveOperationalDatasetReceiver.Stub {
+ final Executor mExecutor;
+ final OutcomeReceiver<ActiveOperationalDataset, ThreadNetworkException> mResultReceiver;
+
+ ActiveDatasetReceiverProxy(
+ @CallbackExecutor Executor executor,
+ OutcomeReceiver<ActiveOperationalDataset, ThreadNetworkException> resultReceiver) {
+ this.mExecutor = executor;
+ this.mResultReceiver = resultReceiver;
+ }
+
+ @Override
+ public void onSuccess(ActiveOperationalDataset dataset) {
+ mExecutor.execute(() -> mResultReceiver.onResult(dataset));
+ }
+
+ @Override
+ public void onError(int errorCode, String errorMessage) {
+ propagateError(mExecutor, mResultReceiver, errorCode, errorMessage);
+ }
+ }
+
private static final class OperationReceiverProxy extends IOperationReceiver.Stub {
final Executor mExecutor;
final OutcomeReceiver<Void, ThreadNetworkException> mResultReceiver;
diff --git a/thread/service/java/com/android/server/thread/InfraInterfaceController.java b/thread/service/java/com/android/server/thread/InfraInterfaceController.java
new file mode 100644
index 0000000..d7c49a0
--- /dev/null
+++ b/thread/service/java/com/android/server/thread/InfraInterfaceController.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.thread;
+
+import android.os.ParcelFileDescriptor;
+
+import java.io.IOException;
+
+/** Controller for the infrastructure network interface. */
+public class InfraInterfaceController {
+ private static final String TAG = "InfraIfController";
+
+ static {
+ System.loadLibrary("service-thread-jni");
+ }
+
+ /**
+ * Creates a socket on the infrastructure network interface for sending/receiving ICMPv6
+ * Neighbor Discovery messages.
+ *
+ * @param infraInterfaceName the infrastructure network interface name.
+ * @return an ICMPv6 socket file descriptor on the Infrastructure network interface.
+ * @throws IOException when fails to create the socket.
+ */
+ public static ParcelFileDescriptor createIcmp6Socket(String infraInterfaceName)
+ throws IOException {
+ return ParcelFileDescriptor.adoptFd(nativeCreateIcmp6Socket(infraInterfaceName));
+ }
+
+ private static native int nativeCreateIcmp6Socket(String interfaceName) throws IOException;
+}
diff --git a/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java b/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java
index f5ebc90..8435912 100644
--- a/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java
+++ b/thread/service/java/com/android/server/thread/ThreadNetworkControllerService.java
@@ -14,6 +14,13 @@
package com.android.server.thread;
+import static android.net.thread.ActiveOperationalDataset.CHANNEL_PAGE_24_GHZ;
+import static android.net.thread.ActiveOperationalDataset.LENGTH_EXTENDED_PAN_ID;
+import static android.net.thread.ActiveOperationalDataset.LENGTH_MESH_LOCAL_PREFIX_BITS;
+import static android.net.thread.ActiveOperationalDataset.LENGTH_NETWORK_KEY;
+import static android.net.thread.ActiveOperationalDataset.LENGTH_PSKC;
+import static android.net.thread.ActiveOperationalDataset.MESH_LOCAL_PREFIX_FIRST_BYTE;
+import static android.net.thread.ActiveOperationalDataset.SecurityPolicy.DEFAULT_ROTATION_TIME_HOURS;
import static android.net.thread.ThreadNetworkController.DEVICE_ROLE_DETACHED;
import static android.net.thread.ThreadNetworkController.THREAD_VERSION_1_3;
import static android.net.thread.ThreadNetworkException.ERROR_ABORTED;
@@ -52,10 +59,13 @@
import android.net.NetworkProvider;
import android.net.NetworkScore;
import android.net.thread.ActiveOperationalDataset;
+import android.net.thread.ActiveOperationalDataset.SecurityPolicy;
+import android.net.thread.IActiveOperationalDatasetReceiver;
import android.net.thread.IOperationReceiver;
import android.net.thread.IOperationalDatasetCallback;
import android.net.thread.IStateCallback;
import android.net.thread.IThreadNetworkController;
+import android.net.thread.OperationalDatasetTimestamp;
import android.net.thread.PendingOperationalDataset;
import android.net.thread.ThreadNetworkController;
import android.net.thread.ThreadNetworkController.DeviceRole;
@@ -66,6 +76,7 @@
import android.os.RemoteException;
import android.os.SystemClock;
import android.util.Log;
+import android.util.SparseArray;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.ServiceManagerWrapper;
@@ -78,9 +89,12 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.security.SecureRandom;
+import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
+import java.util.Random;
import java.util.function.Supplier;
/**
@@ -112,6 +126,9 @@
private final LinkProperties mLinkProperties = new LinkProperties();
private final OtDaemonCallbackProxy mOtDaemonCallbackProxy = new OtDaemonCallbackProxy();
+ // TODO(b/308310823): read supported channel from Thread dameon
+ private final int mSupportedChannelMask = 0x07FFF800; // from channel 11 to 26
+
private IOtDaemon mOtDaemon;
private NetworkAgent mNetworkAgent;
@@ -305,6 +322,93 @@
return THREAD_VERSION_1_3;
}
+ @Override
+ public void createRandomizedDataset(
+ String networkName, IActiveOperationalDatasetReceiver receiver) {
+ mHandler.post(
+ () -> {
+ ActiveOperationalDataset dataset =
+ createRandomizedDatasetInternal(
+ networkName,
+ mSupportedChannelMask,
+ Instant.now(),
+ new Random(),
+ new SecureRandom());
+ try {
+ receiver.onSuccess(dataset);
+ } catch (RemoteException e) {
+ // The client is dead, do nothing
+ }
+ });
+ }
+
+ private static ActiveOperationalDataset createRandomizedDatasetInternal(
+ String networkName,
+ int supportedChannelMask,
+ Instant now,
+ Random random,
+ SecureRandom secureRandom) {
+ int panId = random.nextInt(/* bound= */ 0xffff);
+ final byte[] meshLocalPrefix = newRandomBytes(random, LENGTH_MESH_LOCAL_PREFIX_BITS / 8);
+ meshLocalPrefix[0] = MESH_LOCAL_PREFIX_FIRST_BYTE;
+
+ final SparseArray<byte[]> channelMask = new SparseArray<>(1);
+ channelMask.put(CHANNEL_PAGE_24_GHZ, channelMaskToByteArray(supportedChannelMask));
+
+ final byte[] securityFlags = new byte[] {(byte) 0xff, (byte) 0xf8};
+
+ return new ActiveOperationalDataset.Builder()
+ .setActiveTimestamp(
+ new OperationalDatasetTimestamp(
+ now.getEpochSecond() & 0xffffffffffffL, 0, false))
+ .setExtendedPanId(newRandomBytes(random, LENGTH_EXTENDED_PAN_ID))
+ .setPanId(panId)
+ .setNetworkName(networkName)
+ .setChannel(CHANNEL_PAGE_24_GHZ, selectRandomChannel(supportedChannelMask, random))
+ .setChannelMask(channelMask)
+ .setPskc(newRandomBytes(secureRandom, LENGTH_PSKC))
+ .setNetworkKey(newRandomBytes(secureRandom, LENGTH_NETWORK_KEY))
+ .setMeshLocalPrefix(meshLocalPrefix)
+ .setSecurityPolicy(new SecurityPolicy(DEFAULT_ROTATION_TIME_HOURS, securityFlags))
+ .build();
+ }
+
+ private static byte[] newRandomBytes(Random random, int length) {
+ byte[] result = new byte[length];
+ random.nextBytes(result);
+ return result;
+ }
+
+ private static byte[] channelMaskToByteArray(int channelMask) {
+ // Per Thread spec, a Channel Mask is:
+ // A variable-length bit mask that identifies the channels within the channel page
+ // (1 = selected, 0 = unselected). The channels are represented in most significant bit
+ // order. For example, the most significant bit of the left-most byte indicates channel 0.
+ // If channel 0 and channel 10 are selected, the mask would be: 80 20 00 00. For IEEE
+ // 802.15.4-2006 2.4 GHz PHY, the ChannelMask is 27 bits and MaskLength is 4.
+ //
+ // The pass-in channelMask represents a channel K by (channelMask & (1 << K)), so here
+ // needs to do bit-wise reverse to convert it to the Thread spec format in bytes.
+ channelMask = Integer.reverse(channelMask);
+ return new byte[] {
+ (byte) (channelMask >>> 24),
+ (byte) (channelMask >>> 16),
+ (byte) (channelMask >>> 8),
+ (byte) channelMask
+ };
+ }
+
+ private static int selectRandomChannel(int supportedChannelMask, Random random) {
+ int num = random.nextInt(Integer.bitCount(supportedChannelMask));
+ for (int i = 0; i < 32; i++) {
+ if ((supportedChannelMask & 1) == 1 && (num--) == 0) {
+ return i;
+ }
+ supportedChannelMask >>>= 1;
+ }
+ return -1;
+ }
+
private void enforceAllCallingPermissionsGranted(String... permissions) {
for (String permission : permissions) {
mContext.enforceCallingPermission(
diff --git a/thread/service/jni/com_android_server_thread_InfraInterfaceController.cpp b/thread/service/jni/com_android_server_thread_InfraInterfaceController.cpp
new file mode 100644
index 0000000..5d24eab
--- /dev/null
+++ b/thread/service/jni/com_android_server_thread_InfraInterfaceController.cpp
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "jniThreadInfra"
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ifaddrs.h>
+#include <inttypes.h>
+#include <linux/if_arp.h>
+#include <linux/ioctl.h>
+#include <log/log.h>
+#include <net/if.h>
+#include <netdb.h>
+#include <netinet/icmp6.h>
+#include <netinet/in.h>
+#include <private/android_filesystem_config.h>
+#include <signal.h>
+#include <spawn.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "jni.h"
+#include "nativehelper/JNIHelp.h"
+#include "nativehelper/scoped_utf_chars.h"
+
+namespace android {
+static jint
+com_android_server_thread_InfraInterfaceController_createIcmp6Socket(JNIEnv *env, jobject clazz,
+ jstring interfaceName) {
+ ScopedUtfChars ifName(env, interfaceName);
+
+ struct icmp6_filter filter;
+ constexpr int kEnable = 1;
+ constexpr int kIpv6ChecksumOffset = 2;
+ constexpr int kHopLimit = 255;
+
+ // Initializes the ICMPv6 socket.
+ int sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+ if (sock == -1) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to create the socket (%s)",
+ strerror(errno));
+ return -1;
+ }
+
+ // Only accept Router Advertisements, Router Solicitations and Neighbor
+ // Advertisements.
+ ICMP6_FILTER_SETBLOCKALL(&filter);
+ ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filter);
+ ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filter);
+ ICMP6_FILTER_SETPASS(ND_NEIGHBOR_ADVERT, &filter);
+
+ if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof(filter)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt ICMP6_FILTER (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ // We want a source address and interface index.
+
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &kEnable, sizeof(kEnable)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt IPV6_RECVPKTINFO (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ if (setsockopt(sock, IPPROTO_RAW, IPV6_CHECKSUM, &kIpv6ChecksumOffset,
+ sizeof(kIpv6ChecksumOffset)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt IPV6_CHECKSUM (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ // We need to be able to reject RAs arriving from off-link.
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &kEnable, sizeof(kEnable)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt IPV6_RECVHOPLIMIT (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &kHopLimit, sizeof(kHopLimit)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt IPV6_UNICAST_HOPS (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &kHopLimit, sizeof(kHopLimit)) != 0) {
+ jniThrowExceptionFmt(env, "java/io/IOException",
+ "failed to create the setsockopt IPV6_MULTICAST_HOPS (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, ifName.c_str(), strlen(ifName.c_str()))) {
+ jniThrowExceptionFmt(env, "java/io/IOException", "failed to setsockopt SO_BINDTODEVICE (%s)",
+ strerror(errno));
+ close(sock);
+ return -1;
+ }
+
+ return sock;
+}
+
+/*
+ * JNI registration.
+ */
+
+static const JNINativeMethod gMethods[] = {
+ /* name, signature, funcPtr */
+ {"nativeCreateIcmp6Socket", "(Ljava/lang/String;)I",
+ (void *)com_android_server_thread_InfraInterfaceController_createIcmp6Socket},
+};
+
+int register_com_android_server_thread_InfraInterfaceController(JNIEnv *env) {
+ return jniRegisterNativeMethods(env, "com/android/server/thread/InfraInterfaceController",
+ gMethods, NELEM(gMethods));
+}
+
+}; // namespace android
diff --git a/thread/service/jni/onload.cpp b/thread/service/jni/onload.cpp
index 5081664..66add74 100644
--- a/thread/service/jni/onload.cpp
+++ b/thread/service/jni/onload.cpp
@@ -19,6 +19,7 @@
namespace android {
int register_com_android_server_thread_TunInterfaceController(JNIEnv* env);
+int register_com_android_server_thread_InfraInterfaceController(JNIEnv* env);
}
using namespace android;
@@ -33,5 +34,6 @@
ALOG_ASSERT(env != NULL, "Could not retrieve the env!");
register_com_android_server_thread_TunInterfaceController(env);
+ register_com_android_server_thread_InfraInterfaceController(env);
return JNI_VERSION_1_4;
}
diff --git a/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java b/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java
index 39df21b..0e76930 100644
--- a/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java
+++ b/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java
@@ -70,7 +70,7 @@
// PAN ID: 0xD9A0
// PSKc: A245479C836D551B9CA557F7B9D351B4
// Security Policy: 672 onrcb
- private static final byte[] VALID_DATASET =
+ private static final byte[] VALID_DATASET_TLVS =
base16().decode(
"0E080000000000010000000300001335060004001FFFE002"
+ "08ACC214689BC40BDF0708FD64DB1225F47E0B0510F26B31"
@@ -78,6 +78,9 @@
+ "642D643961300102D9A00410A245479C836D551B9CA557F7"
+ "B9D351B40C0402A0FFF8");
+ private static final ActiveOperationalDataset DEFAULT_DATASET =
+ ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET_TLVS);
+
private static byte[] removeTlv(byte[] dataset, int type) {
ByteArrayOutputStream os = new ByteArrayOutputStream(dataset.length);
int i = 0;
@@ -105,7 +108,8 @@
@Test
public void parcelable_parcelingIsLossLess() {
- ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET);
+ ActiveOperationalDataset dataset =
+ ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET_TLVS);
assertParcelingIsLossless(dataset);
}
@@ -126,7 +130,8 @@
@Test
public void fromThreadTlvs_invalidNetworkKeyTlv_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_NETWORK_KEY, "05080000000000000000");
+ byte[] invalidTlv =
+ replaceTlv(VALID_DATASET_TLVS, TYPE_NETWORK_KEY, "05080000000000000000");
assertThrows(
IllegalArgumentException.class,
@@ -135,7 +140,7 @@
@Test
public void fromThreadTlvs_noNetworkKeyTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_NETWORK_KEY);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_NETWORK_KEY);
assertThrows(
IllegalArgumentException.class,
@@ -144,7 +149,8 @@
@Test
public void fromThreadTlvs_invalidActiveTimestampTlv_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_ACTIVE_TIMESTAMP, "0E0700000000010000");
+ byte[] invalidTlv =
+ replaceTlv(VALID_DATASET_TLVS, TYPE_ACTIVE_TIMESTAMP, "0E0700000000010000");
assertThrows(
IllegalArgumentException.class,
@@ -153,7 +159,7 @@
@Test
public void fromThreadTlvs_noActiveTimestampTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_ACTIVE_TIMESTAMP);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_ACTIVE_TIMESTAMP);
assertThrows(
IllegalArgumentException.class,
@@ -162,7 +168,7 @@
@Test
public void fromThreadTlvs_invalidNetworkNameTlv_emptyName_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_NETWORK_NAME, "0300");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_NETWORK_NAME, "0300");
assertThrows(
IllegalArgumentException.class,
@@ -173,7 +179,9 @@
public void fromThreadTlvs_invalidNetworkNameTlv_tooLongName_throwsIllegalArgument() {
byte[] invalidTlv =
replaceTlv(
- VALID_DATASET, TYPE_NETWORK_NAME, "03114142434445464748494A4B4C4D4E4F5051");
+ VALID_DATASET_TLVS,
+ TYPE_NETWORK_NAME,
+ "03114142434445464748494A4B4C4D4E4F5051");
assertThrows(
IllegalArgumentException.class,
@@ -182,7 +190,7 @@
@Test
public void fromThreadTlvs_noNetworkNameTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_NETWORK_NAME);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_NETWORK_NAME);
assertThrows(
IllegalArgumentException.class,
@@ -191,7 +199,7 @@
@Test
public void fromThreadTlvs_invalidChannelTlv_channelMissing_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000100");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL, "000100");
assertThrows(
IllegalArgumentException.class,
@@ -200,7 +208,7 @@
@Test
public void fromThreadTlvs_undefinedChannelPage_success() {
- byte[] datasetTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "0003010020");
+ byte[] datasetTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL, "0003010020");
ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(datasetTlv);
@@ -210,8 +218,8 @@
@Test
public void fromThreadTlvs_invalid2P4GhzChannel_throwsIllegalArgument() {
- byte[] invalidTlv1 = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000300000A");
- byte[] invalidTlv2 = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000300001B");
+ byte[] invalidTlv1 = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL, "000300000A");
+ byte[] invalidTlv2 = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL, "000300001B");
assertThrows(
IllegalArgumentException.class,
@@ -223,7 +231,7 @@
@Test
public void fromThreadTlvs_valid2P4GhzChannelTlv_success() {
- byte[] validTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "0003000010");
+ byte[] validTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL, "0003000010");
ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(validTlv);
@@ -232,7 +240,7 @@
@Test
public void fromThreadTlvs_noChannelTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_CHANNEL);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_CHANNEL);
assertThrows(
IllegalArgumentException.class,
@@ -241,7 +249,7 @@
@Test
public void fromThreadTlvs_prematureEndOfChannelMaskEntry_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "350100");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL_MASK, "350100");
assertThrows(
IllegalArgumentException.class,
@@ -250,7 +258,7 @@
@Test
public void fromThreadTlvs_inconsistentChannelMaskLength_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "3506000500010000");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL_MASK, "3506000500010000");
assertThrows(
IllegalArgumentException.class,
@@ -261,7 +269,7 @@
public void fromThreadTlvs_unsupportedChannelMaskLength_success() {
ActiveOperationalDataset dataset =
ActiveOperationalDataset.fromThreadTlvs(
- replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "350700050001000000"));
+ replaceTlv(VALID_DATASET_TLVS, TYPE_CHANNEL_MASK, "350700050001000000"));
SparseArray<byte[]> channelMask = dataset.getChannelMask();
assertThat(channelMask.size()).isEqualTo(1);
@@ -271,7 +279,7 @@
@Test
public void fromThreadTlvs_noChannelMaskTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_CHANNEL_MASK);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_CHANNEL_MASK);
assertThrows(
IllegalArgumentException.class,
@@ -280,7 +288,7 @@
@Test
public void fromThreadTlvs_invalidPanIdTlv_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_PAN_ID, "010101");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_PAN_ID, "010101");
assertThrows(
IllegalArgumentException.class,
@@ -289,7 +297,7 @@
@Test
public void fromThreadTlvs_noPanIdTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_PAN_ID);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_PAN_ID);
assertThrows(
IllegalArgumentException.class,
@@ -298,7 +306,8 @@
@Test
public void fromThreadTlvs_invalidExtendedPanIdTlv_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_EXTENDED_PAN_ID, "020700010203040506");
+ byte[] invalidTlv =
+ replaceTlv(VALID_DATASET_TLVS, TYPE_EXTENDED_PAN_ID, "020700010203040506");
assertThrows(
IllegalArgumentException.class,
@@ -307,7 +316,7 @@
@Test
public void fromThreadTlvs_noExtendedPanIdTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_EXTENDED_PAN_ID);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_EXTENDED_PAN_ID);
assertThrows(
IllegalArgumentException.class,
@@ -317,7 +326,7 @@
@Test
public void fromThreadTlvs_invalidPskcTlv_throwsIllegalArgument() {
byte[] invalidTlv =
- replaceTlv(VALID_DATASET, TYPE_PSKC, "0411000102030405060708090A0B0C0D0E0F10");
+ replaceTlv(VALID_DATASET_TLVS, TYPE_PSKC, "0411000102030405060708090A0B0C0D0E0F10");
assertThrows(
IllegalArgumentException.class,
@@ -326,7 +335,7 @@
@Test
public void fromThreadTlvs_noPskcTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_PSKC);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_PSKC);
assertThrows(
IllegalArgumentException.class,
@@ -336,7 +345,7 @@
@Test
public void fromThreadTlvs_invalidMeshLocalPrefixTlv_throwsIllegalArgument() {
byte[] invalidTlv =
- replaceTlv(VALID_DATASET, TYPE_MESH_LOCAL_PREFIX, "0709FD0001020304050607");
+ replaceTlv(VALID_DATASET_TLVS, TYPE_MESH_LOCAL_PREFIX, "0709FD0001020304050607");
assertThrows(
IllegalArgumentException.class,
@@ -345,7 +354,7 @@
@Test
public void fromThreadTlvs_noMeshLocalPrefixTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_MESH_LOCAL_PREFIX);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_MESH_LOCAL_PREFIX);
assertThrows(
IllegalArgumentException.class,
@@ -354,7 +363,7 @@
@Test
public void fromThreadTlvs_tooShortSecurityPolicyTlv_throwsIllegalArgument() {
- byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_SECURITY_POLICY, "0C0101");
+ byte[] invalidTlv = replaceTlv(VALID_DATASET_TLVS, TYPE_SECURITY_POLICY, "0C0101");
assertThrows(
IllegalArgumentException.class,
@@ -363,7 +372,7 @@
@Test
public void fromThreadTlvs_noSecurityPolicyTlv_throwsIllegalArgument() {
- byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_SECURITY_POLICY);
+ byte[] invalidTlv = removeTlv(VALID_DATASET_TLVS, TYPE_SECURITY_POLICY);
assertThrows(
IllegalArgumentException.class,
@@ -429,7 +438,7 @@
@Test
public void fromThreadTlvs_containsUnknownTlvs_unknownTlvsRetained() {
- final byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET, "AA01FFBB020102");
+ final byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET_TLVS, "AA01FFBB020102");
ActiveOperationalDataset dataset =
ActiveOperationalDataset.fromThreadTlvs(datasetWithUnknownTlvs);
@@ -443,7 +452,7 @@
@Test
public void toThreadTlvs_conversionIsLossLess() {
- ActiveOperationalDataset dataset1 = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset dataset1 = DEFAULT_DATASET;
ActiveOperationalDataset dataset2 =
ActiveOperationalDataset.fromThreadTlvs(dataset1.toThreadTlvs());
@@ -465,9 +474,7 @@
};
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setNetworkKey(networkKey)
- .build();
+ new Builder(DEFAULT_DATASET).setNetworkKey(networkKey).build();
assertThat(dataset.getNetworkKey()).isEqualTo(networkKey);
}
@@ -475,7 +482,7 @@
@Test
public void builder_setInvalidNetworkKey_throwsIllegalArgument() {
byte[] invalidNetworkKey = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(
IllegalArgumentException.class, () -> builder.setNetworkKey(invalidNetworkKey));
@@ -486,9 +493,7 @@
byte[] extendedPanId = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setExtendedPanId(extendedPanId)
- .build();
+ new Builder(DEFAULT_DATASET).setExtendedPanId(extendedPanId).build();
assertThat(dataset.getExtendedPanId()).isEqualTo(extendedPanId);
}
@@ -496,31 +501,28 @@
@Test
public void builder_setInvalidExtendedPanId_throwsIllegalArgument() {
byte[] extendedPanId = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(IllegalArgumentException.class, () -> builder.setExtendedPanId(extendedPanId));
}
@Test
public void builder_setValidPanId_success() {
- ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setPanId(0xfffe)
- .build();
+ ActiveOperationalDataset dataset = new Builder(DEFAULT_DATASET).setPanId(0xfffe).build();
assertThat(dataset.getPanId()).isEqualTo(0xfffe);
}
@Test
public void builder_setInvalidPanId_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(IllegalArgumentException.class, () -> builder.setPanId(0xffff));
}
@Test
public void builder_setInvalidChannel_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(IllegalArgumentException.class, () -> builder.setChannel(0, 0));
assertThrows(IllegalArgumentException.class, () -> builder.setChannel(0, 27));
@@ -529,9 +531,7 @@
@Test
public void builder_setValid2P4GhzChannel_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setChannel(CHANNEL_PAGE_24_GHZ, 16)
- .build();
+ new Builder(DEFAULT_DATASET).setChannel(CHANNEL_PAGE_24_GHZ, 16).build();
assertThat(dataset.getChannel()).isEqualTo(16);
assertThat(dataset.getChannelPage()).isEqualTo(CHANNEL_PAGE_24_GHZ);
@@ -540,23 +540,21 @@
@Test
public void builder_setValidNetworkName_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setNetworkName("ot-network")
- .build();
+ new Builder(DEFAULT_DATASET).setNetworkName("ot-network").build();
assertThat(dataset.getNetworkName()).isEqualTo("ot-network");
}
@Test
public void builder_setEmptyNetworkName_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(IllegalArgumentException.class, () -> builder.setNetworkName(""));
}
@Test
public void builder_setTooLongNetworkName_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(
IllegalArgumentException.class, () -> builder.setNetworkName("openthread-network"));
@@ -564,7 +562,7 @@
@Test
public void builder_setTooLongUtf8NetworkName_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
// UTF-8 encoded length of "我的线程网络" is 18 bytes which exceeds the max length
assertThrows(IllegalArgumentException.class, () -> builder.setNetworkName("我的线程网络"));
@@ -573,9 +571,7 @@
@Test
public void builder_setValidUtf8NetworkName_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setNetworkName("我的网络")
- .build();
+ new Builder(DEFAULT_DATASET).setNetworkName("我的网络").build();
assertThat(dataset.getNetworkName()).isEqualTo("我的网络");
}
@@ -584,8 +580,7 @@
public void builder_setValidPskc_success() {
byte[] pskc = base16().decode("A245479C836D551B9CA557F7B9D351B4");
- ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset()).setPskc(pskc).build();
+ ActiveOperationalDataset dataset = new Builder(DEFAULT_DATASET).setPskc(pskc).build();
assertThat(dataset.getPskc()).isEqualTo(pskc);
}
@@ -593,18 +588,18 @@
@Test
public void builder_setTooLongPskc_throwsIllegalArgument() {
byte[] tooLongPskc = base16().decode("A245479C836D551B9CA557F7B9D351B400");
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(IllegalArgumentException.class, () -> builder.setPskc(tooLongPskc));
}
@Test
public void builder_setValidChannelMask_success() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
SparseArray<byte[]> channelMask = new SparseArray<byte[]>(1);
channelMask.put(0, new byte[] {0x00, 0x00, 0x01, 0x00});
- ActiveOperationalDataset dataset = builder.setChannelMask(channelMask).build();
+ ActiveOperationalDataset dataset =
+ new Builder(DEFAULT_DATASET).setChannelMask(channelMask).build();
SparseArray<byte[]> resultChannelMask = dataset.getChannelMask();
assertThat(resultChannelMask.size()).isEqualTo(1);
@@ -613,7 +608,7 @@
@Test
public void builder_setEmptyChannelMask_throwsIllegalArgument() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(
IllegalArgumentException.class,
@@ -623,7 +618,7 @@
@Test
public void builder_setValidActiveTimestamp_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
+ new Builder(DEFAULT_DATASET)
.setActiveTimestamp(
new OperationalDatasetTimestamp(
/* seconds= */ 1,
@@ -638,7 +633,7 @@
@Test
public void builder_wrongMeshLocalPrefixLength_throwsIllegalArguments() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
// The Mesh-Local Prefix length must be 64 bits
assertThrows(
@@ -656,7 +651,7 @@
@Test
public void builder_meshLocalPrefixNotStartWith0xfd_throwsIllegalArguments() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder();
assertThrows(
IllegalArgumentException.class,
@@ -666,9 +661,7 @@
@Test
public void builder_setValidMeshLocalPrefix_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
- .setMeshLocalPrefix(new IpPrefix("fd00::/64"))
- .build();
+ new Builder(DEFAULT_DATASET).setMeshLocalPrefix(new IpPrefix("fd00::/64")).build();
assertThat(dataset.getMeshLocalPrefix()).isEqualTo(new IpPrefix("fd00::/64"));
}
@@ -676,7 +669,7 @@
@Test
public void builder_setValid1P2SecurityPolicy_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
+ new Builder(DEFAULT_DATASET)
.setSecurityPolicy(
new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}))
.build();
@@ -689,7 +682,7 @@
@Test
public void builder_setValid1P1SecurityPolicy_success() {
ActiveOperationalDataset dataset =
- new Builder(ActiveOperationalDataset.createRandomDataset())
+ new Builder(DEFAULT_DATASET)
.setSecurityPolicy(new SecurityPolicy(672, new byte[] {(byte) 0xff}))
.build();
diff --git a/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java b/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java
index 7a49957..0bb18ce 100644
--- a/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java
+++ b/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java
@@ -25,8 +25,10 @@
import android.net.IpPrefix;
import android.net.thread.ActiveOperationalDataset;
+import android.net.thread.ActiveOperationalDataset.SecurityPolicy;
import android.net.thread.OperationalDatasetTimestamp;
import android.net.thread.PendingOperationalDataset;
+import android.util.SparseArray;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
@@ -37,20 +39,36 @@
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.net.InetAddress;
import java.time.Duration;
/** Tests for {@link PendingOperationalDataset}. */
@SmallTest
@RunWith(AndroidJUnit4.class)
public final class PendingOperationalDatasetTest {
- private static final ActiveOperationalDataset DEFAULT_ACTIVE_DATASET =
- ActiveOperationalDataset.createRandomDataset();
+ private static ActiveOperationalDataset createActiveDataset() throws Exception {
+ SparseArray<byte[]> channelMask = new SparseArray<>(1);
+ channelMask.put(0, new byte[] {0x00, 0x1f, (byte) 0xff, (byte) 0xe0});
+
+ return new ActiveOperationalDataset.Builder()
+ .setActiveTimestamp(new OperationalDatasetTimestamp(100, 10, false))
+ .setExtendedPanId(new byte[] {0, 1, 2, 3, 4, 5, 6, 7})
+ .setPanId(12345)
+ .setNetworkName("defaultNet")
+ .setChannel(0, 18)
+ .setChannelMask(channelMask)
+ .setPskc(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
+ .setNetworkKey(new byte[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})
+ .setMeshLocalPrefix(new IpPrefix(InetAddress.getByName("fd00::1"), 64))
+ .setSecurityPolicy(new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}))
+ .build();
+ }
@Test
- public void parcelable_parcelingIsLossLess() {
+ public void parcelable_parcelingIsLossLess() throws Exception {
PendingOperationalDataset dataset =
new PendingOperationalDataset(
- DEFAULT_ACTIVE_DATASET,
+ createActiveDataset(),
new OperationalDatasetTimestamp(31536000, 200, false),
Duration.ofHours(100));
@@ -58,9 +76,15 @@
}
@Test
- public void equalityTests() {
- ActiveOperationalDataset activeDataset1 = ActiveOperationalDataset.createRandomDataset();
- ActiveOperationalDataset activeDataset2 = ActiveOperationalDataset.createRandomDataset();
+ public void equalityTests() throws Exception {
+ ActiveOperationalDataset activeDataset1 =
+ new ActiveOperationalDataset.Builder(createActiveDataset())
+ .setNetworkName("net1")
+ .build();
+ ActiveOperationalDataset activeDataset2 =
+ new ActiveOperationalDataset.Builder(createActiveDataset())
+ .setNetworkName("net2")
+ .build();
new EqualsTester()
.addEqualityGroup(
@@ -103,14 +127,15 @@
}
@Test
- public void constructor_correctValuesAreSet() {
+ public void constructor_correctValuesAreSet() throws Exception {
+ final ActiveOperationalDataset activeDataset = createActiveDataset();
PendingOperationalDataset dataset =
new PendingOperationalDataset(
- DEFAULT_ACTIVE_DATASET,
+ activeDataset,
new OperationalDatasetTimestamp(31536000, 200, false),
Duration.ofHours(100));
- assertThat(dataset.getActiveOperationalDataset()).isEqualTo(DEFAULT_ACTIVE_DATASET);
+ assertThat(dataset.getActiveOperationalDataset()).isEqualTo(activeDataset);
assertThat(dataset.getPendingTimestamp())
.isEqualTo(new OperationalDatasetTimestamp(31536000, 200, false));
assertThat(dataset.getDelayTimer()).isEqualTo(Duration.ofHours(100));
@@ -166,33 +191,35 @@
}
@Test
- public void fromThreadTlvs_completePendingDatasetTlvs_success() {
+ public void fromThreadTlvs_completePendingDatasetTlvs_success() throws Exception {
+ final ActiveOperationalDataset activeDataset = createActiveDataset();
+
// Type Length Value
// 0x33 0x08 0x0000000000010000 (Pending Timestamp TLV)
// 0x34 0x04 0x0000012C (Delay Timer TLV)
final byte[] pendingTimestampAndDelayTimerTlvs =
base16().decode("3308000000000001000034040000012C");
final byte[] pendingDatasetTlvs =
- Bytes.concat(
- pendingTimestampAndDelayTimerTlvs, DEFAULT_ACTIVE_DATASET.toThreadTlvs());
+ Bytes.concat(pendingTimestampAndDelayTimerTlvs, activeDataset.toThreadTlvs());
PendingOperationalDataset dataset =
PendingOperationalDataset.fromThreadTlvs(pendingDatasetTlvs);
- assertThat(dataset.getActiveOperationalDataset()).isEqualTo(DEFAULT_ACTIVE_DATASET);
+ assertThat(dataset.getActiveOperationalDataset()).isEqualTo(activeDataset);
assertThat(dataset.getPendingTimestamp())
.isEqualTo(new OperationalDatasetTimestamp(1, 0, false));
assertThat(dataset.getDelayTimer()).isEqualTo(Duration.ofMillis(300));
}
@Test
- public void fromThreadTlvs_PendingTimestampTlvIsMissing_throwsIllegalArgument() {
+ public void fromThreadTlvs_PendingTimestampTlvIsMissing_throwsIllegalArgument()
+ throws Exception {
// Type Length Value
// 0x34 0x04 0x00000064 (Delay Timer TLV)
final byte[] pendingTimestampAndDelayTimerTlvs = base16().decode("34040000012C");
final byte[] pendingDatasetTlvs =
Bytes.concat(
- pendingTimestampAndDelayTimerTlvs, DEFAULT_ACTIVE_DATASET.toThreadTlvs());
+ pendingTimestampAndDelayTimerTlvs, createActiveDataset().toThreadTlvs());
assertThrows(
IllegalArgumentException.class,
@@ -200,13 +227,13 @@
}
@Test
- public void fromThreadTlvs_delayTimerTlvIsMissing_throwsIllegalArgument() {
+ public void fromThreadTlvs_delayTimerTlvIsMissing_throwsIllegalArgument() throws Exception {
// Type Length Value
// 0x33 0x08 0x0000000000010000 (Pending Timestamp TLV)
final byte[] pendingTimestampAndDelayTimerTlvs = base16().decode("33080000000000010000");
final byte[] pendingDatasetTlvs =
Bytes.concat(
- pendingTimestampAndDelayTimerTlvs, DEFAULT_ACTIVE_DATASET.toThreadTlvs());
+ pendingTimestampAndDelayTimerTlvs, createActiveDataset().toThreadTlvs());
assertThrows(
IllegalArgumentException.class,
@@ -214,8 +241,8 @@
}
@Test
- public void fromThreadTlvs_activeDatasetTlvs_throwsIllegalArgument() {
- final byte[] activeDatasetTlvs = DEFAULT_ACTIVE_DATASET.toThreadTlvs();
+ public void fromThreadTlvs_activeDatasetTlvs_throwsIllegalArgument() throws Exception {
+ final byte[] activeDatasetTlvs = createActiveDataset().toThreadTlvs();
assertThrows(
IllegalArgumentException.class,
@@ -232,10 +259,10 @@
}
@Test
- public void toThreadTlvs_conversionIsLossLess() {
+ public void toThreadTlvs_conversionIsLossLess() throws Exception {
PendingOperationalDataset dataset1 =
new PendingOperationalDataset(
- DEFAULT_ACTIVE_DATASET,
+ createActiveDataset(),
new OperationalDatasetTimestamp(31536000, 200, false),
Duration.ofHours(100));
diff --git a/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkControllerTest.java b/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkControllerTest.java
index cfe310c..e17dd02 100644
--- a/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkControllerTest.java
+++ b/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkControllerTest.java
@@ -132,6 +132,13 @@
getInstrumentation().getUiAutomation().dropShellPermissionIdentity();
}
+ private static ActiveOperationalDataset newRandomizedDataset(
+ String networkName, ThreadNetworkController controller) throws Exception {
+ SettableFuture<ActiveOperationalDataset> future = SettableFuture.create();
+ controller.createRandomizedDataset(networkName, directExecutor(), future::set);
+ return future.get(CALLBACK_TIMEOUT_MILLIS, MILLISECONDS);
+ }
+
private static boolean isAttached(ThreadNetworkController controller) throws Exception {
return ThreadNetworkController.isAttached(getDeviceRole(controller));
}
@@ -322,7 +329,7 @@
grantPermissions(PERMISSION_THREAD_NETWORK_PRIVILEGED);
for (ThreadNetworkController controller : getAllControllers()) {
- ActiveOperationalDataset activeDataset = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset activeDataset = newRandomizedDataset("TestNet", controller);
SettableFuture<Void> joinFuture = SettableFuture.create();
controller.join(activeDataset, mExecutor, newOutcomeReceiver(joinFuture));
@@ -335,11 +342,11 @@
}
@Test
- public void join_withoutPrivilegedPermission_throwsSecurityException() {
+ public void join_withoutPrivilegedPermission_throwsSecurityException() throws Exception {
dropPermissions();
for (ThreadNetworkController controller : getAllControllers()) {
- ActiveOperationalDataset activeDataset = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset activeDataset = newRandomizedDataset("TestNet", controller);
assertThrows(
SecurityException.class,
@@ -356,7 +363,7 @@
for (ThreadNetworkController controller : getAllControllers()) {
ActiveOperationalDataset activeDataset1 =
new ActiveOperationalDataset.Builder(
- ActiveOperationalDataset.createRandomDataset())
+ newRandomizedDataset("TestNet", controller))
.setNetworkKey(KEY_1)
.build();
ActiveOperationalDataset activeDataset2 =
@@ -385,7 +392,7 @@
grantPermissions(PERMISSION_THREAD_NETWORK_PRIVILEGED);
for (ThreadNetworkController controller : getAllControllers()) {
- ActiveOperationalDataset activeDataset = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset activeDataset = newRandomizedDataset("TestNet", controller);
SettableFuture<Void> joinFuture = SettableFuture.create();
SettableFuture<Void> leaveFuture = SettableFuture.create();
controller.join(activeDataset, mExecutor, newOutcomeReceiver(joinFuture));
@@ -413,7 +420,7 @@
grantPermissions(PERMISSION_THREAD_NETWORK_PRIVILEGED);
for (ThreadNetworkController controller : getAllControllers()) {
- ActiveOperationalDataset activeDataset = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset activeDataset = newRandomizedDataset("TestNet", controller);
SettableFuture<Void> joinFuture = SettableFuture.create();
SettableFuture<Void> leaveFuture1 = SettableFuture.create();
SettableFuture<Void> leaveFuture2 = SettableFuture.create();
@@ -437,7 +444,7 @@
for (ThreadNetworkController controller : getAllControllers()) {
ActiveOperationalDataset activeDataset1 =
new ActiveOperationalDataset.Builder(
- ActiveOperationalDataset.createRandomDataset())
+ newRandomizedDataset("TestNet", controller))
.setActiveTimestamp(new OperationalDatasetTimestamp(1L, 0, false))
.setExtendedPanId(new byte[] {1, 1, 1, 1, 1, 1, 1, 1})
.build();
@@ -473,7 +480,7 @@
for (ThreadNetworkController controller : getAllControllers()) {
PendingOperationalDataset pendingDataset =
new PendingOperationalDataset(
- ActiveOperationalDataset.createRandomDataset(),
+ newRandomizedDataset("TestNet", controller),
OperationalDatasetTimestamp.fromInstant(Instant.now()),
Duration.ofSeconds(30));
SettableFuture<Void> migrateFuture = SettableFuture.create();
@@ -496,9 +503,8 @@
for (ThreadNetworkController controller : getAllControllers()) {
final ActiveOperationalDataset activeDataset =
new ActiveOperationalDataset.Builder(
- ActiveOperationalDataset.createRandomDataset())
+ newRandomizedDataset("testNet", controller))
.setActiveTimestamp(new OperationalDatasetTimestamp(1L, 0, false))
- .setNetworkName("testNet")
.build();
ActiveOperationalDataset activeDataset1 =
new ActiveOperationalDataset.Builder(activeDataset)
@@ -546,9 +552,8 @@
for (ThreadNetworkController controller : getAllControllers()) {
final ActiveOperationalDataset activeDataset =
new ActiveOperationalDataset.Builder(
- ActiveOperationalDataset.createRandomDataset())
+ newRandomizedDataset("validName", controller))
.setActiveTimestamp(new OperationalDatasetTimestamp(1L, 0, false))
- .setNetworkName("testNet")
.build();
ActiveOperationalDataset activeDataset1 =
new ActiveOperationalDataset.Builder(activeDataset)
@@ -588,4 +593,35 @@
assertThat(getPendingOperationalDataset(controller)).isNull();
}
}
+
+ @Test
+ public void createRandomizedDataset_wrongNetworkNameLength_throwsIllegalArgumentException() {
+ for (ThreadNetworkController controller : getAllControllers()) {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> controller.createRandomizedDataset("", mExecutor, dataset -> {}));
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ controller.createRandomizedDataset(
+ "ANetNameIs17Bytes", mExecutor, dataset -> {}));
+ }
+ }
+
+ @Test
+ public void createRandomizedDataset_validNetworkName_success() throws Exception {
+ for (ThreadNetworkController controller : getAllControllers()) {
+ ActiveOperationalDataset dataset = newRandomizedDataset("validName", controller);
+
+ assertThat(dataset.getNetworkName()).isEqualTo("validName");
+ assertThat(dataset.getPanId()).isLessThan(0xffff);
+ assertThat(dataset.getChannelMask().size()).isAtLeast(1);
+ assertThat(dataset.getExtendedPanId()).hasLength(8);
+ assertThat(dataset.getNetworkKey()).hasLength(16);
+ assertThat(dataset.getPskc()).hasLength(16);
+ assertThat(dataset.getMeshLocalPrefix().getPrefixLength()).isEqualTo(64);
+ assertThat(dataset.getMeshLocalPrefix().getRawAddress()[0]).isEqualTo((byte) 0xfd);
+ }
+ }
}
diff --git a/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java b/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java
index 78eb3d0..7284968 100644
--- a/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java
+++ b/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java
@@ -20,14 +20,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import android.net.IpPrefix;
import android.net.thread.ActiveOperationalDataset.Builder;
import android.net.thread.ActiveOperationalDataset.SecurityPolicy;
import android.util.SparseArray;
@@ -61,7 +54,7 @@
// PAN ID: 0xD9A0
// PSKc: A245479C836D551B9CA557F7B9D351B4
// Security Policy: 672 onrcb
- private static final byte[] VALID_DATASET =
+ private static final byte[] VALID_DATASET_TLVS =
base16().decode(
"0E080000000000010000000300001335060004001FFFE002"
+ "08ACC214689BC40BDF0708FD64DB1225F47E0B0510F26B31"
@@ -83,7 +76,7 @@
@Test
public void fromThreadTlvs_containsUnknownTlvs_unknownTlvsRetained() {
- byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET, "AA01FFBB020102");
+ byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET_TLVS, "AA01FFBB020102");
ActiveOperationalDataset dataset1 =
ActiveOperationalDataset.fromThreadTlvs(datasetWithUnknownTlvs);
@@ -98,66 +91,8 @@
}
@Test
- public void createRandomDataset_fieldsAreRandomized() {
- // Always return the max bounded value
- doAnswer(invocation -> (int) invocation.getArgument(0) - 1)
- .when(mockRandom)
- .nextInt(anyInt());
- doAnswer(
- invocation -> {
- byte[] output = invocation.getArgument(0);
- for (int i = 0; i < output.length; ++i) {
- output[i] = (byte) (i + 10);
- }
- return null;
- })
- .when(mockRandom)
- .nextBytes(any(byte[].class));
- doAnswer(
- invocation -> {
- byte[] output = invocation.getArgument(0);
- for (int i = 0; i < output.length; ++i) {
- output[i] = (byte) (i + 30);
- }
- return null;
- })
- .when(mockSecureRandom)
- .nextBytes(any(byte[].class));
-
- ActiveOperationalDataset dataset =
- ActiveOperationalDataset.createRandomDataset(mockRandom, mockSecureRandom);
-
- assertThat(dataset.getActiveTimestamp())
- .isEqualTo(new OperationalDatasetTimestamp(1, 0, false));
- assertThat(dataset.getExtendedPanId())
- .isEqualTo(new byte[] {10, 11, 12, 13, 14, 15, 16, 17});
- assertThat(dataset.getMeshLocalPrefix())
- .isEqualTo(new IpPrefix("fd0b:0c0d:0e0f:1011::/64"));
- verify(mockRandom, times(2)).nextBytes(any(byte[].class));
- assertThat(dataset.getPanId()).isEqualTo(0xfffe); // PAN ID <= 0xfffe
- verify(mockRandom, times(1)).nextInt(eq(0xffff));
- assertThat(dataset.getChannel()).isEqualTo(26);
- verify(mockRandom, times(1)).nextInt(eq(16));
- assertThat(dataset.getChannelPage()).isEqualTo(0);
- assertThat(dataset.getChannelMask().size()).isEqualTo(1);
- assertThat(dataset.getPskc())
- .isEqualTo(
- new byte[] {
- 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45
- });
- assertThat(dataset.getNetworkKey())
- .isEqualTo(
- new byte[] {
- 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45
- });
- verify(mockSecureRandom, times(2)).nextBytes(any(byte[].class));
- assertThat(dataset.getSecurityPolicy())
- .isEqualTo(new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}));
- }
-
- @Test
public void builder_buildWithTooLongTlvs_throwsIllegalState() {
- Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+ Builder builder = new Builder(ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET_TLVS));
for (int i = 0; i < 10; i++) {
builder.addUnknownTlv(i, new byte[20]);
}
@@ -167,7 +102,8 @@
@Test
public void builder_setUnknownTlvs_success() {
- ActiveOperationalDataset dataset1 = ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET);
+ ActiveOperationalDataset dataset1 =
+ ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET_TLVS);
SparseArray<byte[]> unknownTlvs = new SparseArray<>(2);
unknownTlvs.put(0x33, new byte[] {1, 2, 3});
unknownTlvs.put(0x44, new byte[] {1, 2, 3, 4});