[Thread] add Thread Operational Dataset API
Design doc: go/thread-android-api
Android FR: b/235016403
Bug: 262683651
Change-Id: Icbd4ee4150e3fd78df627c2e726c259e7ee50871
diff --git a/thread/tests/cts/Android.bp b/thread/tests/cts/Android.bp
index 278798e..ce770e0 100644
--- a/thread/tests/cts/Android.bp
+++ b/thread/tests/cts/Android.bp
@@ -37,6 +37,7 @@
"androidx.test.ext.junit",
"compatibility-device-util-axt",
"ctstestrunner-axt",
+ "guava-android-testlib",
"net-tests-utils",
"truth",
],
diff --git a/thread/tests/cts/AndroidTest.xml b/thread/tests/cts/AndroidTest.xml
index 5ba605f..ffc181c 100644
--- a/thread/tests/cts/AndroidTest.xml
+++ b/thread/tests/cts/AndroidTest.xml
@@ -47,5 +47,7 @@
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="android.net.thread.cts" />
+ <!-- Ignores tests introduced by guava-android-testlib -->
+ <option name="exclude-annotation" value="org.junit.Ignore"/>
</test>
</configuration>
diff --git a/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java b/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java
new file mode 100644
index 0000000..39df21b
--- /dev/null
+++ b/thread/tests/cts/src/android/net/thread/cts/ActiveOperationalDatasetTest.java
@@ -0,0 +1,737 @@
+/*
+ * 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.thread.cts;
+
+import static android.net.thread.ActiveOperationalDataset.CHANNEL_PAGE_24_GHZ;
+
+import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
+
+import static com.google.common.io.BaseEncoding.base16;
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+
+import android.net.IpPrefix;
+import android.net.thread.ActiveOperationalDataset;
+import android.net.thread.ActiveOperationalDataset.Builder;
+import android.net.thread.ActiveOperationalDataset.SecurityPolicy;
+import android.net.thread.OperationalDatasetTimestamp;
+import android.util.SparseArray;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.google.common.primitives.Bytes;
+import com.google.common.testing.EqualsTester;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Arrays;
+
+/** CTS tests for {@link ActiveOperationalDataset}. */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public final class ActiveOperationalDatasetTest {
+ private static final int TYPE_ACTIVE_TIMESTAMP = 14;
+ private static final int TYPE_CHANNEL = 0;
+ private static final int TYPE_CHANNEL_MASK = 53;
+ private static final int TYPE_EXTENDED_PAN_ID = 2;
+ private static final int TYPE_MESH_LOCAL_PREFIX = 7;
+ private static final int TYPE_NETWORK_KEY = 5;
+ private static final int TYPE_NETWORK_NAME = 3;
+ private static final int TYPE_PAN_ID = 1;
+ private static final int TYPE_PSKC = 4;
+ private static final int TYPE_SECURITY_POLICY = 12;
+
+ // A valid Thread Active Operational Dataset generated from OpenThread CLI "dataset new":
+ // Active Timestamp: 1
+ // Channel: 19
+ // Channel Mask: 0x07FFF800
+ // Ext PAN ID: ACC214689BC40BDF
+ // Mesh Local Prefix: fd64:db12:25f4:7e0b::/64
+ // Network Key: F26B3153760F519A63BAFDDFFC80D2AF
+ // Network Name: OpenThread-d9a0
+ // PAN ID: 0xD9A0
+ // PSKc: A245479C836D551B9CA557F7B9D351B4
+ // Security Policy: 672 onrcb
+ private static final byte[] VALID_DATASET =
+ base16().decode(
+ "0E080000000000010000000300001335060004001FFFE002"
+ + "08ACC214689BC40BDF0708FD64DB1225F47E0B0510F26B31"
+ + "53760F519A63BAFDDFFC80D2AF030F4F70656E5468726561"
+ + "642D643961300102D9A00410A245479C836D551B9CA557F7"
+ + "B9D351B40C0402A0FFF8");
+
+ private static byte[] removeTlv(byte[] dataset, int type) {
+ ByteArrayOutputStream os = new ByteArrayOutputStream(dataset.length);
+ int i = 0;
+ while (i < dataset.length) {
+ int ty = dataset[i++] & 0xff;
+ byte length = dataset[i++];
+ if (ty != type) {
+ byte[] value = Arrays.copyOfRange(dataset, i, i + length);
+ os.write(ty);
+ os.write(length);
+ os.writeBytes(value);
+ }
+ i += length;
+ }
+ return os.toByteArray();
+ }
+
+ private static byte[] addTlv(byte[] dataset, String tlvHex) {
+ return Bytes.concat(dataset, base16().decode(tlvHex));
+ }
+
+ private static byte[] replaceTlv(byte[] dataset, int type, String newTlvHex) {
+ return addTlv(removeTlv(dataset, type), newTlvHex);
+ }
+
+ @Test
+ public void parcelable_parcelingIsLossLess() {
+ ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET);
+
+ assertParcelingIsLossless(dataset);
+ }
+
+ @Test
+ public void fromThreadTlvs_tooLongTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = new byte[255];
+ invalidTlv[0] = (byte) 0xff;
+
+ // This is invalid because the TLV has max total length of 254 bytes and the value length
+ // can't exceeds 252 ( = 254 - 1 - 1)
+ invalidTlv[1] = (byte) 253;
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidNetworkKeyTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_NETWORK_KEY, "05080000000000000000");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noNetworkKeyTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_NETWORK_KEY);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidActiveTimestampTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_ACTIVE_TIMESTAMP, "0E0700000000010000");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noActiveTimestampTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_ACTIVE_TIMESTAMP);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidNetworkNameTlv_emptyName_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_NETWORK_NAME, "0300");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidNetworkNameTlv_tooLongName_throwsIllegalArgument() {
+ byte[] invalidTlv =
+ replaceTlv(
+ VALID_DATASET, TYPE_NETWORK_NAME, "03114142434445464748494A4B4C4D4E4F5051");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noNetworkNameTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_NETWORK_NAME);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidChannelTlv_channelMissing_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000100");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_undefinedChannelPage_success() {
+ byte[] datasetTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "0003010020");
+
+ ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(datasetTlv);
+
+ assertThat(dataset.getChannelPage()).isEqualTo(0x01);
+ assertThat(dataset.getChannel()).isEqualTo(0x20);
+ }
+
+ @Test
+ public void fromThreadTlvs_invalid2P4GhzChannel_throwsIllegalArgument() {
+ byte[] invalidTlv1 = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000300000A");
+ byte[] invalidTlv2 = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "000300001B");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv1));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv2));
+ }
+
+ @Test
+ public void fromThreadTlvs_valid2P4GhzChannelTlv_success() {
+ byte[] validTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL, "0003000010");
+
+ ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(validTlv);
+
+ assertThat(dataset.getChannel()).isEqualTo(16);
+ }
+
+ @Test
+ public void fromThreadTlvs_noChannelTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_CHANNEL);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_prematureEndOfChannelMaskEntry_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "350100");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_inconsistentChannelMaskLength_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "3506000500010000");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_unsupportedChannelMaskLength_success() {
+ ActiveOperationalDataset dataset =
+ ActiveOperationalDataset.fromThreadTlvs(
+ replaceTlv(VALID_DATASET, TYPE_CHANNEL_MASK, "350700050001000000"));
+
+ SparseArray<byte[]> channelMask = dataset.getChannelMask();
+ assertThat(channelMask.size()).isEqualTo(1);
+ assertThat(channelMask.get(CHANNEL_PAGE_24_GHZ))
+ .isEqualTo(new byte[] {0x00, 0x01, 0x00, 0x00, 0x00});
+ }
+
+ @Test
+ public void fromThreadTlvs_noChannelMaskTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_CHANNEL_MASK);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidPanIdTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_PAN_ID, "010101");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noPanIdTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_PAN_ID);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidExtendedPanIdTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_EXTENDED_PAN_ID, "020700010203040506");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noExtendedPanIdTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_EXTENDED_PAN_ID);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidPskcTlv_throwsIllegalArgument() {
+ byte[] invalidTlv =
+ replaceTlv(VALID_DATASET, TYPE_PSKC, "0411000102030405060708090A0B0C0D0E0F10");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noPskcTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_PSKC);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_invalidMeshLocalPrefixTlv_throwsIllegalArgument() {
+ byte[] invalidTlv =
+ replaceTlv(VALID_DATASET, TYPE_MESH_LOCAL_PREFIX, "0709FD0001020304050607");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noMeshLocalPrefixTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_MESH_LOCAL_PREFIX);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_tooShortSecurityPolicyTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = replaceTlv(VALID_DATASET, TYPE_SECURITY_POLICY, "0C0101");
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_noSecurityPolicyTlv_throwsIllegalArgument() {
+ byte[] invalidTlv = removeTlv(VALID_DATASET, TYPE_SECURITY_POLICY);
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(invalidTlv));
+ }
+
+ @Test
+ public void fromThreadTlvs_lengthAndDataMissing_throwsIllegalArgument() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(new byte[] {(byte) 0x00}));
+ }
+
+ @Test
+ public void fromThreadTlvs_prematureEndOfData_throwsIllegalArgument() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> ActiveOperationalDataset.fromThreadTlvs(new byte[] {0x00, 0x03, 0x00, 0x00}));
+ }
+
+ @Test
+ public void fromThreadTlvs_validFullDataset_success() {
+ // A valid Thread active operational dataset:
+ // Active Timestamp: 1
+ // Channel: 19
+ // Channel Mask: 0x07FFF800
+ // Ext PAN ID: ACC214689BC40BDF
+ // Mesh Local Prefix: fd64:db12:25f4:7e0b::/64
+ // Network Key: F26B3153760F519A63BAFDDFFC80D2AF
+ // Network Name: OpenThread-d9a0
+ // PAN ID: 0xD9A0
+ // PSKc: A245479C836D551B9CA557F7B9D351B4
+ // Security Policy: 672 onrcb
+ byte[] validDatasetTlv =
+ base16().decode(
+ "0E080000000000010000000300001335060004001FFFE002"
+ + "08ACC214689BC40BDF0708FD64DB1225F47E0B0510F26B31"
+ + "53760F519A63BAFDDFFC80D2AF030F4F70656E5468726561"
+ + "642D643961300102D9A00410A245479C836D551B9CA557F7"
+ + "B9D351B40C0402A0FFF8");
+
+ ActiveOperationalDataset dataset = ActiveOperationalDataset.fromThreadTlvs(validDatasetTlv);
+
+ assertThat(dataset.getNetworkKey())
+ .isEqualTo(base16().decode("F26B3153760F519A63BAFDDFFC80D2AF"));
+ assertThat(dataset.getPanId()).isEqualTo(0xd9a0);
+ assertThat(dataset.getExtendedPanId()).isEqualTo(base16().decode("ACC214689BC40BDF"));
+ assertThat(dataset.getChannel()).isEqualTo(19);
+ assertThat(dataset.getNetworkName()).isEqualTo("OpenThread-d9a0");
+ assertThat(dataset.getPskc())
+ .isEqualTo(base16().decode("A245479C836D551B9CA557F7B9D351B4"));
+ assertThat(dataset.getActiveTimestamp())
+ .isEqualTo(new OperationalDatasetTimestamp(1, 0, false));
+ SparseArray<byte[]> channelMask = dataset.getChannelMask();
+ assertThat(channelMask.size()).isEqualTo(1);
+ assertThat(channelMask.get(CHANNEL_PAGE_24_GHZ))
+ .isEqualTo(new byte[] {0x00, 0x1f, (byte) 0xff, (byte) 0xe0});
+ assertThat(dataset.getMeshLocalPrefix())
+ .isEqualTo(new IpPrefix("fd64:db12:25f4:7e0b::/64"));
+ assertThat(dataset.getSecurityPolicy())
+ .isEqualTo(new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}));
+ }
+
+ @Test
+ public void fromThreadTlvs_containsUnknownTlvs_unknownTlvsRetained() {
+ final byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET, "AA01FFBB020102");
+
+ ActiveOperationalDataset dataset =
+ ActiveOperationalDataset.fromThreadTlvs(datasetWithUnknownTlvs);
+
+ byte[] newDatasetTlvs = dataset.toThreadTlvs();
+ String newDatasetTlvsHex = base16().encode(newDatasetTlvs);
+ assertThat(newDatasetTlvs.length).isEqualTo(datasetWithUnknownTlvs.length);
+ assertThat(newDatasetTlvsHex).contains("AA01FF");
+ assertThat(newDatasetTlvsHex).contains("BB020102");
+ }
+
+ @Test
+ public void toThreadTlvs_conversionIsLossLess() {
+ ActiveOperationalDataset dataset1 = ActiveOperationalDataset.createRandomDataset();
+
+ ActiveOperationalDataset dataset2 =
+ ActiveOperationalDataset.fromThreadTlvs(dataset1.toThreadTlvs());
+
+ assertThat(dataset2).isEqualTo(dataset1);
+ }
+
+ @Test
+ public void builder_buildWithdefaultValues_throwsIllegalState() {
+ assertThrows(IllegalStateException.class, () -> new Builder().build());
+ }
+
+ @Test
+ public void builder_setValidNetworkKey_success() {
+ final byte[] networkKey =
+ new byte[] {
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
+ 0x0d, 0x0e, 0x0f
+ };
+
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setNetworkKey(networkKey)
+ .build();
+
+ assertThat(dataset.getNetworkKey()).isEqualTo(networkKey);
+ }
+
+ @Test
+ public void builder_setInvalidNetworkKey_throwsIllegalArgument() {
+ byte[] invalidNetworkKey = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(
+ IllegalArgumentException.class, () -> builder.setNetworkKey(invalidNetworkKey));
+ }
+
+ @Test
+ public void builder_setValidExtendedPanId_success() {
+ byte[] extendedPanId = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
+
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setExtendedPanId(extendedPanId)
+ .build();
+
+ assertThat(dataset.getExtendedPanId()).isEqualTo(extendedPanId);
+ }
+
+ @Test
+ public void builder_setInvalidExtendedPanId_throwsIllegalArgument() {
+ byte[] extendedPanId = new byte[] {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(IllegalArgumentException.class, () -> builder.setExtendedPanId(extendedPanId));
+ }
+
+ @Test
+ public void builder_setValidPanId_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setPanId(0xfffe)
+ .build();
+
+ assertThat(dataset.getPanId()).isEqualTo(0xfffe);
+ }
+
+ @Test
+ public void builder_setInvalidPanId_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(IllegalArgumentException.class, () -> builder.setPanId(0xffff));
+ }
+
+ @Test
+ public void builder_setInvalidChannel_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(IllegalArgumentException.class, () -> builder.setChannel(0, 0));
+ assertThrows(IllegalArgumentException.class, () -> builder.setChannel(0, 27));
+ }
+
+ @Test
+ public void builder_setValid2P4GhzChannel_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setChannel(CHANNEL_PAGE_24_GHZ, 16)
+ .build();
+
+ assertThat(dataset.getChannel()).isEqualTo(16);
+ assertThat(dataset.getChannelPage()).isEqualTo(CHANNEL_PAGE_24_GHZ);
+ }
+
+ @Test
+ public void builder_setValidNetworkName_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setNetworkName("ot-network")
+ .build();
+
+ assertThat(dataset.getNetworkName()).isEqualTo("ot-network");
+ }
+
+ @Test
+ public void builder_setEmptyNetworkName_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(IllegalArgumentException.class, () -> builder.setNetworkName(""));
+ }
+
+ @Test
+ public void builder_setTooLongNetworkName_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(
+ IllegalArgumentException.class, () -> builder.setNetworkName("openthread-network"));
+ }
+
+ @Test
+ public void builder_setTooLongUtf8NetworkName_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ // UTF-8 encoded length of "我的线程网络" is 18 bytes which exceeds the max length
+ assertThrows(IllegalArgumentException.class, () -> builder.setNetworkName("我的线程网络"));
+ }
+
+ @Test
+ public void builder_setValidUtf8NetworkName_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setNetworkName("我的网络")
+ .build();
+
+ assertThat(dataset.getNetworkName()).isEqualTo("我的网络");
+ }
+
+ @Test
+ public void builder_setValidPskc_success() {
+ byte[] pskc = base16().decode("A245479C836D551B9CA557F7B9D351B4");
+
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset()).setPskc(pskc).build();
+
+ assertThat(dataset.getPskc()).isEqualTo(pskc);
+ }
+
+ @Test
+ public void builder_setTooLongPskc_throwsIllegalArgument() {
+ byte[] tooLongPskc = base16().decode("A245479C836D551B9CA557F7B9D351B400");
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ 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();
+
+ SparseArray<byte[]> resultChannelMask = dataset.getChannelMask();
+ assertThat(resultChannelMask.size()).isEqualTo(1);
+ assertThat(resultChannelMask.get(0)).isEqualTo(new byte[] {0x00, 0x00, 0x01, 0x00});
+ }
+
+ @Test
+ public void builder_setEmptyChannelMask_throwsIllegalArgument() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> builder.setChannelMask(new SparseArray<byte[]>()));
+ }
+
+ @Test
+ public void builder_setValidActiveTimestamp_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setActiveTimestamp(
+ new OperationalDatasetTimestamp(
+ /* seconds= */ 1,
+ /* ticks= */ 0,
+ /* isAuthoritativeSource= */ true))
+ .build();
+
+ assertThat(dataset.getActiveTimestamp().getSeconds()).isEqualTo(1);
+ assertThat(dataset.getActiveTimestamp().getTicks()).isEqualTo(0);
+ assertThat(dataset.getActiveTimestamp().isAuthoritativeSource()).isTrue();
+ }
+
+ @Test
+ public void builder_wrongMeshLocalPrefixLength_throwsIllegalArguments() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ // The Mesh-Local Prefix length must be 64 bits
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> builder.setMeshLocalPrefix(new IpPrefix("fd00::/32")));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> builder.setMeshLocalPrefix(new IpPrefix("fd00::/96")));
+
+ // The Mesh-Local Prefix must start with 0xfd
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> builder.setMeshLocalPrefix(new IpPrefix("fc00::/64")));
+ }
+
+ @Test
+ public void builder_meshLocalPrefixNotStartWith0xfd_throwsIllegalArguments() {
+ Builder builder = new Builder(ActiveOperationalDataset.createRandomDataset());
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> builder.setMeshLocalPrefix(new IpPrefix("fc00::/64")));
+ }
+
+ @Test
+ public void builder_setValidMeshLocalPrefix_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setMeshLocalPrefix(new IpPrefix("fd00::/64"))
+ .build();
+
+ assertThat(dataset.getMeshLocalPrefix()).isEqualTo(new IpPrefix("fd00::/64"));
+ }
+
+ @Test
+ public void builder_setValid1P2SecurityPolicy_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setSecurityPolicy(
+ new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}))
+ .build();
+
+ assertThat(dataset.getSecurityPolicy().getRotationTimeHours()).isEqualTo(672);
+ assertThat(dataset.getSecurityPolicy().getFlags())
+ .isEqualTo(new byte[] {(byte) 0xff, (byte) 0xf8});
+ }
+
+ @Test
+ public void builder_setValid1P1SecurityPolicy_success() {
+ ActiveOperationalDataset dataset =
+ new Builder(ActiveOperationalDataset.createRandomDataset())
+ .setSecurityPolicy(new SecurityPolicy(672, new byte[] {(byte) 0xff}))
+ .build();
+
+ assertThat(dataset.getSecurityPolicy().getRotationTimeHours()).isEqualTo(672);
+ assertThat(dataset.getSecurityPolicy().getFlags()).isEqualTo(new byte[] {(byte) 0xff});
+ }
+
+ @Test
+ public void securityPolicy_invalidRotationTime_throwsIllegalArguments() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> new SecurityPolicy(0, new byte[] {(byte) 0xff, (byte) 0xf8}));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> new SecurityPolicy(0x1ffff, new byte[] {(byte) 0xff, (byte) 0xf8}));
+ }
+
+ @Test
+ public void securityPolicy_emptyFlags_throwsIllegalArguments() {
+ assertThrows(IllegalArgumentException.class, () -> new SecurityPolicy(672, new byte[] {}));
+ }
+
+ @Test
+ public void securityPolicy_tooLongFlags_success() {
+ SecurityPolicy securityPolicy =
+ new SecurityPolicy(672, new byte[] {0, 1, 2, 3, 4, 5, 6, 7});
+
+ assertThat(securityPolicy.getFlags()).isEqualTo(new byte[] {0, 1, 2, 3, 4, 5, 6, 7});
+ }
+
+ @Test
+ public void securityPolicy_equals() {
+ new EqualsTester()
+ .addEqualityGroup(
+ new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}),
+ new SecurityPolicy(672, new byte[] {(byte) 0xff, (byte) 0xf8}))
+ .addEqualityGroup(
+ new SecurityPolicy(1, new byte[] {(byte) 0xff}),
+ new SecurityPolicy(1, new byte[] {(byte) 0xff}))
+ .addEqualityGroup(
+ new SecurityPolicy(1, new byte[] {(byte) 0xff, (byte) 0xf8}),
+ new SecurityPolicy(1, new byte[] {(byte) 0xff, (byte) 0xf8}))
+ .testEquals();
+ }
+}
diff --git a/thread/tests/cts/src/android/net/thread/cts/OperationalDatasetTimestampTest.java b/thread/tests/cts/src/android/net/thread/cts/OperationalDatasetTimestampTest.java
new file mode 100644
index 0000000..9be3d56
--- /dev/null
+++ b/thread/tests/cts/src/android/net/thread/cts/OperationalDatasetTimestampTest.java
@@ -0,0 +1,127 @@
+/*
+ * 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.thread.cts;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+
+import android.net.thread.OperationalDatasetTimestamp;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.google.common.testing.EqualsTester;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.time.Instant;
+
+/** Tests for {@link OperationalDatasetTimestamp}. */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public final class OperationalDatasetTimestampTest {
+ @Test
+ public void fromInstant_tooLargeInstant_throwsIllegalArgument() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ OperationalDatasetTimestamp.fromInstant(
+ Instant.ofEpochSecond(0xffffffffffffL + 1L)));
+ }
+
+ @Test
+ public void fromInstant_ticksIsRounded() {
+ Instant instant = Instant.ofEpochSecond(100L);
+
+ // 32767.5 / 32768 * 1000000000 = 999984741.2109375 and given the `ticks` is rounded, so
+ // the `ticks` should be 32767 for 999984741 and 0 (carried over to seconds) for 999984742.
+ OperationalDatasetTimestamp timestampTicks32767 =
+ OperationalDatasetTimestamp.fromInstant(instant.plusNanos(999984741));
+ OperationalDatasetTimestamp timestampTicks0 =
+ OperationalDatasetTimestamp.fromInstant(instant.plusNanos(999984742));
+
+ assertThat(timestampTicks32767.getSeconds()).isEqualTo(100L);
+ assertThat(timestampTicks0.getSeconds()).isEqualTo(101L);
+ assertThat(timestampTicks32767.getTicks()).isEqualTo(32767);
+ assertThat(timestampTicks0.getTicks()).isEqualTo(0);
+ assertThat(timestampTicks32767.isAuthoritativeSource()).isTrue();
+ assertThat(timestampTicks0.isAuthoritativeSource()).isTrue();
+ }
+
+ @Test
+ public void toInstant_nanosIsRounded() {
+ // 32767 / 32768 * 1000000000 = 999969482.421875
+ assertThat(new OperationalDatasetTimestamp(100L, 32767, false).toInstant().getNano())
+ .isEqualTo(999969482);
+
+ // 32766 / 32768 * 1000000000 = 999938964.84375
+ assertThat(new OperationalDatasetTimestamp(100L, 32766, false).toInstant().getNano())
+ .isEqualTo(999938965);
+ }
+
+ @Test
+ public void toInstant_onlyAuthoritativeSourceDiscarded() {
+ OperationalDatasetTimestamp timestamp1 =
+ new OperationalDatasetTimestamp(100L, 0x7fff, false);
+
+ OperationalDatasetTimestamp timestamp2 =
+ OperationalDatasetTimestamp.fromInstant(timestamp1.toInstant());
+
+ assertThat(timestamp2.getSeconds()).isEqualTo(100L);
+ assertThat(timestamp2.getTicks()).isEqualTo(0x7fff);
+ assertThat(timestamp2.isAuthoritativeSource()).isTrue();
+ }
+
+ @Test
+ public void constructor_tooLargeSeconds_throwsIllegalArguments() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ new OperationalDatasetTimestamp(
+ /* seconds= */ 0x0001112233445566L,
+ /* ticks= */ 0,
+ /* isAuthoritativeSource= */ true));
+ }
+
+ @Test
+ public void constructor_tooLargeTicks_throwsIllegalArguments() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ new OperationalDatasetTimestamp(
+ /* seconds= */ 0x01L,
+ /* ticks= */ 0x8000,
+ /* isAuthoritativeSource= */ true));
+ }
+
+ @Test
+ public void equalityTests() {
+ new EqualsTester()
+ .addEqualityGroup(
+ new OperationalDatasetTimestamp(100, 100, false),
+ new OperationalDatasetTimestamp(100, 100, false))
+ .addEqualityGroup(
+ new OperationalDatasetTimestamp(0, 0, false),
+ new OperationalDatasetTimestamp(0, 0, false))
+ .addEqualityGroup(
+ new OperationalDatasetTimestamp(0xffffffffffffL, 0x7fff, true),
+ new OperationalDatasetTimestamp(0xffffffffffffL, 0x7fff, true))
+ .testEquals();
+ }
+}
diff --git a/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java b/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java
new file mode 100644
index 0000000..7a49957
--- /dev/null
+++ b/thread/tests/cts/src/android/net/thread/cts/PendingOperationalDatasetTest.java
@@ -0,0 +1,247 @@
+/*
+ * 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.thread.cts;
+
+import static com.android.testutils.ParcelUtils.assertParcelingIsLossless;
+
+import static com.google.common.io.BaseEncoding.base16;
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+
+import android.net.IpPrefix;
+import android.net.thread.ActiveOperationalDataset;
+import android.net.thread.OperationalDatasetTimestamp;
+import android.net.thread.PendingOperationalDataset;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.google.common.primitives.Bytes;
+import com.google.common.testing.EqualsTester;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+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();
+
+ @Test
+ public void parcelable_parcelingIsLossLess() {
+ PendingOperationalDataset dataset =
+ new PendingOperationalDataset(
+ DEFAULT_ACTIVE_DATASET,
+ new OperationalDatasetTimestamp(31536000, 200, false),
+ Duration.ofHours(100));
+
+ assertParcelingIsLossless(dataset);
+ }
+
+ @Test
+ public void equalityTests() {
+ ActiveOperationalDataset activeDataset1 = ActiveOperationalDataset.createRandomDataset();
+ ActiveOperationalDataset activeDataset2 = ActiveOperationalDataset.createRandomDataset();
+
+ new EqualsTester()
+ .addEqualityGroup(
+ new PendingOperationalDataset(
+ activeDataset1,
+ new OperationalDatasetTimestamp(31536000, 100, false),
+ Duration.ofMillis(0)),
+ new PendingOperationalDataset(
+ activeDataset1,
+ new OperationalDatasetTimestamp(31536000, 100, false),
+ Duration.ofMillis(0)))
+ .addEqualityGroup(
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(31536000, 100, false),
+ Duration.ofMillis(0)),
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(31536000, 100, false),
+ Duration.ofMillis(0)))
+ .addEqualityGroup(
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(15768000, 0, false),
+ Duration.ofMillis(0)),
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(15768000, 0, false),
+ Duration.ofMillis(0)))
+ .addEqualityGroup(
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(15768000, 0, false),
+ Duration.ofMillis(100)),
+ new PendingOperationalDataset(
+ activeDataset2,
+ new OperationalDatasetTimestamp(15768000, 0, false),
+ Duration.ofMillis(100)))
+ .testEquals();
+ }
+
+ @Test
+ public void constructor_correctValuesAreSet() {
+ PendingOperationalDataset dataset =
+ new PendingOperationalDataset(
+ DEFAULT_ACTIVE_DATASET,
+ new OperationalDatasetTimestamp(31536000, 200, false),
+ Duration.ofHours(100));
+
+ assertThat(dataset.getActiveOperationalDataset()).isEqualTo(DEFAULT_ACTIVE_DATASET);
+ assertThat(dataset.getPendingTimestamp())
+ .isEqualTo(new OperationalDatasetTimestamp(31536000, 200, false));
+ assertThat(dataset.getDelayTimer()).isEqualTo(Duration.ofHours(100));
+ }
+
+ @Test
+ public void fromThreadTlvs_openthreadTlvs_success() {
+ // An example Pending Operational Dataset which is generated with OpenThread CLI:
+ // Pending Timestamp: 2
+ // Active Timestamp: 1
+ // Channel: 26
+ // Channel Mask: 0x07fff800
+ // Delay: 46354
+ // Ext PAN ID: a74182f4d3f4de41
+ // Mesh Local Prefix: fd46:c1b9:e159:5574::/64
+ // Network Key: ed916e454d96fd00184f10a6f5c9e1d3
+ // Network Name: OpenThread-bff8
+ // PAN ID: 0xbff8
+ // PSKc: 264f78414adc683191863d968f72d1b7
+ // Security Policy: 672 onrc
+ final byte[] OPENTHREAD_PENDING_DATASET_TLVS =
+ base16().lowerCase()
+ .decode(
+ "0e0800000000000100003308000000000002000034040000b51200030000"
+ + "1a35060004001fffe00208a74182f4d3f4de410708fd46c1b9"
+ + "e15955740510ed916e454d96fd00184f10a6f5c9e1d3030f4f"
+ + "70656e5468726561642d626666380102bff80410264f78414a"
+ + "dc683191863d968f72d1b70c0402a0f7f8");
+
+ PendingOperationalDataset pendingDataset =
+ PendingOperationalDataset.fromThreadTlvs(OPENTHREAD_PENDING_DATASET_TLVS);
+
+ ActiveOperationalDataset activeDataset = pendingDataset.getActiveOperationalDataset();
+ assertThat(pendingDataset.getPendingTimestamp().getSeconds()).isEqualTo(2L);
+ assertThat(activeDataset.getActiveTimestamp().getSeconds()).isEqualTo(1L);
+ assertThat(activeDataset.getChannel()).isEqualTo(26);
+ assertThat(activeDataset.getChannelMask().get(0))
+ .isEqualTo(new byte[] {0x00, 0x1f, (byte) 0xff, (byte) 0xe0});
+ assertThat(pendingDataset.getDelayTimer().toMillis()).isEqualTo(46354);
+ assertThat(activeDataset.getExtendedPanId())
+ .isEqualTo(base16().lowerCase().decode("a74182f4d3f4de41"));
+ assertThat(activeDataset.getMeshLocalPrefix())
+ .isEqualTo(new IpPrefix("fd46:c1b9:e159:5574::/64"));
+ assertThat(activeDataset.getNetworkKey())
+ .isEqualTo(base16().lowerCase().decode("ed916e454d96fd00184f10a6f5c9e1d3"));
+ assertThat(activeDataset.getNetworkName()).isEqualTo("OpenThread-bff8");
+ assertThat(activeDataset.getPanId()).isEqualTo(0xbff8);
+ assertThat(activeDataset.getPskc())
+ .isEqualTo(base16().lowerCase().decode("264f78414adc683191863d968f72d1b7"));
+ assertThat(activeDataset.getSecurityPolicy().getRotationTimeHours()).isEqualTo(672);
+ assertThat(activeDataset.getSecurityPolicy().getFlags())
+ .isEqualTo(new byte[] {(byte) 0xf7, (byte) 0xf8});
+ }
+
+ @Test
+ public void fromThreadTlvs_completePendingDatasetTlvs_success() {
+ // 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());
+
+ PendingOperationalDataset dataset =
+ PendingOperationalDataset.fromThreadTlvs(pendingDatasetTlvs);
+
+ assertThat(dataset.getActiveOperationalDataset()).isEqualTo(DEFAULT_ACTIVE_DATASET);
+ assertThat(dataset.getPendingTimestamp())
+ .isEqualTo(new OperationalDatasetTimestamp(1, 0, false));
+ assertThat(dataset.getDelayTimer()).isEqualTo(Duration.ofMillis(300));
+ }
+
+ @Test
+ public void fromThreadTlvs_PendingTimestampTlvIsMissing_throwsIllegalArgument() {
+ // 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());
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> PendingOperationalDataset.fromThreadTlvs(pendingDatasetTlvs));
+ }
+
+ @Test
+ public void fromThreadTlvs_delayTimerTlvIsMissing_throwsIllegalArgument() {
+ // 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());
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> PendingOperationalDataset.fromThreadTlvs(pendingDatasetTlvs));
+ }
+
+ @Test
+ public void fromThreadTlvs_activeDatasetTlvs_throwsIllegalArgument() {
+ final byte[] activeDatasetTlvs = DEFAULT_ACTIVE_DATASET.toThreadTlvs();
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> PendingOperationalDataset.fromThreadTlvs(activeDatasetTlvs));
+ }
+
+ @Test
+ public void fromThreadTlvs_malformedTlvs_throwsIllegalArgument() {
+ final byte[] invalidTlvs = new byte[] {0x00};
+
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> PendingOperationalDataset.fromThreadTlvs(invalidTlvs));
+ }
+
+ @Test
+ public void toThreadTlvs_conversionIsLossLess() {
+ PendingOperationalDataset dataset1 =
+ new PendingOperationalDataset(
+ DEFAULT_ACTIVE_DATASET,
+ new OperationalDatasetTimestamp(31536000, 200, false),
+ Duration.ofHours(100));
+
+ PendingOperationalDataset dataset2 =
+ PendingOperationalDataset.fromThreadTlvs(dataset1.toThreadTlvs());
+
+ assertThat(dataset2).isEqualTo(dataset1);
+ }
+}
diff --git a/thread/tests/unit/Android.bp b/thread/tests/unit/Android.bp
new file mode 100644
index 0000000..1f16ad1
--- /dev/null
+++ b/thread/tests/unit/Android.bp
@@ -0,0 +1,50 @@
+//
+// 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+android_test {
+ name: "ThreadNetworkUnitTests",
+ min_sdk_version: "33",
+ sdk_version: "module_current",
+ manifest: "AndroidManifest.xml",
+ test_config: "AndroidTest.xml",
+ srcs: [
+ "src/**/*.java",
+ ],
+ test_suites: [
+ "general-tests",
+ ],
+ static_libs: [
+ "androidx.test.ext.junit",
+ "compatibility-device-util-axt",
+ "ctstestrunner-axt",
+ "framework-connectivity-pre-jarjar",
+ "framework-connectivity-t-pre-jarjar",
+ "guava-android-testlib",
+ "net-tests-utils",
+ "truth-prebuilt",
+ ],
+ libs: [
+ "android.test.base",
+ "android.test.runner",
+ ],
+ // Test coverage system runs on different devices. Need to
+ // compile for all architectures.
+ compile_multilib: "both",
+}
diff --git a/thread/tests/unit/AndroidManifest.xml b/thread/tests/unit/AndroidManifest.xml
new file mode 100644
index 0000000..ace7c52
--- /dev/null
+++ b/thread/tests/unit/AndroidManifest.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ 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.
+ -->
+
+<manifest
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ package="android.net.thread.unittests">
+
+ <application android:debuggable="true">
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation
+ android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="android.net.thread.unittests"
+ android:label="Unit tests for android.net.thread" />
+</manifest>
diff --git a/thread/tests/unit/AndroidTest.xml b/thread/tests/unit/AndroidTest.xml
new file mode 100644
index 0000000..663ff74
--- /dev/null
+++ b/thread/tests/unit/AndroidTest.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ 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.
+ -->
+
+<configuration description="Config for Thread network unit test cases">
+ <option name="test-tag" value="ThreadNetworkUnitTests" />
+ <option name="test-suite-tag" value="apct" />
+
+ <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
+ <option name="test-file-name" value="ThreadNetworkUnitTests.apk" />
+ <option name="check-min-sdk" value="true" />
+ <option name="cleanup-apks" value="true" />
+ </target_preparer>
+
+ <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+ <option name="package" value="android.net.thread.unittests" />
+ <!-- Ignores tests introduced by guava-android-testlib -->
+ <option name="exclude-annotation" value="org.junit.Ignore"/>
+ </test>
+</configuration>
diff --git a/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java b/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java
new file mode 100644
index 0000000..78eb3d0
--- /dev/null
+++ b/thread/tests/unit/src/android/net/thread/ActiveOperationalDatasetTest.java
@@ -0,0 +1,202 @@
+/*
+ * 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.thread;
+
+import static com.google.common.io.BaseEncoding.base16;
+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;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import com.google.common.primitives.Bytes;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.security.SecureRandom;
+import java.util.Random;
+
+/** Unit tests for {@link ActiveOperationalDataset}. */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class ActiveOperationalDatasetTest {
+ // A valid Thread Active Operational Dataset generated from OpenThread CLI "dataset new":
+ // Active Timestamp: 1
+ // Channel: 19
+ // Channel Mask: 0x07FFF800
+ // Ext PAN ID: ACC214689BC40BDF
+ // Mesh Local Prefix: fd64:db12:25f4:7e0b::/64
+ // Network Key: F26B3153760F519A63BAFDDFFC80D2AF
+ // Network Name: OpenThread-d9a0
+ // PAN ID: 0xD9A0
+ // PSKc: A245479C836D551B9CA557F7B9D351B4
+ // Security Policy: 672 onrcb
+ private static final byte[] VALID_DATASET =
+ base16().decode(
+ "0E080000000000010000000300001335060004001FFFE002"
+ + "08ACC214689BC40BDF0708FD64DB1225F47E0B0510F26B31"
+ + "53760F519A63BAFDDFFC80D2AF030F4F70656E5468726561"
+ + "642D643961300102D9A00410A245479C836D551B9CA557F7"
+ + "B9D351B40C0402A0FFF8");
+
+ @Mock private Random mockRandom;
+ @Mock private SecureRandom mockSecureRandom;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ private static byte[] addTlv(byte[] dataset, String tlvHex) {
+ return Bytes.concat(dataset, base16().decode(tlvHex));
+ }
+
+ @Test
+ public void fromThreadTlvs_containsUnknownTlvs_unknownTlvsRetained() {
+ byte[] datasetWithUnknownTlvs = addTlv(VALID_DATASET, "AA01FFBB020102");
+
+ ActiveOperationalDataset dataset1 =
+ ActiveOperationalDataset.fromThreadTlvs(datasetWithUnknownTlvs);
+ ActiveOperationalDataset dataset2 =
+ ActiveOperationalDataset.fromThreadTlvs(dataset1.toThreadTlvs());
+
+ SparseArray<byte[]> unknownTlvs = dataset2.getUnknownTlvs();
+ assertThat(unknownTlvs.size()).isEqualTo(2);
+ assertThat(unknownTlvs.get(0xAA)).isEqualTo(new byte[] {(byte) 0xFF});
+ assertThat(unknownTlvs.get(0xBB)).isEqualTo(new byte[] {0x01, 0x02});
+ assertThat(dataset2).isEqualTo(dataset1);
+ }
+
+ @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());
+ for (int i = 0; i < 10; i++) {
+ builder.addUnknownTlv(i, new byte[20]);
+ }
+
+ assertThrows(IllegalStateException.class, () -> new Builder().build());
+ }
+
+ @Test
+ public void builder_setUnknownTlvs_success() {
+ ActiveOperationalDataset dataset1 = ActiveOperationalDataset.fromThreadTlvs(VALID_DATASET);
+ SparseArray<byte[]> unknownTlvs = new SparseArray<>(2);
+ unknownTlvs.put(0x33, new byte[] {1, 2, 3});
+ unknownTlvs.put(0x44, new byte[] {1, 2, 3, 4});
+
+ ActiveOperationalDataset dataset2 =
+ new ActiveOperationalDataset.Builder(dataset1).setUnknownTlvs(unknownTlvs).build();
+
+ assertThat(dataset1.getUnknownTlvs().size()).isEqualTo(0);
+ assertThat(dataset2.getUnknownTlvs().size()).isEqualTo(2);
+ assertThat(dataset2.getUnknownTlvs().get(0x33)).isEqualTo(new byte[] {1, 2, 3});
+ assertThat(dataset2.getUnknownTlvs().get(0x44)).isEqualTo(new byte[] {1, 2, 3, 4});
+ }
+
+ @Test
+ public void securityPolicy_fromTooShortTlvValue_throwsIllegalArgument() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> SecurityPolicy.fromTlvValue(new byte[] {0x01}));
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> SecurityPolicy.fromTlvValue(new byte[] {0x01, 0x02}));
+ }
+
+ @Test
+ public void securityPolicy_toTlvValue_conversionIsLossLess() {
+ SecurityPolicy policy1 = new SecurityPolicy(200, new byte[] {(byte) 0xFF, (byte) 0xF8});
+
+ SecurityPolicy policy2 = SecurityPolicy.fromTlvValue(policy1.toTlvValue());
+
+ assertThat(policy2).isEqualTo(policy1);
+ }
+}
diff --git a/thread/tests/unit/src/android/net/thread/OperationalDatasetTimestampTest.java b/thread/tests/unit/src/android/net/thread/OperationalDatasetTimestampTest.java
new file mode 100644
index 0000000..32063fc
--- /dev/null
+++ b/thread/tests/unit/src/android/net/thread/OperationalDatasetTimestampTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.thread;
+
+import static com.google.common.io.BaseEncoding.base16;
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.junit.Assert.assertThrows;
+
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+import androidx.test.filters.SmallTest;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/** Unit tests for {@link OperationalDatasetTimestamp}. */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public final class OperationalDatasetTimestampTest {
+ @Test
+ public void fromTlvValue_invalidTimestamp_throwsIllegalArguments() {
+ assertThrows(
+ IllegalArgumentException.class,
+ () -> OperationalDatasetTimestamp.fromTlvValue(new byte[7]));
+ }
+
+ @Test
+ public void fromTlvValue_goodValue_success() {
+ OperationalDatasetTimestamp timestamp =
+ OperationalDatasetTimestamp.fromTlvValue(base16().decode("FFEEDDCCBBAA9989"));
+
+ assertThat(timestamp.getSeconds()).isEqualTo(0xFFEEDDCCBBAAL);
+ // 0x9989 is 0x4CC4 << 1 + 1
+ assertThat(timestamp.getTicks()).isEqualTo(0x4CC4);
+ assertThat(timestamp.isAuthoritativeSource()).isTrue();
+ }
+
+ @Test
+ public void toTlvValue_conversionIsLossLess() {
+ OperationalDatasetTimestamp timestamp1 = new OperationalDatasetTimestamp(100L, 10, true);
+
+ OperationalDatasetTimestamp timestamp2 =
+ OperationalDatasetTimestamp.fromTlvValue(timestamp1.toTlvValue());
+
+ assertThat(timestamp2).isEqualTo(timestamp1);
+ }
+}