summaryrefslogtreecommitdiff
path: root/common/tests/unit/src/com/android/net/module/util/netlink
diff options
context:
space:
mode:
Diffstat (limited to 'common/tests/unit/src/com/android/net/module/util/netlink')
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/ConntrackMessageTest.java433
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/InetDiagSocketTest.java692
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/NduseroptMessageTest.java295
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/NetlinkConstantsTest.java132
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/NetlinkErrorMessageTest.java91
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/NetlinkUtilsTest.java226
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkAddressMessageTest.java289
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkLinkMessageTest.java192
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkNeighborMessageTest.java207
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java229
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/StructInetDiagSockIdTest.java225
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptPref64Test.java202
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptRdnssTest.java195
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/StructNlAttrTest.java95
-rw-r--r--common/tests/unit/src/com/android/net/module/util/netlink/StructNlMsgHdrTest.java102
15 files changed, 0 insertions, 3605 deletions
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/ConntrackMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/ConntrackMessageTest.java
deleted file mode 100644
index f02b4cb6..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/ConntrackMessageTest.java
+++ /dev/null
@@ -1,433 +0,0 @@
-/*
- * Copyright (C) 2017 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.netlink;
-
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_NEW;
-import static com.android.net.module.util.netlink.NetlinkConstants.NFNL_SUBSYS_CTNETLINK;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assume.assumeTrue;
-
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet4Address;
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.Arrays;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class ConntrackMessageTest {
- private static final boolean USING_LE = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN);
-
- private short makeCtType(short msgType) {
- return (short) (NFNL_SUBSYS_CTNETLINK << 8 | (byte) msgType);
- }
-
- // Example 1: TCP (192.168.43.209, 44333) -> (23.211.13.26, 443)
- public static final String CT_V4UPDATE_TCP_HEX =
- // struct nlmsghdr
- "50000000" + // length = 80
- "0001" + // type = (1 << 8) | 0
- "0501" + // flags
- "01000000" + // seqno = 1
- "00000000" + // pid = 0
- // struct nfgenmsg
- "02" + // nfgen_family = AF_INET
- "00" + // version = NFNETLINK_V0
- "0000" + // res_id
- // struct nlattr
- "3400" + // nla_len = 52
- "0180" + // nla_type = nested CTA_TUPLE_ORIG
- // struct nlattr
- "1400" + // nla_len = 20
- "0180" + // nla_type = nested CTA_TUPLE_IP
- "0800 0100 C0A82BD1" + // nla_type=CTA_IP_V4_SRC, ip=192.168.43.209
- "0800 0200 17D30D1A" + // nla_type=CTA_IP_V4_DST, ip=23.211.13.26
- // struct nlattr
- "1C00" + // nla_len = 28
- "0280" + // nla_type = nested CTA_TUPLE_PROTO
- "0500 0100 06 000000" + // nla_type=CTA_PROTO_NUM, proto=6
- "0600 0200 AD2D 0000" + // nla_type=CTA_PROTO_SRC_PORT, port=44333 (big endian)
- "0600 0300 01BB 0000" + // nla_type=CTA_PROTO_DST_PORT, port=443 (big endian)
- // struct nlattr
- "0800" + // nla_len = 8
- "0700" + // nla_type = CTA_TIMEOUT
- "00069780"; // nla_value = 432000 (big endian)
- public static final byte[] CT_V4UPDATE_TCP_BYTES =
- HexEncoding.decode(CT_V4UPDATE_TCP_HEX.replaceAll(" ", "").toCharArray(), false);
-
- private byte[] makeIPv4TimeoutUpdateRequestTcp() throws Exception {
- return ConntrackMessage.newIPv4TimeoutUpdateRequest(
- OsConstants.IPPROTO_TCP,
- (Inet4Address) InetAddress.getByName("192.168.43.209"), 44333,
- (Inet4Address) InetAddress.getByName("23.211.13.26"), 443,
- 432000);
- }
-
- // Example 2: UDP (100.96.167.146, 37069) -> (216.58.197.10, 443)
- public static final String CT_V4UPDATE_UDP_HEX =
- // struct nlmsghdr
- "50000000" + // length = 80
- "0001" + // type = (1 << 8) | 0
- "0501" + // flags
- "01000000" + // seqno = 1
- "00000000" + // pid = 0
- // struct nfgenmsg
- "02" + // nfgen_family = AF_INET
- "00" + // version = NFNETLINK_V0
- "0000" + // res_id
- // struct nlattr
- "3400" + // nla_len = 52
- "0180" + // nla_type = nested CTA_TUPLE_ORIG
- // struct nlattr
- "1400" + // nla_len = 20
- "0180" + // nla_type = nested CTA_TUPLE_IP
- "0800 0100 6460A792" + // nla_type=CTA_IP_V4_SRC, ip=100.96.167.146
- "0800 0200 D83AC50A" + // nla_type=CTA_IP_V4_DST, ip=216.58.197.10
- // struct nlattr
- "1C00" + // nla_len = 28
- "0280" + // nla_type = nested CTA_TUPLE_PROTO
- "0500 0100 11 000000" + // nla_type=CTA_PROTO_NUM, proto=17
- "0600 0200 90CD 0000" + // nla_type=CTA_PROTO_SRC_PORT, port=37069 (big endian)
- "0600 0300 01BB 0000" + // nla_type=CTA_PROTO_DST_PORT, port=443 (big endian)
- // struct nlattr
- "0800" + // nla_len = 8
- "0700" + // nla_type = CTA_TIMEOUT
- "000000B4"; // nla_value = 180 (big endian)
- public static final byte[] CT_V4UPDATE_UDP_BYTES =
- HexEncoding.decode(CT_V4UPDATE_UDP_HEX.replaceAll(" ", "").toCharArray(), false);
-
- private byte[] makeIPv4TimeoutUpdateRequestUdp() throws Exception {
- return ConntrackMessage.newIPv4TimeoutUpdateRequest(
- OsConstants.IPPROTO_UDP,
- (Inet4Address) InetAddress.getByName("100.96.167.146"), 37069,
- (Inet4Address) InetAddress.getByName("216.58.197.10"), 443,
- 180);
- }
-
- @Test
- public void testConntrackMakeIPv4TcpTimeoutUpdate() throws Exception {
- assumeTrue(USING_LE);
-
- final byte[] tcp = makeIPv4TimeoutUpdateRequestTcp();
- assertArrayEquals(CT_V4UPDATE_TCP_BYTES, tcp);
- }
-
- @Test
- public void testConntrackParseIPv4TcpTimeoutUpdate() throws Exception {
- assumeTrue(USING_LE);
-
- final byte[] tcp = makeIPv4TimeoutUpdateRequestTcp();
- final ByteBuffer byteBuffer = ByteBuffer.wrap(tcp);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
- assertNotNull(msg);
- assertTrue(msg instanceof ConntrackMessage);
- final ConntrackMessage conntrackMessage = (ConntrackMessage) msg;
-
- final StructNlMsgHdr hdr = conntrackMessage.getHeader();
- assertNotNull(hdr);
- assertEquals(80, hdr.nlmsg_len);
- assertEquals(makeCtType(IPCTNL_MSG_CT_NEW), hdr.nlmsg_type);
- assertEquals((short) (StructNlMsgHdr.NLM_F_REPLACE | StructNlMsgHdr.NLM_F_REQUEST
- | StructNlMsgHdr.NLM_F_ACK), hdr.nlmsg_flags);
- assertEquals(1, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructNfGenMsg nfmsgHdr = conntrackMessage.nfGenMsg;
- assertNotNull(nfmsgHdr);
- assertEquals((byte) OsConstants.AF_INET, nfmsgHdr.nfgen_family);
- assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
- assertEquals((short) 0, nfmsgHdr.res_id);
-
- assertEquals(InetAddress.parseNumericAddress("192.168.43.209"),
- conntrackMessage.tupleOrig.srcIp);
- assertEquals(InetAddress.parseNumericAddress("23.211.13.26"),
- conntrackMessage.tupleOrig.dstIp);
- assertEquals((byte) OsConstants.IPPROTO_TCP, conntrackMessage.tupleOrig.protoNum);
- assertEquals((short) 44333, conntrackMessage.tupleOrig.srcPort);
- assertEquals((short) 443, conntrackMessage.tupleOrig.dstPort);
-
- assertNull(conntrackMessage.tupleReply);
-
- assertEquals(0 /* absent */, conntrackMessage.status);
- assertEquals(432000, conntrackMessage.timeoutSec);
- }
-
- @Test
- public void testConntrackMakeIPv4UdpTimeoutUpdate() throws Exception {
- assumeTrue(USING_LE);
-
- final byte[] udp = makeIPv4TimeoutUpdateRequestUdp();
- assertArrayEquals(CT_V4UPDATE_UDP_BYTES, udp);
- }
-
- @Test
- public void testConntrackParseIPv4UdpTimeoutUpdate() throws Exception {
- assumeTrue(USING_LE);
-
- final byte[] udp = makeIPv4TimeoutUpdateRequestUdp();
- final ByteBuffer byteBuffer = ByteBuffer.wrap(udp);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
- assertNotNull(msg);
- assertTrue(msg instanceof ConntrackMessage);
- final ConntrackMessage conntrackMessage = (ConntrackMessage) msg;
-
- final StructNlMsgHdr hdr = conntrackMessage.getHeader();
- assertNotNull(hdr);
- assertEquals(80, hdr.nlmsg_len);
- assertEquals(makeCtType(IPCTNL_MSG_CT_NEW), hdr.nlmsg_type);
- assertEquals((short) (StructNlMsgHdr.NLM_F_REPLACE | StructNlMsgHdr.NLM_F_REQUEST
- | StructNlMsgHdr.NLM_F_ACK), hdr.nlmsg_flags);
- assertEquals(1, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructNfGenMsg nfmsgHdr = conntrackMessage.nfGenMsg;
- assertNotNull(nfmsgHdr);
- assertEquals((byte) OsConstants.AF_INET, nfmsgHdr.nfgen_family);
- assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
- assertEquals((short) 0, nfmsgHdr.res_id);
-
- assertEquals(InetAddress.parseNumericAddress("100.96.167.146"),
- conntrackMessage.tupleOrig.srcIp);
- assertEquals(InetAddress.parseNumericAddress("216.58.197.10"),
- conntrackMessage.tupleOrig.dstIp);
- assertEquals((byte) OsConstants.IPPROTO_UDP, conntrackMessage.tupleOrig.protoNum);
- assertEquals((short) 37069, conntrackMessage.tupleOrig.srcPort);
- assertEquals((short) 443, conntrackMessage.tupleOrig.dstPort);
-
- assertNull(conntrackMessage.tupleReply);
-
- assertEquals(0 /* absent */, conntrackMessage.status);
- assertEquals(180, conntrackMessage.timeoutSec);
- }
-
- public static final String CT_V4NEW_TCP_HEX =
- // CHECKSTYLE:OFF IndentationCheck
- // struct nlmsghdr
- "8C000000" + // length = 140
- "0001" + // type = NFNL_SUBSYS_CTNETLINK (1) << 8 | IPCTNL_MSG_CT_NEW (0)
- "0006" + // flags = NLM_F_CREATE (1 << 10) | NLM_F_EXCL (1 << 9)
- "00000000" + // seqno = 0
- "00000000" + // pid = 0
- // struct nfgenmsg
- "02" + // nfgen_family = AF_INET
- "00" + // version = NFNETLINK_V0
- "1234" + // res_id = 0x1234 (big endian)
- // struct nlattr
- "3400" + // nla_len = 52
- "0180" + // nla_type = nested CTA_TUPLE_ORIG
- // struct nlattr
- "1400" + // nla_len = 20
- "0180" + // nla_type = nested CTA_TUPLE_IP
- "0800 0100 C0A8500C" + // nla_type=CTA_IP_V4_SRC, ip=192.168.80.12
- "0800 0200 8C700874" + // nla_type=CTA_IP_V4_DST, ip=140.112.8.116
- // struct nlattr
- "1C00" + // nla_len = 28
- "0280" + // nla_type = nested CTA_TUPLE_PROTO
- "0500 0100 06 000000" + // nla_type=CTA_PROTO_NUM, proto=IPPROTO_TCP (6)
- "0600 0200 F3F1 0000" + // nla_type=CTA_PROTO_SRC_PORT, port=62449 (big endian)
- "0600 0300 01BB 0000" + // nla_type=CTA_PROTO_DST_PORT, port=443 (big endian)
- // struct nlattr
- "3400" + // nla_len = 52
- "0280" + // nla_type = nested CTA_TUPLE_REPLY
- // struct nlattr
- "1400" + // nla_len = 20
- "0180" + // nla_type = nested CTA_TUPLE_IP
- "0800 0100 8C700874" + // nla_type=CTA_IP_V4_SRC, ip=140.112.8.116
- "0800 0200 6451B301" + // nla_type=CTA_IP_V4_DST, ip=100.81.179.1
- // struct nlattr
- "1C00" + // nla_len = 28
- "0280" + // nla_type = nested CTA_TUPLE_PROTO
- "0500 0100 06 000000" + // nla_type=CTA_PROTO_NUM, proto=IPPROTO_TCP (6)
- "0600 0200 01BB 0000" + // nla_type=CTA_PROTO_SRC_PORT, port=443 (big endian)
- "0600 0300 F3F1 0000" + // nla_type=CTA_PROTO_DST_PORT, port=62449 (big endian)
- // struct nlattr
- "0800" + // nla_len = 8
- "0300" + // nla_type = CTA_STATUS
- "00000198" + // nla_value = 0b110011000 (big endian)
- // IPS_CONFIRMED (1 << 3) | IPS_SRC_NAT (1 << 4) |
- // IPS_SRC_NAT_DONE (1 << 7) | IPS_DST_NAT_DONE (1 << 8)
- // struct nlattr
- "0800" + // nla_len = 8
- "0700" + // nla_type = CTA_TIMEOUT
- "00000078"; // nla_value = 120 (big endian)
- // CHECKSTYLE:ON IndentationCheck
- public static final byte[] CT_V4NEW_TCP_BYTES =
- HexEncoding.decode(CT_V4NEW_TCP_HEX.replaceAll(" ", "").toCharArray(), false);
-
- @Test
- public void testParseCtNew() {
- assumeTrue(USING_LE);
-
- final ByteBuffer byteBuffer = ByteBuffer.wrap(CT_V4NEW_TCP_BYTES);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
- assertNotNull(msg);
- assertTrue(msg instanceof ConntrackMessage);
- final ConntrackMessage conntrackMessage = (ConntrackMessage) msg;
-
- final StructNlMsgHdr hdr = conntrackMessage.getHeader();
- assertNotNull(hdr);
- assertEquals(140, hdr.nlmsg_len);
- assertEquals(makeCtType(IPCTNL_MSG_CT_NEW), hdr.nlmsg_type);
- assertEquals((short) (StructNlMsgHdr.NLM_F_CREATE | StructNlMsgHdr.NLM_F_EXCL),
- hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructNfGenMsg nfmsgHdr = conntrackMessage.nfGenMsg;
- assertNotNull(nfmsgHdr);
- assertEquals((byte) OsConstants.AF_INET, nfmsgHdr.nfgen_family);
- assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
- assertEquals((short) 0x1234, nfmsgHdr.res_id);
-
- assertEquals(InetAddress.parseNumericAddress("192.168.80.12"),
- conntrackMessage.tupleOrig.srcIp);
- assertEquals(InetAddress.parseNumericAddress("140.112.8.116"),
- conntrackMessage.tupleOrig.dstIp);
- assertEquals((byte) OsConstants.IPPROTO_TCP, conntrackMessage.tupleOrig.protoNum);
- assertEquals((short) 62449, conntrackMessage.tupleOrig.srcPort);
- assertEquals((short) 443, conntrackMessage.tupleOrig.dstPort);
-
- assertEquals(InetAddress.parseNumericAddress("140.112.8.116"),
- conntrackMessage.tupleReply.srcIp);
- assertEquals(InetAddress.parseNumericAddress("100.81.179.1"),
- conntrackMessage.tupleReply.dstIp);
- assertEquals((byte) OsConstants.IPPROTO_TCP, conntrackMessage.tupleReply.protoNum);
- assertEquals((short) 443, conntrackMessage.tupleReply.srcPort);
- assertEquals((short) 62449, conntrackMessage.tupleReply.dstPort);
-
- assertEquals(0x198, conntrackMessage.status);
- assertEquals(120, conntrackMessage.timeoutSec);
- }
-
- @Test
- public void testParseTruncation() {
- assumeTrue(USING_LE);
-
- // Expect no crash while parsing the truncated message which has been truncated to every
- // length between 0 and its full length - 1.
- for (int len = 0; len < CT_V4NEW_TCP_BYTES.length; len++) {
- final byte[] truncated = Arrays.copyOfRange(CT_V4NEW_TCP_BYTES, 0, len);
-
- final ByteBuffer byteBuffer = ByteBuffer.wrap(truncated);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer,
- OsConstants.NETLINK_NETFILTER);
- }
- }
-
- @Test
- public void testParseTruncationWithInvalidByte() {
- assumeTrue(USING_LE);
-
- // Expect no crash while parsing the message which is truncated by invalid bytes. The
- // message has been truncated to every length between 0 and its full length - 1.
- for (byte invalid : new byte[]{(byte) 0x00, (byte) 0xff}) {
- for (int len = 0; len < CT_V4NEW_TCP_BYTES.length; len++) {
- final byte[] truncated = new byte[CT_V4NEW_TCP_BYTES.length];
- Arrays.fill(truncated, (byte) invalid);
- System.arraycopy(CT_V4NEW_TCP_BYTES, 0, truncated, 0, len);
-
- final ByteBuffer byteBuffer = ByteBuffer.wrap(truncated);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer,
- OsConstants.NETLINK_NETFILTER);
- }
- }
- }
-
- // Malformed conntrack messages.
- public static final String CT_MALFORMED_HEX =
- // CHECKSTYLE:OFF IndentationCheck
- // <-- nlmsghr -->|<-nfgenmsg->|<-- CTA_TUPLE_ORIG -->|
- // CTA_TUPLE_ORIG has no nla_value.
- "18000000 0001 0006 00000000 00000000 02 00 0000 0400 0180"
- // nested CTA_TUPLE_IP has no nla_value.
- + "1C000000 0001 0006 00000000 00000000 02 00 0000 0800 0180 0400 0180"
- // nested CTA_IP_V4_SRC has no nla_value.
- + "20000000 0001 0006 00000000 00000000 02 00 0000 0C00 0180 0800 0180 0400 0100"
- // nested CTA_TUPLE_PROTO has no nla_value.
- // <-- nlmsghr -->|<-nfgenmsg->|<-- CTA_TUPLE_ORIG
- + "30000000 0001 0006 00000000 00000000 02 00 0000 1C00 0180 1400 0180 0800 0100"
- // -->|
- + "C0A8500C 0800 0200 8C700874 0400 0280";
- // CHECKSTYLE:ON IndentationCheck
- public static final byte[] CT_MALFORMED_BYTES =
- HexEncoding.decode(CT_MALFORMED_HEX.replaceAll(" ", "").toCharArray(), false);
-
- @Test
- public void testParseMalformation() {
- assumeTrue(USING_LE);
-
- final ByteBuffer byteBuffer = ByteBuffer.wrap(CT_MALFORMED_BYTES);
- byteBuffer.order(ByteOrder.nativeOrder());
-
- // Expect no crash while parsing the malformed message.
- int messageCount = 0;
- while (byteBuffer.remaining() > 0) {
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer,
- OsConstants.NETLINK_NETFILTER);
- messageCount++;
- }
- assertEquals(4, messageCount);
- }
-
- @Test
- public void testToString() {
- assumeTrue(USING_LE);
-
- final ByteBuffer byteBuffer = ByteBuffer.wrap(CT_V4NEW_TCP_BYTES);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
- assertNotNull(msg);
- assertTrue(msg instanceof ConntrackMessage);
- final ConntrackMessage conntrackMessage = (ConntrackMessage) msg;
-
- // Bug: "nlmsg_flags{1536(NLM_F_MATCH))" is not correct because StructNlMsgHdr
- // #stringForNlMsgFlags can't convert all flags (ex: NLM_F_CREATE) and can't distinguish
- // the flags which have the same value (ex: NLM_F_MATCH <0x200> and NLM_F_EXCL <0x200>).
- // The flags output string should be "NLM_F_CREATE|NLM_F_EXCL" in this case.
- // TODO: correct the flag converted string once #stringForNlMsgFlags does.
- final String expected = ""
- + "ConntrackMessage{"
- + "nlmsghdr{StructNlMsgHdr{ nlmsg_len{140}, nlmsg_type{256(IPCTNL_MSG_CT_NEW)}, "
- + "nlmsg_flags{1536(NLM_F_MATCH)}, nlmsg_seq{0}, nlmsg_pid{0} }}, "
- + "nfgenmsg{NfGenMsg{ nfgen_family{AF_INET}, version{0}, res_id{4660} }}, "
- + "tuple_orig{Tuple{IPPROTO_TCP: 192.168.80.12:62449 -> 140.112.8.116:443}}, "
- + "tuple_reply{Tuple{IPPROTO_TCP: 140.112.8.116:443 -> 100.81.179.1:62449}}, "
- + "status{408(IPS_CONFIRMED|IPS_SRC_NAT|IPS_SRC_NAT_DONE|IPS_DST_NAT_DONE)}, "
- + "timeout_sec{120}}";
- assertEquals(expected, conntrackMessage.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/InetDiagSocketTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/InetDiagSocketTest.java
deleted file mode 100644
index 65e99f82..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/InetDiagSocketTest.java
+++ /dev/null
@@ -1,692 +0,0 @@
-/*
- * Copyright (C) 2018 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.netlink;
-
-import static android.os.Process.ROOT_UID;
-import static android.os.Process.SHELL_UID;
-import static android.system.OsConstants.AF_INET;
-import static android.system.OsConstants.AF_INET6;
-import static android.system.OsConstants.IPPROTO_TCP;
-import static android.system.OsConstants.IPPROTO_UDP;
-import static android.system.OsConstants.NETLINK_INET_DIAG;
-
-import static com.android.net.module.util.netlink.NetlinkConstants.SOCK_DESTROY;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_ACK;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_REQUEST;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import android.net.InetAddresses;
-import android.util.ArraySet;
-import android.util.Range;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.net.InetSocketAddress;
-import java.net.UnknownHostException;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.List;
-import java.util.Set;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class InetDiagSocketTest {
- // ::FFFF:192.0.2.1
- private static final byte[] SRC_V4_MAPPED_V6_ADDRESS_BYTES = {
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01,
- };
- // ::FFFF:192.0.2.2
- private static final byte[] DST_V4_MAPPED_V6_ADDRESS_BYTES = {
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x02,
- };
-
- // Hexadecimal representation of InetDiagReqV2 request.
- private static final String INET_DIAG_REQ_V2_UDP_INET4_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0103" + // flags = NLM_F_REQUEST | NLM_F_DUMP
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "02" + // family = AF_INET
- "11" + // protcol = IPPROTO_UDP
- "00" + // idiag_ext
- "00" + // pad
- "ffffffff" + // idiag_states
- // inet_diag_sockid
- "a5de" + // idiag_sport = 42462
- "b971" + // idiag_dport = 47473
- "0a006402000000000000000000000000" + // idiag_src = 10.0.100.2
- "08080808000000000000000000000000" + // idiag_dst = 8.8.8.8
- "00000000" + // idiag_if
- "ffffffffffffffff"; // idiag_cookie = INET_DIAG_NOCOOKIE
- private static final byte[] INET_DIAG_REQ_V2_UDP_INET4_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_UDP_INET4_HEX.toCharArray(), false);
-
- @Test
- public void testInetDiagReqV2UdpInet4() throws Exception {
- InetSocketAddress local = new InetSocketAddress(InetAddress.getByName("10.0.100.2"),
- 42462);
- InetSocketAddress remote = new InetSocketAddress(InetAddress.getByName("8.8.8.8"),
- 47473);
- final byte[] msg = InetDiagMessage.inetDiagReqV2(IPPROTO_UDP, local, remote, AF_INET,
- (short) (NLM_F_REQUEST | NLM_F_DUMP));
- assertArrayEquals(INET_DIAG_REQ_V2_UDP_INET4_BYTES, msg);
- }
-
- // Hexadecimal representation of InetDiagReqV2 request.
- private static final String INET_DIAG_REQ_V2_TCP_INET6_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0100" + // flags = NLM_F_REQUEST
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "0a" + // family = AF_INET6
- "06" + // protcol = IPPROTO_TCP
- "00" + // idiag_ext
- "00" + // pad
- "ffffffff" + // idiag_states
- // inet_diag_sockid
- "a5de" + // idiag_sport = 42462
- "b971" + // idiag_dport = 47473
- "fe8000000000000086c9b2fffe6aed4b" + // idiag_src = fe80::86c9:b2ff:fe6a:ed4b
- "08080808000000000000000000000000" + // idiag_dst = 8.8.8.8
- "00000000" + // idiag_if
- "ffffffffffffffff"; // idiag_cookie = INET_DIAG_NOCOOKIE
- private static final byte[] INET_DIAG_REQ_V2_TCP_INET6_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_TCP_INET6_HEX.toCharArray(), false);
-
- @Test
- public void testInetDiagReqV2TcpInet6() throws Exception {
- InetSocketAddress local = new InetSocketAddress(
- InetAddress.getByName("fe80::86c9:b2ff:fe6a:ed4b"), 42462);
- InetSocketAddress remote = new InetSocketAddress(InetAddress.getByName("8.8.8.8"),
- 47473);
- byte[] msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, local, remote, AF_INET6,
- NLM_F_REQUEST);
-
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_BYTES, msg);
- }
-
- // Hexadecimal representation of InetDiagReqV2 request with extension, INET_DIAG_INFO.
- private static final String INET_DIAG_REQ_V2_TCP_INET_INET_DIAG_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0100" + // flags = NLM_F_REQUEST
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "02" + // family = AF_INET
- "06" + // protcol = IPPROTO_TCP
- "02" + // idiag_ext = INET_DIAG_INFO
- "00" + // pad
- "ffffffff" + // idiag_states
- // inet_diag_sockid
- "3039" + // idiag_sport = 12345
- "d431" + // idiag_dport = 54321
- "01020304000000000000000000000000" + // idiag_src = 1.2.3.4
- "08080404000000000000000000000000" + // idiag_dst = 8.8.4.4
- "00000000" + // idiag_if
- "ffffffffffffffff"; // idiag_cookie = INET_DIAG_NOCOOKIE
-
- private static final byte[] INET_DIAG_REQ_V2_TCP_INET_INET_DIAG_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_TCP_INET_INET_DIAG_HEX.toCharArray(), false);
- private static final int TCP_ALL_STATES = 0xffffffff;
- @Test
- public void testInetDiagReqV2TcpInetWithExt() throws Exception {
- InetSocketAddress local = new InetSocketAddress(
- InetAddress.getByName("1.2.3.4"), 12345);
- InetSocketAddress remote = new InetSocketAddress(InetAddress.getByName("8.8.4.4"),
- 54321);
- byte[] msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, local, remote, AF_INET,
- NLM_F_REQUEST, 0 /* pad */, 2 /* idiagExt */, TCP_ALL_STATES);
-
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET_INET_DIAG_BYTES, msg);
-
- local = new InetSocketAddress(
- InetAddress.getByName("fe80::86c9:b2ff:fe6a:ed4b"), 42462);
- remote = new InetSocketAddress(InetAddress.getByName("8.8.8.8"),
- 47473);
- msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, local, remote, AF_INET6,
- NLM_F_REQUEST, 0 /* pad */, 0 /* idiagExt */, TCP_ALL_STATES);
-
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_BYTES, msg);
- }
-
- // Hexadecimal representation of InetDiagReqV2 request with no socket specified.
- private static final String INET_DIAG_REQ_V2_TCP_INET6_NO_ID_SPECIFIED_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0100" + // flags = NLM_F_REQUEST
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "0a" + // family = AF_INET6
- "06" + // protcol = IPPROTO_TCP
- "00" + // idiag_ext
- "00" + // pad
- "ffffffff" + // idiag_states
- // inet_diag_sockid
- "0000" + // idiag_sport
- "0000" + // idiag_dport
- "00000000000000000000000000000000" + // idiag_src
- "00000000000000000000000000000000" + // idiag_dst
- "00000000" + // idiag_if
- "0000000000000000"; // idiag_cookie
-
- private static final byte[] INET_DIAG_REQ_V2_TCP_INET6_NO_ID_SPECIFIED_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_TCP_INET6_NO_ID_SPECIFIED_HEX.toCharArray(), false);
-
- @Test
- public void testInetDiagReqV2TcpInet6NoIdSpecified() throws Exception {
- InetSocketAddress local = new InetSocketAddress(
- InetAddress.getByName("fe80::fe6a:ed4b"), 12345);
- InetSocketAddress remote = new InetSocketAddress(InetAddress.getByName("8.8.4.4"),
- 54321);
- // Verify no socket specified if either local or remote socket address is null.
- byte[] msgExt = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, null, null, AF_INET6,
- NLM_F_REQUEST, 0 /* pad */, 0 /* idiagExt */, TCP_ALL_STATES);
- byte[] msg;
- try {
- msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, null, remote, AF_INET6,
- NLM_F_REQUEST);
- fail("Both remote and local should be null, expected UnknownHostException");
- } catch (IllegalArgumentException e) {
- }
-
- try {
- msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, local, null, AF_INET6,
- NLM_F_REQUEST, 0 /* pad */, 0 /* idiagExt */, TCP_ALL_STATES);
- fail("Both remote and local should be null, expected UnknownHostException");
- } catch (IllegalArgumentException e) {
- }
-
- msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, null, null, AF_INET6,
- NLM_F_REQUEST, 0 /* pad */, 0 /* idiagExt */, TCP_ALL_STATES);
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_NO_ID_SPECIFIED_BYTES, msg);
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_NO_ID_SPECIFIED_BYTES, msgExt);
- }
-
- // Hexadecimal representation of InetDiagReqV2 request with v4-mapped v6 address
- private static final String INET_DIAG_REQ_V2_TCP_INET6_V4_MAPPED_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0100" + // flags = NLM_F_REQUEST
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "0a" + // family = AF_INET6
- "06" + // protcol = IPPROTO_TCP
- "00" + // idiag_ext
- "00" + // pad
- "ffffffff" + // idiag_states
- // inet_diag_sockid
- "a817" + // idiag_sport = 43031
- "960f" + // idiag_dport = 38415
- "00000000000000000000ffffc0000201" + // idiag_src = ::FFFF:192.0.2.1
- "00000000000000000000ffffc0000202" + // idiag_dst = ::FFFF:192.0.2.2
- "00000000" + // idiag_if
- "ffffffffffffffff"; // idiag_cookie = INET_DIAG_NOCOOKIE
-
- private static final byte[] INET_DIAG_REQ_V2_TCP_INET6_V4_MAPPED_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_TCP_INET6_V4_MAPPED_HEX.toCharArray(), false);
-
- @Test
- public void testInetDiagReqV2TcpInet6V4Mapped() throws Exception {
- final Inet6Address srcAddr = Inet6Address.getByAddress(
- null /* host */, SRC_V4_MAPPED_V6_ADDRESS_BYTES, -1 /* scope_id */);
- final Inet6Address dstAddr = Inet6Address.getByAddress(
- null /* host */, DST_V4_MAPPED_V6_ADDRESS_BYTES, -1 /* scope_id */);
- final byte[] msg = InetDiagMessage.inetDiagReqV2(
- IPPROTO_TCP,
- new InetSocketAddress(srcAddr, 43031),
- new InetSocketAddress(dstAddr, 38415),
- AF_INET6,
- NLM_F_REQUEST);
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_V4_MAPPED_BYTES, msg);
- }
-
- // Hexadecimal representation of InetDiagReqV2 request with SOCK_DESTROY
- private static final String INET_DIAG_REQ_V2_TCP_INET6_DESTROY_HEX =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1500" + // type = SOCK_DESTROY
- "0500" + // flags = NLM_F_REQUEST | NLM_F_ACK
- "00000000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct inet_diag_req_v2
- "0a" + // family = AF_INET6
- "06" + // protcol = IPPROTO_TCP
- "00" + // idiag_ext
- "00" + // pad
- "ffffffff" + // idiag_states = TCP_ALL_STATES
- // inet_diag_sockid
- "a817" + // idiag_sport = 43031
- "960f" + // idiag_dport = 38415
- "20010db8000000000000000000000001" + // idiag_src = 2001:db8::1
- "20010db8000000000000000000000002" + // idiag_dst = 2001:db8::2
- "07000000" + // idiag_if = 7
- "5800000000000000"; // idiag_cookie = 88
-
- private static final byte[] INET_DIAG_REQ_V2_TCP_INET6_DESTROY_BYTES =
- HexEncoding.decode(INET_DIAG_REQ_V2_TCP_INET6_DESTROY_HEX.toCharArray(), false);
-
- @Test
- public void testInetDiagReqV2TcpInet6Destroy() throws Exception {
- final StructInetDiagSockId sockId = new StructInetDiagSockId(
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::1"), 43031),
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::2"), 38415),
- 7 /* ifIndex */,
- 88 /* cookie */);
- final byte[] msg = InetDiagMessage.inetDiagReqV2(IPPROTO_TCP, sockId, AF_INET6,
- SOCK_DESTROY, (short) (NLM_F_REQUEST | NLM_F_ACK), 0 /* pad */, 0 /* idiagExt */,
- TCP_ALL_STATES);
-
- assertArrayEquals(INET_DIAG_REQ_V2_TCP_INET6_DESTROY_BYTES, msg);
- }
-
- private void assertNlMsgHdr(StructNlMsgHdr hdr, short type, short flags, int seq, int pid) {
- assertNotNull(hdr);
- assertEquals(type, hdr.nlmsg_type);
- assertEquals(flags, hdr.nlmsg_flags);
- assertEquals(seq, hdr.nlmsg_seq);
- assertEquals(pid, hdr.nlmsg_pid);
- }
-
- private void assertInetDiagSockId(StructInetDiagSockId sockId,
- InetSocketAddress locSocketAddress, InetSocketAddress remSocketAddress,
- int ifIndex, long cookie) {
- assertEquals(locSocketAddress, sockId.locSocketAddress);
- assertEquals(remSocketAddress, sockId.remSocketAddress);
- assertEquals(ifIndex, sockId.ifIndex);
- assertEquals(cookie, sockId.cookie);
- }
-
- // Hexadecimal representation of InetDiagMessage
- private static final String INET_DIAG_MSG_HEX1 =
- // struct nlmsghdr
- "58000000" + // length = 88
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0200" + // flags = NLM_F_MULTI
- "00000000" + // seqno
- "f5220000" + // pid
- // struct inet_diag_msg
- "0a" + // family = AF_INET6
- "01" + // idiag_state = 1
- "02" + // idiag_timer = 2
- "ff" + // idiag_retrans = 255
- // inet_diag_sockid
- "a817" + // idiag_sport = 43031
- "960f" + // idiag_dport = 38415
- "20010db8000000000000000000000001" + // idiag_src = 2001:db8::1
- "20010db8000000000000000000000002" + // idiag_dst = 2001:db8::2
- "07000000" + // idiag_if = 7
- "5800000000000000" + // idiag_cookie = 88
- "04000000" + // idiag_expires = 4
- "05000000" + // idiag_rqueue = 5
- "06000000" + // idiag_wqueue = 6
- "a3270000" + // idiag_uid = 10147
- "a57e19f0"; // idiag_inode = 4028202661
-
- private void assertInetDiagMsg1(final NetlinkMessage msg) {
- assertNotNull(msg);
-
- assertTrue(msg instanceof InetDiagMessage);
- final InetDiagMessage inetDiagMsg = (InetDiagMessage) msg;
-
- assertNlMsgHdr(inetDiagMsg.getHeader(),
- NetlinkConstants.SOCK_DIAG_BY_FAMILY,
- StructNlMsgHdr.NLM_F_MULTI,
- 0 /* seq */,
- 8949 /* pid */);
-
- assertEquals(AF_INET6, inetDiagMsg.inetDiagMsg.idiag_family);
- assertEquals(1, inetDiagMsg.inetDiagMsg.idiag_state);
- assertEquals(2, inetDiagMsg.inetDiagMsg.idiag_timer);
- assertEquals(255, inetDiagMsg.inetDiagMsg.idiag_retrans);
- assertInetDiagSockId(inetDiagMsg.inetDiagMsg.id,
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::1"), 43031),
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::2"), 38415),
- 7 /* ifIndex */,
- 88 /* cookie */);
- assertEquals(4, inetDiagMsg.inetDiagMsg.idiag_expires);
- assertEquals(5, inetDiagMsg.inetDiagMsg.idiag_rqueue);
- assertEquals(6, inetDiagMsg.inetDiagMsg.idiag_wqueue);
- assertEquals(10147, inetDiagMsg.inetDiagMsg.idiag_uid);
- assertEquals(4028202661L, inetDiagMsg.inetDiagMsg.idiag_inode);
- }
-
- // Hexadecimal representation of InetDiagMessage
- private static final String INET_DIAG_MSG_HEX2 =
- // struct nlmsghdr
- "58000000" + // length = 88
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0200" + // flags = NLM_F_MULTI
- "00000000" + // seqno
- "f5220000" + // pid
- // struct inet_diag_msg
- "0a" + // family = AF_INET6
- "02" + // idiag_state = 2
- "10" + // idiag_timer = 16
- "20" + // idiag_retrans = 32
- // inet_diag_sockid
- "a845" + // idiag_sport = 43077
- "01bb" + // idiag_dport = 443
- "20010db8000000000000000000000003" + // idiag_src = 2001:db8::3
- "20010db8000000000000000000000004" + // idiag_dst = 2001:db8::4
- "08000000" + // idiag_if = 8
- "6300000000000000" + // idiag_cookie = 99
- "30000000" + // idiag_expires = 48
- "40000000" + // idiag_rqueue = 64
- "50000000" + // idiag_wqueue = 80
- "39300000" + // idiag_uid = 12345
- "851a0000"; // idiag_inode = 6789
-
- private void assertInetDiagMsg2(final NetlinkMessage msg) {
- assertNotNull(msg);
-
- assertTrue(msg instanceof InetDiagMessage);
- final InetDiagMessage inetDiagMsg = (InetDiagMessage) msg;
-
- assertNlMsgHdr(inetDiagMsg.getHeader(),
- NetlinkConstants.SOCK_DIAG_BY_FAMILY,
- StructNlMsgHdr.NLM_F_MULTI,
- 0 /* seq */,
- 8949 /* pid */);
-
- assertEquals(AF_INET6, inetDiagMsg.inetDiagMsg.idiag_family);
- assertEquals(2, inetDiagMsg.inetDiagMsg.idiag_state);
- assertEquals(16, inetDiagMsg.inetDiagMsg.idiag_timer);
- assertEquals(32, inetDiagMsg.inetDiagMsg.idiag_retrans);
- assertInetDiagSockId(inetDiagMsg.inetDiagMsg.id,
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::3"), 43077),
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::4"), 443),
- 8 /* ifIndex */,
- 99 /* cookie */);
- assertEquals(48, inetDiagMsg.inetDiagMsg.idiag_expires);
- assertEquals(64, inetDiagMsg.inetDiagMsg.idiag_rqueue);
- assertEquals(80, inetDiagMsg.inetDiagMsg.idiag_wqueue);
- assertEquals(12345, inetDiagMsg.inetDiagMsg.idiag_uid);
- assertEquals(6789, inetDiagMsg.inetDiagMsg.idiag_inode);
- }
-
- private static final byte[] INET_DIAG_MSG_BYTES =
- HexEncoding.decode(INET_DIAG_MSG_HEX1.toCharArray(), false);
-
- @Test
- public void testParseInetDiagResponse() throws Exception {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(INET_DIAG_MSG_BYTES);
- byteBuffer.order(ByteOrder.nativeOrder());
- assertInetDiagMsg1(NetlinkMessage.parse(byteBuffer, NETLINK_INET_DIAG));
- }
-
-
- private static final byte[] INET_DIAG_MSG_BYTES_MULTIPLE =
- HexEncoding.decode((INET_DIAG_MSG_HEX1 + INET_DIAG_MSG_HEX2).toCharArray(), false);
-
- @Test
- public void testParseInetDiagResponseMultiple() {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(INET_DIAG_MSG_BYTES_MULTIPLE);
- byteBuffer.order(ByteOrder.nativeOrder());
- assertInetDiagMsg1(NetlinkMessage.parse(byteBuffer, NETLINK_INET_DIAG));
- assertInetDiagMsg2(NetlinkMessage.parse(byteBuffer, NETLINK_INET_DIAG));
- }
-
- private static final String INET_DIAG_SOCK_ID_V4_MAPPED_V6_HEX =
- "a845" + // idiag_sport = 43077
- "01bb" + // idiag_dport = 443
- "00000000000000000000ffffc0000201" + // idiag_src = ::FFFF:192.0.2.1
- "00000000000000000000ffffc0000202" + // idiag_dst = ::FFFF:192.0.2.2
- "08000000" + // idiag_if = 8
- "6300000000000000"; // idiag_cookie = 99
-
- private static final byte[] INET_DIAG_SOCK_ID_V4_MAPPED_V6_BYTES =
- HexEncoding.decode(INET_DIAG_SOCK_ID_V4_MAPPED_V6_HEX.toCharArray(), false);
-
- @Test
- public void testParseAndPackInetDiagSockIdV4MappedV6() {
- final ByteBuffer parseByteBuffer = ByteBuffer.wrap(INET_DIAG_SOCK_ID_V4_MAPPED_V6_BYTES);
- parseByteBuffer.order(ByteOrder.nativeOrder());
- final StructInetDiagSockId diagSockId =
- StructInetDiagSockId.parse(parseByteBuffer, (short) AF_INET6);
- assertNotNull(diagSockId);
-
- final ByteBuffer packByteBuffer =
- ByteBuffer.allocate(INET_DIAG_SOCK_ID_V4_MAPPED_V6_BYTES.length);
- diagSockId.pack(packByteBuffer);
-
- // Move position to the head since ByteBuffer#equals compares the values from the current
- // position.
- parseByteBuffer.position(0);
- packByteBuffer.position(0);
- assertEquals(parseByteBuffer, packByteBuffer);
- }
-
- // Hexadecimal representation of InetDiagMessage with v4-mapped v6 address
- private static final String INET_DIAG_MSG_V4_MAPPED_V6_HEX =
- // struct nlmsghdr
- "58000000" + // length = 88
- "1400" + // type = SOCK_DIAG_BY_FAMILY
- "0200" + // flags = NLM_F_MULTI
- "00000000" + // seqno
- "f5220000" + // pid
- // struct inet_diag_msg
- "0a" + // family = AF_INET6
- "01" + // idiag_state = 1
- "02" + // idiag_timer = 2
- "03" + // idiag_retrans = 3
- // inet_diag_sockid
- "a817" + // idiag_sport = 43031
- "960f" + // idiag_dport = 38415
- "00000000000000000000ffffc0000201" + // idiag_src = ::FFFF:192.0.2.1
- "00000000000000000000ffffc0000202" + // idiag_dst = ::FFFF:192.0.2.2
- "07000000" + // idiag_if = 7
- "5800000000000000" + // idiag_cookie = 88
- "04000000" + // idiag_expires = 4
- "05000000" + // idiag_rqueue = 5
- "06000000" + // idiag_wqueue = 6
- "a3270000" + // idiag_uid = 10147
- "A57E1900"; // idiag_inode = 1670821
-
- private static final byte[] INET_DIAG_MSG_V4_MAPPED_V6_BYTES =
- HexEncoding.decode(INET_DIAG_MSG_V4_MAPPED_V6_HEX.toCharArray(), false);
-
- @Test
- public void testParseInetDiagResponseV4MappedV6() throws Exception {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(INET_DIAG_MSG_V4_MAPPED_V6_BYTES);
- byteBuffer.order(ByteOrder.nativeOrder());
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_INET_DIAG);
-
- assertNotNull(msg);
- assertTrue(msg instanceof InetDiagMessage);
- final InetDiagMessage inetDiagMsg = (InetDiagMessage) msg;
- final Inet6Address srcAddr = Inet6Address.getByAddress(
- null /* host */, SRC_V4_MAPPED_V6_ADDRESS_BYTES, -1 /* scope_id */);
- final Inet6Address dstAddr = Inet6Address.getByAddress(
- null /* host */, DST_V4_MAPPED_V6_ADDRESS_BYTES, -1 /* scope_id */);
- assertInetDiagSockId(inetDiagMsg.inetDiagMsg.id,
- new InetSocketAddress(srcAddr, 43031),
- new InetSocketAddress(dstAddr, 38415),
- 7 /* ifIndex */,
- 88 /* cookie */);
- }
-
- private void doTestIsLoopback(InetAddress srcAddr, InetAddress dstAddr, boolean expected) {
- final InetDiagMessage inetDiagMsg = new InetDiagMessage(new StructNlMsgHdr());
- inetDiagMsg.inetDiagMsg.id = new StructInetDiagSockId(
- new InetSocketAddress(srcAddr, 43031),
- new InetSocketAddress(dstAddr, 38415)
- );
-
- assertEquals(expected, InetDiagMessage.isLoopback(inetDiagMsg));
- }
-
- @Test
- public void testIsLoopback() {
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("127.0.0.1"),
- InetAddresses.parseNumericAddress("192.0.2.1"),
- true
- );
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("192.0.2.1"),
- InetAddresses.parseNumericAddress("127.7.7.7"),
- true
- );
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("::1"),
- InetAddresses.parseNumericAddress("::1"),
- true
- );
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("::1"),
- InetAddresses.parseNumericAddress("2001:db8::1"),
- true
- );
- }
-
- @Test
- public void testIsLoopbackSameSrcDstAddress() {
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("192.0.2.1"),
- InetAddresses.parseNumericAddress("192.0.2.1"),
- true
- );
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("2001:db8::1"),
- InetAddresses.parseNumericAddress("2001:db8::1"),
- true
- );
- }
-
- @Test
- public void testIsLoopbackNonLoopbackSocket() {
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("192.0.2.1"),
- InetAddresses.parseNumericAddress("192.0.2.2"),
- false
- );
- doTestIsLoopback(
- InetAddresses.parseNumericAddress("2001:db8::1"),
- InetAddresses.parseNumericAddress("2001:db8::2"),
- false
- );
- }
-
- @Test
- public void testIsLoopbackV4MappedV6() throws UnknownHostException {
- // ::FFFF:127.1.2.3
- final byte[] addrLoopbackByte = {
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
- (byte) 0x7f, (byte) 0x01, (byte) 0x02, (byte) 0x03,
- };
- // ::FFFF:192.0.2.1
- final byte[] addrNonLoopbackByte1 = {
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01,
- };
- // ::FFFF:192.0.2.2
- final byte[] addrNonLoopbackByte2 = {
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0xff, (byte) 0xff,
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x02,
- };
-
- final Inet6Address addrLoopback = Inet6Address.getByAddress(null, addrLoopbackByte, -1);
- final Inet6Address addrNonLoopback1 =
- Inet6Address.getByAddress(null, addrNonLoopbackByte1, -1);
- final Inet6Address addrNonLoopback2 =
- Inet6Address.getByAddress(null, addrNonLoopbackByte2, -1);
-
- doTestIsLoopback(addrLoopback, addrNonLoopback1, true);
- doTestIsLoopback(addrNonLoopback1, addrNonLoopback2, false);
- doTestIsLoopback(addrNonLoopback1, addrNonLoopback1, true);
- }
-
- private void doTestContainsUid(final int uid, final Set<Range<Integer>> ranges,
- final boolean expected) {
- final InetDiagMessage inetDiagMsg = new InetDiagMessage(new StructNlMsgHdr());
- inetDiagMsg.inetDiagMsg.idiag_uid = uid;
- assertEquals(expected, InetDiagMessage.containsUid(inetDiagMsg, ranges));
- }
-
- @Test
- public void testContainsUid() {
- doTestContainsUid(77 /* uid */,
- new ArraySet<>(List.of(new Range<>(0, 100))),
- true /* expected */);
- doTestContainsUid(77 /* uid */,
- new ArraySet<>(List.of(new Range<>(77, 77), new Range<>(100, 200))),
- true /* expected */);
-
- doTestContainsUid(77 /* uid */,
- new ArraySet<>(List.of(new Range<>(100, 200))),
- false /* expected */);
- doTestContainsUid(77 /* uid */,
- new ArraySet<>(List.of(new Range<>(0, 76), new Range<>(78, 100))),
- false /* expected */);
- }
-
- private void doTestIsAdbSocket(final int uid, final boolean expected) {
- final InetDiagMessage inetDiagMsg = new InetDiagMessage(new StructNlMsgHdr());
- inetDiagMsg.inetDiagMsg.idiag_uid = uid;
- inetDiagMsg.inetDiagMsg.id = new StructInetDiagSockId(
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::1"), 38417),
- new InetSocketAddress(InetAddresses.parseNumericAddress("2001:db8::2"), 38415)
- );
- assertEquals(expected, InetDiagMessage.isAdbSocket(inetDiagMsg));
- }
-
- @Test
- public void testIsAdbSocket() {
- final int appUid = 10108;
- doTestIsAdbSocket(SHELL_UID, true /* expected */);
- doTestIsAdbSocket(ROOT_UID, false /* expected */);
- doTestIsAdbSocket(appUid, false /* expected */);
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/NduseroptMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/NduseroptMessageTest.java
deleted file mode 100644
index 4fc5ec2e..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/NduseroptMessageTest.java
+++ /dev/null
@@ -1,295 +0,0 @@
-/*
- * Copyright (C) 2020 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.netlink;
-
-import static android.net.InetAddresses.parseNumericAddress;
-import static android.system.OsConstants.AF_INET6;
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import android.net.InetAddresses;
-import android.net.IpPrefix;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet6Address;
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class NduseroptMessageTest {
-
- private static final byte ICMP_TYPE_RA = (byte) 134;
-
- private static final int IFINDEX1 = 15715755;
- private static final int IFINDEX2 = 1431655765;
-
- // IPv6, 0 bytes of options, interface index 15715755, type 134 (RA), code 0, padding.
- private static final String HDR_EMPTY = "0a00" + "0000" + "abcdef00" + "8600000000000000";
-
- // IPv6, 16 bytes of options, interface index 1431655765, type 134 (RA), code 0, padding.
- private static final String HDR_16BYTE = "0a00" + "1000" + "55555555" + "8600000000000000";
-
- // IPv6, 32 bytes of options, interface index 1431655765, type 134 (RA), code 0, padding.
- private static final String HDR_32BYTE = "0a00" + "2000" + "55555555" + "8600000000000000";
-
- // PREF64 option, 2001:db8:3:4:5:6::/96, lifetime=10064
- private static final String OPT_PREF64 = "2602" + "2750" + "20010db80003000400050006";
-
- // Length 20, NDUSEROPT_SRCADDR, fe80:2:3:4:5:6:7:8
- private static final String NLA_SRCADDR = "1400" + "0100" + "fe800002000300040005000600070008";
-
- private static final InetAddress SADDR1 = parseNumericAddress("fe80:2:3:4:5:6:7:8%" + IFINDEX1);
- private static final InetAddress SADDR2 = parseNumericAddress("fe80:2:3:4:5:6:7:8%" + IFINDEX2);
-
- private static final String MSG_EMPTY = HDR_EMPTY + NLA_SRCADDR;
- private static final String MSG_PREF64 = HDR_16BYTE + OPT_PREF64 + NLA_SRCADDR;
-
- @Test
- public void testParsing() {
- NduseroptMessage msg = parseNduseroptMessage(toBuffer(MSG_EMPTY));
- assertMatches(AF_INET6, 0, IFINDEX1, ICMP_TYPE_RA, (byte) 0, SADDR1, msg);
- assertNull(msg.option);
-
- msg = parseNduseroptMessage(toBuffer(MSG_PREF64));
- assertMatches(AF_INET6, 16, IFINDEX2, ICMP_TYPE_RA, (byte) 0, SADDR2, msg);
- assertPref64Option("2001:db8:3:4:5:6::/96", msg.option);
- }
-
- @Test
- public void testParseWithinNetlinkMessage() throws Exception {
- // A NduseroptMessage inside a netlink message. Ensure that it parses the same way both by
- // parsing the netlink message via NetlinkMessage.parse() and by parsing the option itself
- // with NduseroptMessage.parse().
- final String hexBytes =
- "44000000440000000000000000000000" // len=68, RTM_NEWNDUSEROPT
- + "0A0010001E0000008600000000000000" // IPv6, opt_bytes=16, ifindex=30, RA
- + "260202580064FF9B0000000000000000" // pref64, prefix=64:ff9b::/96, 600
- + "14000100FE800000000000000250B6FFFEB7C499"; // srcaddr=fe80::250:b6ff:feb7:c499
-
- ByteBuffer buf = toBuffer(hexBytes);
- assertEquals(68, buf.limit());
- buf.order(ByteOrder.nativeOrder());
-
- NetlinkMessage nlMsg = NetlinkMessage.parse(buf, NETLINK_ROUTE);
- assertNotNull(nlMsg);
- assertTrue(nlMsg instanceof NduseroptMessage);
-
- NduseroptMessage msg = (NduseroptMessage) nlMsg;
- InetAddress srcaddr = InetAddress.getByName("fe80::250:b6ff:feb7:c499%30");
- assertMatches(AF_INET6, 16, 30, ICMP_TYPE_RA, (byte) 0, srcaddr, msg);
- assertPref64Option("64:ff9b::/96", msg.option);
-
- final String hexBytesWithoutHeader = hexBytes.substring(StructNlMsgHdr.STRUCT_SIZE * 2);
- ByteBuffer bufWithoutHeader = toBuffer(hexBytesWithoutHeader);
- assertEquals(52, bufWithoutHeader.limit());
- msg = parseNduseroptMessage(bufWithoutHeader);
- assertMatches(AF_INET6, 16, 30, ICMP_TYPE_RA, (byte) 0, srcaddr, msg);
- assertPref64Option("64:ff9b::/96", msg.option);
- }
-
- @Test
- public void testParseRdnssOptionWithinNetlinkMessage() throws Exception {
- final String hexBytes =
- "4C000000440000000000000000000000"
- + "0A0018001E0000008600000000000000"
- + "1903000000001770FD123456789000000000000000000001" // RDNSS option
- + "14000100FE800000000000000250B6FFFEB7C499";
-
- ByteBuffer buf = toBuffer(hexBytes);
- assertEquals(76, buf.limit());
- buf.order(ByteOrder.nativeOrder());
-
- NetlinkMessage nlMsg = NetlinkMessage.parse(buf, NETLINK_ROUTE);
- assertNotNull(nlMsg);
- assertTrue(nlMsg instanceof NduseroptMessage);
-
- NduseroptMessage msg = (NduseroptMessage) nlMsg;
- InetAddress srcaddr = InetAddress.getByName("fe80::250:b6ff:feb7:c499%30");
- assertMatches(AF_INET6, 24, 30, ICMP_TYPE_RA, (byte) 0, srcaddr, msg);
- assertRdnssOption(msg.option, 6000 /* lifetime */,
- (Inet6Address) InetAddresses.parseNumericAddress("fd12:3456:7890::1"));
- }
-
- @Test
- public void testParseTruncatedRdnssOptionWithinNetlinkMessage() throws Exception {
- final String truncatedHexBytes =
- "38000000440000000000000000000000"
- + "0A0018001E0000008600000000000000"
- + "1903000000001770FD123456789000000000000000000001"; // RDNSS option
-
- ByteBuffer buf = toBuffer(truncatedHexBytes);
- buf.order(ByteOrder.nativeOrder());
- NetlinkMessage nlMsg = NetlinkMessage.parse(buf, NETLINK_ROUTE);
- assertNull(nlMsg);
- }
-
- @Test
- public void testParseUnknownOptionWithinNetlinkMessage() throws Exception {
- final String hexBytes =
- "4C000000440000000000000000000000"
- + "0A0018001E0000008600000000000000"
- + "310300000000177006676F6F676C652E03636F6D00000000" // DNSSL option: "google.com"
- + "14000100FE800000000000000250B6FFFEB7C499";
-
- ByteBuffer buf = toBuffer(hexBytes);
- assertEquals(76, buf.limit());
- buf.order(ByteOrder.nativeOrder());
-
- NetlinkMessage nlMsg = NetlinkMessage.parse(buf, NETLINK_ROUTE);
- assertNotNull(nlMsg);
- assertTrue(nlMsg instanceof NduseroptMessage);
-
- NduseroptMessage msg = (NduseroptMessage) nlMsg;
- InetAddress srcaddr = InetAddress.getByName("fe80::250:b6ff:feb7:c499%30");
- assertMatches(AF_INET6, 24, 30, ICMP_TYPE_RA, (byte) 0, srcaddr, msg);
- assertEquals(NdOption.UNKNOWN, msg.option);
- }
-
- @Test
- public void testUnknownOption() {
- ByteBuffer buf = toBuffer(MSG_PREF64);
- // Replace the PREF64 option type (38) with an unknown option number.
- final int optionStart = NduseroptMessage.STRUCT_SIZE;
- assertEquals(38, buf.get(optionStart));
- buf.put(optionStart, (byte) 42);
-
- NduseroptMessage msg = parseNduseroptMessage(buf);
- assertMatches(AF_INET6, 16, IFINDEX2, ICMP_TYPE_RA, (byte) 0, SADDR2, msg);
- assertEquals(NdOption.UNKNOWN, msg.option);
-
- buf.flip();
- assertEquals(42, buf.get(optionStart));
- buf.put(optionStart, (byte) 38);
-
- msg = parseNduseroptMessage(buf);
- assertMatches(AF_INET6, 16, IFINDEX2, ICMP_TYPE_RA, (byte) 0, SADDR2, msg);
- assertPref64Option("2001:db8:3:4:5:6::/96", msg.option);
- }
-
- @Test
- public void testZeroLengthOption() {
- // Make sure an unknown option with a 0-byte length is ignored and parsing continues with
- // the address, which comes after it.
- final String hexString = HDR_16BYTE + "00000000000000000000000000000000" + NLA_SRCADDR;
- ByteBuffer buf = toBuffer(hexString);
- assertEquals(52, buf.limit());
- NduseroptMessage msg = parseNduseroptMessage(buf);
- assertMatches(AF_INET6, 16, IFINDEX2, ICMP_TYPE_RA, (byte) 0, SADDR2, msg);
- assertNull(msg.option);
- }
-
- @Test
- public void testTooLongOption() {
- // Make sure that if an option's length is too long, it's ignored and parsing continues with
- // the address, which comes after it.
- final String hexString = HDR_16BYTE + "26030000000000000000000000000000" + NLA_SRCADDR;
- ByteBuffer buf = toBuffer(hexString);
- assertEquals(52, buf.limit());
- NduseroptMessage msg = parseNduseroptMessage(buf);
- assertMatches(AF_INET6, 16, IFINDEX2, ICMP_TYPE_RA, (byte) 0, SADDR2, msg);
- assertNull(msg.option);
- }
-
- @Test
- public void testOptionsTooLong() {
- // Header claims 32 bytes of options. Buffer ends before options end.
- String hexString = HDR_32BYTE + OPT_PREF64;
- ByteBuffer buf = toBuffer(hexString);
- assertEquals(32, buf.limit());
- assertNull(NduseroptMessage.parse(toBuffer(hexString), NETLINK_ROUTE));
-
- // Header claims 32 bytes of options. Buffer ends at end of options with no source address.
- hexString = HDR_32BYTE + OPT_PREF64 + OPT_PREF64;
- buf = toBuffer(hexString);
- assertEquals(48, buf.limit());
- assertNull(NduseroptMessage.parse(toBuffer(hexString), NETLINK_ROUTE));
- }
-
- @Test
- public void testTruncation() {
- final int optLen = MSG_PREF64.length() / 2; // 1 byte = 2 hex chars
- for (int len = 0; len < optLen; len++) {
- ByteBuffer buf = toBuffer(MSG_PREF64.substring(0, len * 2));
- NduseroptMessage msg = parseNduseroptMessage(buf);
- if (len < optLen) {
- assertNull(msg);
- } else {
- assertNotNull(msg);
- assertPref64Option("2001:db8:3:4:5:6::/96", msg.option);
- }
- }
- }
-
- @Test
- public void testToString() {
- NduseroptMessage msg = parseNduseroptMessage(toBuffer(MSG_PREF64));
- assertNotNull(msg);
- assertEquals("Nduseroptmsg(10, 16, 1431655765, 134, 0, fe80:2:3:4:5:6:7:8%1431655765)",
- msg.toString());
- }
-
- // Convenience method to parse a NduseroptMessage that's not part of a netlink message.
- private NduseroptMessage parseNduseroptMessage(ByteBuffer buf) {
- return NduseroptMessage.parse(null, buf);
- }
-
- private ByteBuffer toBuffer(String hexString) {
- return ByteBuffer.wrap(HexEncoding.decode(hexString));
- }
-
- private void assertMatches(int family, int optsLen, int ifindex, byte icmpType,
- byte icmpCode, InetAddress srcaddr, NduseroptMessage msg) {
- assertNotNull(msg);
- assertEquals(family, msg.family);
- assertEquals(ifindex, msg.ifindex);
- assertEquals(optsLen, msg.opts_len);
- assertEquals(icmpType, msg.icmp_type);
- assertEquals(icmpCode, msg.icmp_code);
- assertEquals(srcaddr, msg.srcaddr);
- }
-
- private void assertPref64Option(String prefix, NdOption opt) {
- assertNotNull(opt);
- assertTrue(opt instanceof StructNdOptPref64);
- StructNdOptPref64 pref64Opt = (StructNdOptPref64) opt;
- assertEquals(new IpPrefix(prefix), pref64Opt.prefix);
- }
-
- private void assertRdnssOption(NdOption opt, long lifetime, Inet6Address... servers) {
- assertNotNull(opt);
- assertTrue(opt instanceof StructNdOptRdnss);
- StructNdOptRdnss rdnss = (StructNdOptRdnss) opt;
- assertEquals(StructNdOptRdnss.TYPE, rdnss.type);
- assertEquals((byte) (servers.length * 2 + 1), rdnss.header.length);
- assertEquals(lifetime, rdnss.header.lifetime);
- assertArrayEquals(servers, rdnss.servers);
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkConstantsTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkConstantsTest.java
deleted file mode 100644
index 143e4d4a..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkConstantsTest.java
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * Copyright (C) 2020 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.netlink;
-
-import static android.system.OsConstants.NETLINK_INET_DIAG;
-import static android.system.OsConstants.NETLINK_NETFILTER;
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_DELETE;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_CTRZERO;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_DYING;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_STATS;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_STATS_CPU;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_UNCONFIRMED;
-import static com.android.net.module.util.netlink.NetlinkConstants.IPCTNL_MSG_CT_NEW;
-import static com.android.net.module.util.netlink.NetlinkConstants.NFNL_SUBSYS_CTNETLINK;
-import static com.android.net.module.util.netlink.NetlinkConstants.NLMSG_DONE;
-import static com.android.net.module.util.netlink.NetlinkConstants.NLMSG_ERROR;
-import static com.android.net.module.util.netlink.NetlinkConstants.NLMSG_NOOP;
-import static com.android.net.module.util.netlink.NetlinkConstants.NLMSG_OVERRUN;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_DELADDR;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_DELLINK;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_DELNEIGH;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_DELROUTE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_DELRULE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_GETADDR;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_GETLINK;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_GETNEIGH;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_GETROUTE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_GETRULE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWADDR;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWLINK;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWNDUSEROPT;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWNEIGH;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWROUTE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_NEWRULE;
-import static com.android.net.module.util.netlink.NetlinkConstants.RTM_SETLINK;
-import static com.android.net.module.util.netlink.NetlinkConstants.SOCK_DIAG_BY_FAMILY;
-import static com.android.net.module.util.netlink.NetlinkConstants.stringForNlMsgType;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class NetlinkConstantsTest {
- private static final short UNKNOWN_FAMILY = 1234;
-
- private short makeCtType(short msgType) {
- return (short) (NFNL_SUBSYS_CTNETLINK << 8 | (byte) msgType);
- }
-
- @Test
- public void testStringForNlMsgType() {
- assertEquals("RTM_NEWLINK", stringForNlMsgType(RTM_NEWLINK, NETLINK_ROUTE));
- assertEquals("RTM_DELLINK", stringForNlMsgType(RTM_DELLINK, NETLINK_ROUTE));
- assertEquals("RTM_GETLINK", stringForNlMsgType(RTM_GETLINK, NETLINK_ROUTE));
- assertEquals("RTM_SETLINK", stringForNlMsgType(RTM_SETLINK, NETLINK_ROUTE));
- assertEquals("RTM_NEWADDR", stringForNlMsgType(RTM_NEWADDR, NETLINK_ROUTE));
- assertEquals("RTM_DELADDR", stringForNlMsgType(RTM_DELADDR, NETLINK_ROUTE));
- assertEquals("RTM_GETADDR", stringForNlMsgType(RTM_GETADDR, NETLINK_ROUTE));
- assertEquals("RTM_NEWROUTE", stringForNlMsgType(RTM_NEWROUTE, NETLINK_ROUTE));
- assertEquals("RTM_DELROUTE", stringForNlMsgType(RTM_DELROUTE, NETLINK_ROUTE));
- assertEquals("RTM_GETROUTE", stringForNlMsgType(RTM_GETROUTE, NETLINK_ROUTE));
- assertEquals("RTM_NEWNEIGH", stringForNlMsgType(RTM_NEWNEIGH, NETLINK_ROUTE));
- assertEquals("RTM_DELNEIGH", stringForNlMsgType(RTM_DELNEIGH, NETLINK_ROUTE));
- assertEquals("RTM_GETNEIGH", stringForNlMsgType(RTM_GETNEIGH, NETLINK_ROUTE));
- assertEquals("RTM_NEWRULE", stringForNlMsgType(RTM_NEWRULE, NETLINK_ROUTE));
- assertEquals("RTM_DELRULE", stringForNlMsgType(RTM_DELRULE, NETLINK_ROUTE));
- assertEquals("RTM_GETRULE", stringForNlMsgType(RTM_GETRULE, NETLINK_ROUTE));
- assertEquals("RTM_NEWNDUSEROPT", stringForNlMsgType(RTM_NEWNDUSEROPT, NETLINK_ROUTE));
-
- assertEquals("SOCK_DIAG_BY_FAMILY",
- stringForNlMsgType(SOCK_DIAG_BY_FAMILY, NETLINK_INET_DIAG));
-
- assertEquals("IPCTNL_MSG_CT_NEW",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_NEW), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_DELETE",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_DELETE), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET_CTRZERO",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_CTRZERO), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET_STATS_CPU",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_STATS_CPU), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET_STATS",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_STATS), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET_DYING",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_DYING), NETLINK_NETFILTER));
- assertEquals("IPCTNL_MSG_CT_GET_UNCONFIRMED",
- stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_UNCONFIRMED), NETLINK_NETFILTER));
- }
-
- @Test
- public void testStringForNlMsgType_ControlMessage() {
- for (int family : new int[]{NETLINK_ROUTE, NETLINK_INET_DIAG, NETLINK_NETFILTER}) {
- assertEquals("NLMSG_NOOP", stringForNlMsgType(NLMSG_NOOP, family));
- assertEquals("NLMSG_ERROR", stringForNlMsgType(NLMSG_ERROR, family));
- assertEquals("NLMSG_DONE", stringForNlMsgType(NLMSG_DONE, family));
- assertEquals("NLMSG_OVERRUN", stringForNlMsgType(NLMSG_OVERRUN, family));
- }
- }
-
- @Test
- public void testStringForNlMsgType_UnknownFamily() {
- assertTrue(stringForNlMsgType(RTM_NEWLINK, UNKNOWN_FAMILY).startsWith("unknown"));
- assertTrue(stringForNlMsgType(SOCK_DIAG_BY_FAMILY, UNKNOWN_FAMILY).startsWith("unknown"));
- assertTrue(stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_NEW), UNKNOWN_FAMILY)
- .startsWith("unknown"));
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkErrorMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkErrorMessageTest.java
deleted file mode 100644
index ab7d9cf5..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkErrorMessageTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2015 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.netlink;
-
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_ACK;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_REPLACE;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_REQUEST;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class NetlinkErrorMessageTest {
- private static final String TAG = "NetlinkErrorMessageTest";
-
- // Hexadecimal representation of packet capture.
- public static final String NLM_ERROR_OK_HEX =
- // struct nlmsghdr
- "24000000" + // length = 36
- "0200" + // type = 2 (NLMSG_ERROR)
- "0000" + // flags
- "26350000" + // seqno
- "64100000" + // pid = userspace process
- // error integer
- "00000000" + // "errno" (0 == OK)
- // struct nlmsghdr
- "30000000" + // length (48) of original request
- "1C00" + // type = 28 (RTM_NEWNEIGH)
- "0501" + // flags (NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE)
- "26350000" + // seqno
- "00000000"; // pid = kernel
- public static final byte[] NLM_ERROR_OK =
- HexEncoding.decode(NLM_ERROR_OK_HEX.toCharArray(), false);
-
- @Test
- public void testParseNlmErrorOk() {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(NLM_ERROR_OK);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof NetlinkErrorMessage);
- final NetlinkErrorMessage errorMsg = (NetlinkErrorMessage) msg;
-
- final StructNlMsgHdr hdr = errorMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(36, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.NLMSG_ERROR, hdr.nlmsg_type);
- assertEquals(0, hdr.nlmsg_flags);
- assertEquals(13606, hdr.nlmsg_seq);
- assertEquals(4196, hdr.nlmsg_pid);
-
- final StructNlMsgErr err = errorMsg.getNlMsgError();
- assertNotNull(err);
- assertEquals(0, err.error);
- assertNotNull(err.msg);
- assertEquals(48, err.msg.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_NEWNEIGH, err.msg.nlmsg_type);
- assertEquals((NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE), err.msg.nlmsg_flags);
- assertEquals(13606, err.msg.nlmsg_seq);
- assertEquals(0, err.msg.nlmsg_pid);
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkUtilsTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkUtilsTest.java
deleted file mode 100644
index 3a72dd1b..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/NetlinkUtilsTest.java
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- * Copyright (C) 2015 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.netlink;
-
-import static android.system.OsConstants.AF_INET;
-import static android.system.OsConstants.AF_INET6;
-import static android.system.OsConstants.AF_UNSPEC;
-import static android.system.OsConstants.EACCES;
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static com.android.net.module.util.netlink.NetlinkUtils.DEFAULT_RECV_BUFSIZE;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP;
-import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_REQUEST;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-import static org.junit.Assume.assumeFalse;
-
-import android.content.Context;
-import android.system.ErrnoException;
-import android.system.NetlinkSocketAddress;
-import android.system.Os;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.platform.app.InstrumentationRegistry;
-import androidx.test.runner.AndroidJUnit4;
-
-import com.android.modules.utils.build.SdkLevel;
-import com.android.net.module.util.Struct;
-
-import libcore.io.IoUtils;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.io.FileDescriptor;
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class NetlinkUtilsTest {
- private static final String TAG = "NetlinkUtilsTest";
- private static final int TEST_SEQNO = 5;
- private static final int TEST_TIMEOUT_MS = 500;
-
- @Test
- public void testGetNeighborsQuery() throws Exception {
- final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE);
- assertNotNull(fd);
-
- NetlinkUtils.connectSocketToNetlink(fd);
-
- final NetlinkSocketAddress localAddr = (NetlinkSocketAddress) Os.getsockname(fd);
- assertNotNull(localAddr);
- assertEquals(0, localAddr.getGroupsMask());
- assertTrue(0 != localAddr.getPortId());
-
- final byte[] req = RtNetlinkNeighborMessage.newGetNeighborsRequest(TEST_SEQNO);
- assertNotNull(req);
-
- final Context ctx = InstrumentationRegistry.getInstrumentation().getContext();
- final int targetSdk =
- ctx.getPackageManager()
- .getApplicationInfo(ctx.getPackageName(), 0)
- .targetSdkVersion;
-
- // Apps targeting an SDK version > S are not allowed to send RTM_GETNEIGH{TBL} messages
- if (SdkLevel.isAtLeastT() && targetSdk > 31) {
- var ctxt = new String(Files.readAllBytes(Paths.get("/proc/thread-self/attr/current")));
- assumeFalse("must not be platform app", ctxt.startsWith("u:r:platform_app:s0:"));
- try {
- NetlinkUtils.sendMessage(fd, req, 0, req.length, TEST_TIMEOUT_MS);
- fail("RTM_GETNEIGH is not allowed for apps targeting SDK > 31 on T+ platforms,"
- + " target SDK version: " + targetSdk);
- } catch (ErrnoException e) {
- // Expected
- assertEquals(e.errno, EACCES);
- return;
- }
- }
-
- // Check that apps targeting lower API levels / running on older platforms succeed
- assertEquals(req.length,
- NetlinkUtils.sendMessage(fd, req, 0, req.length, TEST_TIMEOUT_MS));
-
- int neighMessageCount = 0;
- int doneMessageCount = 0;
-
- while (doneMessageCount == 0) {
- ByteBuffer response =
- NetlinkUtils.recvMessage(fd, DEFAULT_RECV_BUFSIZE, TEST_TIMEOUT_MS);
- assertNotNull(response);
- assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit());
- assertEquals(0, response.position());
- assertEquals(ByteOrder.nativeOrder(), response.order());
-
- // Verify the messages at least appears minimally reasonable.
- while (response.remaining() > 0) {
- final NetlinkMessage msg = NetlinkMessage.parse(response, NETLINK_ROUTE);
- assertNotNull(msg);
- final StructNlMsgHdr hdr = msg.getHeader();
- assertNotNull(hdr);
-
- if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) {
- doneMessageCount++;
- continue;
- }
-
- assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
- assertTrue(msg instanceof RtNetlinkNeighborMessage);
- assertTrue((hdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0);
- assertEquals(TEST_SEQNO, hdr.nlmsg_seq);
- assertEquals(localAddr.getPortId(), hdr.nlmsg_pid);
-
- neighMessageCount++;
- }
- }
-
- assertEquals(1, doneMessageCount);
- // TODO: make sure this test passes sanely in airplane mode.
- assertTrue(neighMessageCount > 0);
-
- IoUtils.closeQuietly(fd);
- }
-
- @Test
- public void testBasicWorkingGetAddrQuery() throws Exception {
- final FileDescriptor fd = NetlinkUtils.netlinkSocketForProto(NETLINK_ROUTE);
- assertNotNull(fd);
-
- NetlinkUtils.connectSocketToNetlink(fd);
-
- final NetlinkSocketAddress localAddr = (NetlinkSocketAddress) Os.getsockname(fd);
- assertNotNull(localAddr);
- assertEquals(0, localAddr.getGroupsMask());
- assertTrue(0 != localAddr.getPortId());
-
- final int testSeqno = 8;
- final byte[] req = newGetAddrRequest(testSeqno);
- assertNotNull(req);
-
- final long timeout = 500;
- assertEquals(req.length, NetlinkUtils.sendMessage(fd, req, 0, req.length, timeout));
-
- int addrMessageCount = 0;
-
- while (true) {
- ByteBuffer response = NetlinkUtils.recvMessage(fd, DEFAULT_RECV_BUFSIZE, timeout);
- assertNotNull(response);
- assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit());
- assertEquals(0, response.position());
- assertEquals(ByteOrder.nativeOrder(), response.order());
-
- final NetlinkMessage msg = NetlinkMessage.parse(response, NETLINK_ROUTE);
- assertNotNull(msg);
- final StructNlMsgHdr nlmsghdr = msg.getHeader();
- assertNotNull(nlmsghdr);
-
- if (nlmsghdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) {
- break;
- }
-
- assertEquals(NetlinkConstants.RTM_NEWADDR, nlmsghdr.nlmsg_type);
- assertTrue((nlmsghdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0);
- assertEquals(testSeqno, nlmsghdr.nlmsg_seq);
- assertEquals(localAddr.getPortId(), nlmsghdr.nlmsg_pid);
- assertTrue(msg instanceof RtNetlinkAddressMessage);
- addrMessageCount++;
-
- // From the query response we can see the RTM_NEWADDR messages representing for IPv4
- // and IPv6 loopback address: 127.0.0.1 and ::1.
- final StructIfaddrMsg ifaMsg = ((RtNetlinkAddressMessage) msg).getIfaddrHeader();
- final InetAddress ipAddress = ((RtNetlinkAddressMessage) msg).getIpAddress();
- assertTrue(
- "Non-IP address family: " + ifaMsg.family,
- ifaMsg.family == AF_INET || ifaMsg.family == AF_INET6);
- assertTrue(ipAddress.isLoopbackAddress());
- }
-
- assertTrue(addrMessageCount > 0);
-
- IoUtils.closeQuietly(fd);
- }
-
- /** A convenience method to create an RTM_GETADDR request message. */
- private static byte[] newGetAddrRequest(int seqNo) {
- final int length = StructNlMsgHdr.STRUCT_SIZE + Struct.getSize(StructIfaddrMsg.class);
- final byte[] bytes = new byte[length];
- final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
- byteBuffer.order(ByteOrder.nativeOrder());
-
- final StructNlMsgHdr nlmsghdr = new StructNlMsgHdr();
- nlmsghdr.nlmsg_len = length;
- nlmsghdr.nlmsg_type = NetlinkConstants.RTM_GETADDR;
- nlmsghdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
- nlmsghdr.nlmsg_seq = seqNo;
- nlmsghdr.pack(byteBuffer);
-
- final StructIfaddrMsg addrMsg = new StructIfaddrMsg((byte) AF_UNSPEC /* family */,
- (short) 0 /* prefixLen */, (short) 0 /* flags */, (short) 0 /* scope */,
- 0 /* index */);
- addrMsg.pack(byteBuffer);
-
- return bytes;
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkAddressMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkAddressMessageTest.java
deleted file mode 100644
index 01126d2f..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkAddressMessageTest.java
+++ /dev/null
@@ -1,289 +0,0 @@
-/*
- * Copyright (C) 2021 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.netlink;
-
-import static android.system.OsConstants.IFA_F_PERMANENT;
-import static android.system.OsConstants.NETLINK_ROUTE;
-import static android.system.OsConstants.RT_SCOPE_LINK;
-import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
-
-import static com.android.testutils.MiscAsserts.assertThrows;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import android.net.InetAddresses;
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import com.android.net.module.util.HexDump;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet6Address;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class RtNetlinkAddressMessageTest {
- private static final Inet6Address TEST_LINK_LOCAL =
- (Inet6Address) InetAddresses.parseNumericAddress("FE80::2C41:5CFF:FE09:6665");
- private static final Inet6Address TEST_GLOBAL_ADDRESS =
- (Inet6Address) InetAddresses.parseNumericAddress("2001:DB8:1::100");
-
- // An example of the full RTM_NEWADDR message.
- private static final String RTM_NEWADDR_HEX =
- "48000000140000000000000000000000" // struct nlmsghr
- + "0A4080FD1E000000" // struct ifaddrmsg
- + "14000100FE800000000000002C415CFFFE096665" // IFA_ADDRESS
- + "14000600100E0000201C00002A70000045700000" // IFA_CACHEINFO
- + "0800080080000000"; // IFA_FLAGS
-
- private ByteBuffer toByteBuffer(final String hexString) {
- return ByteBuffer.wrap(HexDump.hexStringToByteArray(hexString));
- }
-
- @Test
- public void testParseRtmNewAddress() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWADDR_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkAddressMessage);
- final RtNetlinkAddressMessage addrMsg = (RtNetlinkAddressMessage) msg;
-
- final StructNlMsgHdr hdr = addrMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(72, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_NEWADDR, hdr.nlmsg_type);
- assertEquals(0, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructIfaddrMsg ifaddrMsgHdr = addrMsg.getIfaddrHeader();
- assertNotNull(ifaddrMsgHdr);
- assertEquals((byte) OsConstants.AF_INET6, ifaddrMsgHdr.family);
- assertEquals(64, ifaddrMsgHdr.prefixLen);
- assertEquals(0x80, ifaddrMsgHdr.flags);
- assertEquals(0xFD, ifaddrMsgHdr.scope);
- assertEquals(30, ifaddrMsgHdr.index);
-
- assertEquals((Inet6Address) addrMsg.getIpAddress(), TEST_LINK_LOCAL);
- assertEquals(3600L, addrMsg.getIfacacheInfo().preferred);
- assertEquals(7200L, addrMsg.getIfacacheInfo().valid);
- assertEquals(28714, addrMsg.getIfacacheInfo().cstamp);
- assertEquals(28741, addrMsg.getIfacacheInfo().tstamp);
- assertEquals(0x80, addrMsg.getFlags());
- }
-
- private static final String RTM_NEWADDR_PACK_HEX =
- "48000000140000000000000000000000" // struct nlmsghr
- + "0A4080FD1E000000" // struct ifaddrmsg
- + "14000100FE800000000000002C415CFFFE096665" // IFA_ADDRESS
- + "14000600FFFFFFFFFFFFFFFF2A7000002A700000" // IFA_CACHEINFO
- + "0800080081000000"; // IFA_FLAGS(override ifa_flags)
-
- @Test
- public void testPackRtmNewAddr() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWADDR_PACK_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkAddressMessage);
- final RtNetlinkAddressMessage addrMsg = (RtNetlinkAddressMessage) msg;
-
- final ByteBuffer packBuffer = ByteBuffer.allocate(72);
- packBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- addrMsg.pack(packBuffer);
- assertEquals(RTM_NEWADDR_PACK_HEX, HexDump.toHexString(packBuffer.array()));
- }
-
- private static final String RTM_NEWADDR_TRUNCATED_HEX =
- "44000000140000000000000000000000" // struct nlmsghr
- + "0A4080FD1E000000" // struct ifaddrmsg
- + "10000100FE800000000000002C415CFF" // IFA_ADDRESS(truncated)
- + "14000600FFFFFFFFFFFFFFFF2A7000002A700000" // IFA_CACHEINFO
- + "0800080080000000"; // IFA_FLAGS
-
- @Test
- public void testTruncatedRtmNewAddr() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWADDR_TRUNCATED_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- // Parsing RTM_NEWADDR with truncated IFA_ADDRESS attribute returns null.
- assertNull(msg);
- }
-
- @Test
- public void testCreateRtmNewAddressMessage() {
- // Hexadecimal representation of our created packet.
- final String expectedNewAddressHex =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = 20 (RTM_NEWADDR)
- "0501" + // flags = NLM_F_ACK | NLM_F_REQUEST | NLM_F_REPLACE
- "01000000" + // seqno = 1
- "00000000" + // pid = 0 (send to kernel)
- // struct IfaddrMsg
- "0A" + // family = inet6
- "40" + // prefix len = 64
- "00" + // flags = 0
- "FD" + // scope = RT_SCOPE_LINK
- "17000000" + // ifindex = 23
- // struct nlattr: IFA_ADDRESS
- "1400" + // len
- "0100" + // type
- "FE800000000000002C415CFFFE096665" + // IP address = fe80::2C41:5cff:fe09:6665
- // struct nlattr: IFA_CACHEINFO
- "1400" + // len
- "0600" + // type
- "FFFFFFFF" + // preferred = infinite
- "FFFFFFFF" + // valid = infinite
- "00000000" + // cstamp
- "00000000" + // tstamp
- // struct nlattr: IFA_FLAGS
- "0800" + // len
- "0800" + // type
- "80000000"; // flags = IFA_F_PERMANENT
- final byte[] expectedNewAddress =
- HexEncoding.decode(expectedNewAddressHex.toCharArray(), false);
-
- final byte[] bytes = RtNetlinkAddressMessage.newRtmNewAddressMessage(1 /* seqno */,
- TEST_LINK_LOCAL, (short) 64 /* prefix len */, IFA_F_PERMANENT /* flags */,
- (byte) RT_SCOPE_LINK /* scope */, 23 /* ifindex */,
- (long) 0xFFFFFFFF /* preferred */, (long) 0xFFFFFFFF /* valid */);
- assertArrayEquals(expectedNewAddress, bytes);
- }
-
- @Test
- public void testCreateRtmDelAddressMessage() {
- // Hexadecimal representation of our created packet.
- final String expectedDelAddressHex =
- // struct nlmsghdr
- "2C000000" + // length = 44
- "1500" + // type = 21 (RTM_DELADDR)
- "0500" + // flags = NLM_F_ACK | NLM_F_REQUEST
- "01000000" + // seqno = 1
- "00000000" + // pid = 0 (send to kernel)
- // struct IfaddrMsg
- "0A" + // family = inet6
- "40" + // prefix len = 64
- "00" + // flags = 0
- "00" + // scope = RT_SCOPE_UNIVERSE
- "3B000000" + // ifindex = 59
- // struct nlattr: IFA_ADDRESS
- "1400" + // len
- "0100" + // type
- "20010DB8000100000000000000000100"; // IP address = 2001:db8:1::100
- final byte[] expectedDelAddress =
- HexEncoding.decode(expectedDelAddressHex.toCharArray(), false);
-
- final byte[] bytes = RtNetlinkAddressMessage.newRtmDelAddressMessage(1 /* seqno */,
- TEST_GLOBAL_ADDRESS, (short) 64 /* prefix len */, 59 /* ifindex */);
- assertArrayEquals(expectedDelAddress, bytes);
- }
-
- @Test
- public void testCreateRtmNewAddressMessage_nullIpAddress() {
- assertThrows(NullPointerException.class,
- () -> RtNetlinkAddressMessage.newRtmNewAddressMessage(1 /* seqno */,
- null /* IP address */, (short) 0 /* prefix len */,
- IFA_F_PERMANENT /* flags */, (byte) RT_SCOPE_LINK /* scope */,
- 23 /* ifindex */, (long) 0xFFFFFFFF /* preferred */,
- (long) 0xFFFFFFFF /* valid */));
- }
-
- @Test
- public void testCreateRtmDelAddressMessage_nullIpAddress() {
- assertThrows(NullPointerException.class,
- () -> RtNetlinkAddressMessage.newRtmDelAddressMessage(1 /* seqno */,
- null /* IP address */, (short) 0 /* prefix len */, 59 /* ifindex */));
- }
-
- @Test
- public void testCreateRtmNewAddressMessage_u32Flags() {
- // Hexadecimal representation of our created packet.
- final String expectedNewAddressHex =
- // struct nlmsghdr
- "48000000" + // length = 72
- "1400" + // type = 20 (RTM_NEWADDR)
- "0501" + // flags = NLM_F_ACK | NLM_F_REQUEST | NLM_F_REPLACE
- "01000000" + // seqno = 1
- "00000000" + // pid = 0 (send to kernel)
- // struct IfaddrMsg
- "0A" + // family = inet6
- "80" + // prefix len = 128
- "00" + // flags = 0
- "00" + // scope = RT_SCOPE_UNIVERSE
- "17000000" + // ifindex = 23
- // struct nlattr: IFA_ADDRESS
- "1400" + // len
- "0100" + // type
- "20010DB8000100000000000000000100" + // IP address = 2001:db8:1::100
- // struct nlattr: IFA_CACHEINFO
- "1400" + // len
- "0600" + // type
- "FFFFFFFF" + // preferred = infinite
- "FFFFFFFF" + // valid = infinite
- "00000000" + // cstamp
- "00000000" + // tstamp
- // struct nlattr: IFA_FLAGS
- "0800" + // len
- "0800" + // type
- "00030000"; // flags = IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE
- final byte[] expectedNewAddress =
- HexEncoding.decode(expectedNewAddressHex.toCharArray(), false);
-
- final byte[] bytes = RtNetlinkAddressMessage.newRtmNewAddressMessage(1 /* seqno */,
- TEST_GLOBAL_ADDRESS, (short) 128 /* prefix len */,
- (int) 0x300 /* flags: IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE */,
- (byte) RT_SCOPE_UNIVERSE /* scope */, 23 /* ifindex */,
- (long) 0xFFFFFFFF /* preferred */, (long) 0xFFFFFFFF /* valid */);
- assertArrayEquals(expectedNewAddress, bytes);
- }
-
- @Test
- public void testToString() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWADDR_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkAddressMessage);
- final RtNetlinkAddressMessage addrMsg = (RtNetlinkAddressMessage) msg;
- final String expected = "RtNetlinkAddressMessage{ "
- + "nlmsghdr{"
- + "StructNlMsgHdr{ nlmsg_len{72}, nlmsg_type{20(RTM_NEWADDR)}, nlmsg_flags{0()}, "
- + "nlmsg_seq{0}, nlmsg_pid{0} }}, "
- + "Ifaddrmsg{"
- + "family: 10, prefixLen: 64, flags: 128, scope: 253, index: 30}, "
- + "IP Address{fe80::2c41:5cff:fe09:6665}, "
- + "IfacacheInfo{"
- + "preferred: 3600, valid: 7200, cstamp: 28714, tstamp: 28741}, "
- + "Address Flags{00000080} "
- + "}";
- assertEquals(expected, addrMsg.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkLinkMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkLinkMessageTest.java
deleted file mode 100644
index 9db63db1..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkLinkMessageTest.java
+++ /dev/null
@@ -1,192 +0,0 @@
-/*
- * Copyright (C) 2021 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.netlink;
-
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static com.android.net.module.util.NetworkStackConstants.ETHER_MTU;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import android.net.MacAddress;
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import com.android.net.module.util.HexDump;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class RtNetlinkLinkMessageTest {
-
- // An example of the full RTM_NEWLINK message.
- private static final String RTM_NEWLINK_HEX =
- "64000000100000000000000000000000" // struct nlmsghr
- + "000001001E0000000210000000000000" // struct ifinfo
- + "0A000300776C616E30000000" // IFLA_IFNAME(wlan0)
- + "08000D00B80B0000" // IFLA_PROTINFO
- + "0500100002000000" // IFLA_OPERSTATE
- + "0500110001000000" // IFLA_LINKMODE
- + "08000400DC050000" // IFLA_MTU
- + "0A00010092C3E3C9374E0000" // IFLA_ADDRESS
- + "0A000200FFFFFFFFFFFF0000"; // IFLA_BROADCAST
-
- private ByteBuffer toByteBuffer(final String hexString) {
- return ByteBuffer.wrap(HexDump.hexStringToByteArray(hexString));
- }
-
- @Test
- public void testParseRtmNewLink() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWLINK_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkLinkMessage);
- final RtNetlinkLinkMessage linkMsg = (RtNetlinkLinkMessage) msg;
-
- final StructNlMsgHdr hdr = linkMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(100, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_NEWLINK, hdr.nlmsg_type);
- assertEquals(0, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructIfinfoMsg ifinfomsgHdr = linkMsg.getIfinfoHeader();
- assertNotNull(ifinfomsgHdr);
- assertEquals((byte) OsConstants.AF_UNSPEC, ifinfomsgHdr.family);
- assertEquals(OsConstants.ARPHRD_ETHER, ifinfomsgHdr.type);
- assertEquals(30, ifinfomsgHdr.index);
- assertEquals(0, ifinfomsgHdr.change);
-
- assertEquals(ETHER_MTU, linkMsg.getMtu());
- assertEquals(MacAddress.fromString("92:C3:E3:C9:37:4E"), linkMsg.getHardwareAddress());
- assertTrue(linkMsg.getInterfaceName().equals("wlan0"));
- }
-
- /**
- * Example:
- * # adb shell ip tunnel add トン0 mode sit local any remote 8.8.8.8
- * # adb shell ip link show | grep トン
- * 33: トン0@NONE: <POINTOPOINT,NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group
- * default qlen 1000
- *
- * IFLA_IFNAME attribute: \x0c\x00\x03\x00\xe3\x83\x88\xe3\x83\xb3\x30\x00
- * length: 0x000c
- * type: 0x0003
- * value: \xe3\x83\x88\xe3\x83\xb3\x30\x00
- * ト (\xe3\x83\x88)
- * ン (\xe3\x83\xb3)
- * 0 (\x30)
- * null terminated (\x00)
- */
- private static final String RTM_NEWLINK_UTF8_HEX =
- "34000000100000000000000000000000" // struct nlmsghr
- + "000001001E0000000210000000000000" // struct ifinfo
- + "08000400DC050000" // IFLA_MTU
- + "0A00010092C3E3C9374E0000" // IFLA_ADDRESS
- + "0C000300E38388E383B33000"; // IFLA_IFNAME(トン0)
-
- @Test
- public void testParseRtmNewLink_utf8Ifname() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWLINK_UTF8_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkLinkMessage);
- final RtNetlinkLinkMessage linkMsg = (RtNetlinkLinkMessage) msg;
-
- assertTrue(linkMsg.getInterfaceName().equals("トン0"));
- }
-
- private static final String RTM_NEWLINK_PACK_HEX =
- "34000000100000000000000000000000" // struct nlmsghr
- + "000001001E0000000210000000000000" // struct ifinfo
- + "08000400DC050000" // IFLA_MTU
- + "0A00010092C3E3C9374E0000" // IFLA_ADDRESS
- + "0A000300776C616E30000000"; // IFLA_IFNAME(wlan0)
-
- @Test
- public void testPackRtmNewLink() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWLINK_PACK_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkLinkMessage);
- final RtNetlinkLinkMessage linkMsg = (RtNetlinkLinkMessage) msg;
-
- final ByteBuffer packBuffer = ByteBuffer.allocate(64);
- packBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- linkMsg.pack(packBuffer);
- assertEquals(RTM_NEWLINK_PACK_HEX, HexDump.toHexString(packBuffer.array()));
- }
-
- private static final String RTM_NEWLINK_TRUNCATED_HEX =
- "54000000100000000000000000000000" // struct nlmsghr
- + "000001001E0000000210000000000000" // struct ifinfo
- + "08000D00B80B0000" // IFLA_PROTINFO
- + "0500100002000000" // IFLA_OPERSTATE
- + "0800010092C3E3C9" // IFLA_ADDRESS(truncated)
- + "0500110001000000" // IFLA_LINKMODE
- + "0A000300776C616E30000000" // IFLA_IFNAME(wlan0)
- + "08000400DC050000"; // IFLA_MTU
-
- @Test
- public void testTruncatedRtmNewLink() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWLINK_TRUNCATED_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkLinkMessage);
- final RtNetlinkLinkMessage linkMsg = (RtNetlinkLinkMessage) msg;
-
- // Truncated IFLA_ADDRESS attribute doesn't affect parsing other attrs.
- assertNull(linkMsg.getHardwareAddress());
- assertEquals(ETHER_MTU, linkMsg.getMtu());
- assertTrue(linkMsg.getInterfaceName().equals("wlan0"));
- }
-
- @Test
- public void testToString() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWLINK_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkLinkMessage);
- final RtNetlinkLinkMessage linkMsg = (RtNetlinkLinkMessage) msg;
- final String expected = "RtNetlinkLinkMessage{ "
- + "nlmsghdr{"
- + "StructNlMsgHdr{ nlmsg_len{100}, nlmsg_type{16(RTM_NEWLINK)}, nlmsg_flags{0()}, "
- + "nlmsg_seq{0}, nlmsg_pid{0} }}, "
- + "Ifinfomsg{"
- + "family: 0, type: 1, index: 30, flags: 4098, change: 0}, "
- + "Hardware Address{92:c3:e3:c9:37:4e}, " + "MTU{1500}, "
- + "Ifname{wlan0} "
- + "}";
- assertEquals(expected, linkMsg.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkNeighborMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkNeighborMessageTest.java
deleted file mode 100644
index 4d8900cd..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkNeighborMessageTest.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright (C) 2019 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.netlink;
-
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static com.android.net.module.util.netlink.StructNdMsg.NUD_STALE;
-import static com.android.testutils.NetlinkTestUtils.makeDelNeighMessage;
-import static com.android.testutils.NetlinkTestUtils.makeNewNeighMessage;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-import android.net.InetAddresses;
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet4Address;
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.util.Arrays;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class RtNetlinkNeighborMessageTest {
- private static final String TAG = "RtNetlinkNeighborMessageTest";
-
- public static final byte[] RTM_DELNEIGH = makeDelNeighMessage(
- InetAddresses.parseNumericAddress("192.168.159.254"), NUD_STALE);
-
- public static final byte[] RTM_NEWNEIGH = makeNewNeighMessage(
- InetAddresses.parseNumericAddress("fe80::86c9:b2ff:fe6a:ed4b"), NUD_STALE);
-
- // An example of the full response from an RTM_GETNEIGH query.
- private static final String RTM_GETNEIGH_RESPONSE_HEX =
- // <-- struct nlmsghr -->|<-- struct ndmsg -->|<-- struct nlattr: NDA_DST -->|<-- NDA_LLADDR -->|<-- NDA_PROBES -->|<-- NDA_CACHEINFO -->|
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000001 0a00 0200 333300000001 0000 0800 0400 00000000 1400 0300 a2280000 32110000 32110000 01000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff000001 0a00 0200 3333ff000001 0000 0800 0400 00000000 1400 0300 0d280000 9d100000 9d100000 00000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 0400 80 01 1400 0100 20010db800040ca00000000000000001 0a00 0200 84c9b26aed4b 0000 0800 0400 04000000 1400 0300 90100000 90100000 90080000 01000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff47da19 0a00 0200 3333ff47da19 0000 0800 0400 00000000 1400 0300 a1280000 31110000 31110000 01000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff020000000000000000000000000016 0a00 0200 333300000016 0000 0800 0400 00000000 1400 0300 912a0000 21130000 21130000 00000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff0200000000000000000001ffeace3b 0a00 0200 3333ffeace3b 0000 0800 0400 00000000 1400 0300 922a0000 22130000 22130000 00000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ff5c2a83 0a00 0200 3333ff5c2a83 0000 0800 0400 00000000 1400 0300 391c0000 c9040000 c9040000 01000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 01000000 4000 00 02 1400 0100 00000000000000000000000000000000 0a00 0200 000000000000 0000 0800 0400 00000000 1400 0300 cd180200 5d010200 5d010200 08000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000002 0a00 0200 333300000002 0000 0800 0400 00000000 1400 0300 352a0000 c5120000 c5120000 00000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff020000000000000000000000000016 0a00 0200 333300000016 0000 0800 0400 00000000 1400 0300 982a0000 28130000 28130000 00000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 0800 80 01 1400 0100 fe8000000000000086c9b2fffe6aed4b 0a00 0200 84c9b26aed4b 0000 0800 0400 00000000 1400 0300 23000000 24000000 57000000 13000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 15000000 4000 00 05 1400 0100 ff0200000000000000000001ffeace3b 0a00 0200 3333ffeace3b 0000 0800 0400 00000000 1400 0300 992a0000 29130000 29130000 01000000" +
- "58000000 1c00 0200 00000000 3e2b0000 0a 00 0000 14000000 4000 00 05 1400 0100 ff020000000000000000000000000002 0a00 0200 333300000002 0000 0800 0400 00000000 1400 0300 2e2a0000 be120000 be120000 00000000" +
- "44000000 1c00 0200 00000000 3e2b0000 02 00 0000 18000000 4000 00 03 0800 0100 00000000 0400 0200 0800 0400 00000000 1400 0300 75280000 05110000 05110000 22000000";
- public static final byte[] RTM_GETNEIGH_RESPONSE =
- HexEncoding.decode(RTM_GETNEIGH_RESPONSE_HEX.replaceAll(" ", "").toCharArray(), false);
-
- @Test
- public void testParseRtmDelNeigh() {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_DELNEIGH);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkNeighborMessage);
- final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
-
- final StructNlMsgHdr hdr = neighMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(76, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_DELNEIGH, hdr.nlmsg_type);
- assertEquals(0, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
- assertNotNull(ndmsgHdr);
- assertEquals((byte) OsConstants.AF_INET, ndmsgHdr.ndm_family);
- assertEquals(21, ndmsgHdr.ndm_ifindex);
- assertEquals(NUD_STALE, ndmsgHdr.ndm_state);
- final InetAddress destination = neighMsg.getDestination();
- assertNotNull(destination);
- assertEquals(InetAddress.parseNumericAddress("192.168.159.254"), destination);
- }
-
- @Test
- public void testParseRtmNewNeigh() {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_NEWNEIGH);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkNeighborMessage);
- final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
-
- final StructNlMsgHdr hdr = neighMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(88, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
- assertEquals(0, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructNdMsg ndmsgHdr = neighMsg.getNdHeader();
- assertNotNull(ndmsgHdr);
- assertEquals((byte) OsConstants.AF_INET6, ndmsgHdr.ndm_family);
- assertEquals(21, ndmsgHdr.ndm_ifindex);
- assertEquals(NUD_STALE, ndmsgHdr.ndm_state);
- final InetAddress destination = neighMsg.getDestination();
- assertNotNull(destination);
- assertEquals(InetAddress.parseNumericAddress("fe80::86c9:b2ff:fe6a:ed4b"), destination);
- }
-
- @Test
- public void testParseRtmGetNeighResponse() {
- final ByteBuffer byteBuffer = ByteBuffer.wrap(RTM_GETNEIGH_RESPONSE);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
-
- int messageCount = 0;
- while (byteBuffer.remaining() > 0) {
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkNeighborMessage);
- final RtNetlinkNeighborMessage neighMsg = (RtNetlinkNeighborMessage) msg;
-
- final StructNlMsgHdr hdr = neighMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type);
- assertEquals(StructNlMsgHdr.NLM_F_MULTI, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(11070, hdr.nlmsg_pid);
-
- final int probes = neighMsg.getProbes();
- assertTrue("Unexpected number of probes. Got " + probes + ", max=5",
- probes < 5);
- final int ndm_refcnt = neighMsg.getCacheInfo().ndm_refcnt;
- assertTrue("nda_cacheinfo has unexpectedly high ndm_refcnt: " + ndm_refcnt,
- ndm_refcnt < 0x100);
-
- messageCount++;
- }
- // TODO: add more detailed spot checks.
- assertEquals(14, messageCount);
- }
-
- @Test
- public void testCreateRtmNewNeighMessage() {
- final int seqNo = 2635;
- final int ifIndex = 14;
- final byte[] llAddr =
- new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6 };
-
- // Hexadecimal representation of our created packet.
- final String expectedNewNeighHex =
- // struct nlmsghdr
- "30000000" + // length = 48
- "1c00" + // type = 28 (RTM_NEWNEIGH)
- "0501" + // flags (NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE)
- "4b0a0000" + // seqno
- "00000000" + // pid (0 == kernel)
- // struct ndmsg
- "02" + // family
- "00" + // pad1
- "0000" + // pad2
- "0e000000" + // interface index (14)
- "0800" + // NUD state (0x08 == NUD_DELAY)
- "00" + // flags
- "00" + // type
- // struct nlattr: NDA_DST
- "0800" + // length = 8
- "0100" + // type (1 == NDA_DST, for neighbor messages)
- "7f000001" + // IPv4 address (== 127.0.0.1)
- // struct nlattr: NDA_LLADDR
- "0a00" + // length = 10
- "0200" + // type (2 == NDA_LLADDR, for neighbor messages)
- "010203040506" + // MAC Address (== 01:02:03:04:05:06)
- "0000"; // padding, for 4 byte alignment
- final byte[] expectedNewNeigh =
- HexEncoding.decode(expectedNewNeighHex.toCharArray(), false);
-
- final byte[] bytes = RtNetlinkNeighborMessage.newNewNeighborMessage(
- seqNo, Inet4Address.LOOPBACK, StructNdMsg.NUD_DELAY, ifIndex, llAddr);
- if (!Arrays.equals(expectedNewNeigh, bytes)) {
- assertEquals(expectedNewNeigh.length, bytes.length);
- for (int i = 0; i < Math.min(expectedNewNeigh.length, bytes.length); i++) {
- assertEquals(expectedNewNeigh[i], bytes[i]);
- }
- }
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java
deleted file mode 100644
index 9881653a..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/RtNetlinkRouteMessageTest.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/*
- * Copyright (C) 2021 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.netlink;
-
-import static android.system.OsConstants.NETLINK_ROUTE;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-
-import android.net.InetAddresses;
-import android.net.IpPrefix;
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import com.android.net.module.util.HexDump;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet6Address;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class RtNetlinkRouteMessageTest {
- private static final IpPrefix TEST_IPV6_GLOBAL_PREFIX = new IpPrefix("2001:db8:1::/64");
- private static final Inet6Address TEST_IPV6_LINK_LOCAL_GATEWAY =
- (Inet6Address) InetAddresses.parseNumericAddress("fe80::1");
-
- // An example of the full RTM_NEWROUTE message.
- private static final String RTM_NEWROUTE_HEX =
- "88000000180000060000000000000000" // struct nlmsghr
- + "0A400000FC02000100000000" // struct rtmsg
- + "08000F00C7060000" // RTA_TABLE
- + "1400010020010DB8000100000000000000000000" // RTA_DST
- + "08000400DF020000" // RTA_OIF
- + "0800060000010000" // RTA_PRIORITY
- + "24000C0000000000000000005EEA000000000000" // RTA_CACHEINFO
- + "00000000000000000000000000000000"
- + "14000500FE800000000000000000000000000001" // RTA_GATEWAY
- + "0500140000000000"; // RTA_PREF
-
- private ByteBuffer toByteBuffer(final String hexString) {
- return ByteBuffer.wrap(HexDump.hexStringToByteArray(hexString));
- }
-
- private void assertRtmRouteMessage(final RtNetlinkRouteMessage routeMsg) {
- final StructNlMsgHdr hdr = routeMsg.getHeader();
- assertNotNull(hdr);
- assertEquals(136, hdr.nlmsg_len);
- assertEquals(NetlinkConstants.RTM_NEWROUTE, hdr.nlmsg_type);
- assertEquals(0x600, hdr.nlmsg_flags);
- assertEquals(0, hdr.nlmsg_seq);
- assertEquals(0, hdr.nlmsg_pid);
-
- final StructRtMsg rtmsg = routeMsg.getRtMsgHeader();
- assertNotNull(rtmsg);
- assertEquals((byte) OsConstants.AF_INET6, rtmsg.family);
- assertEquals(64, rtmsg.dstLen);
- assertEquals(0, rtmsg.srcLen);
- assertEquals(0, rtmsg.tos);
- assertEquals(0xFC, rtmsg.table);
- assertEquals(NetlinkConstants.RTPROT_KERNEL, rtmsg.protocol);
- assertEquals(NetlinkConstants.RT_SCOPE_UNIVERSE, rtmsg.scope);
- assertEquals(NetlinkConstants.RTN_UNICAST, rtmsg.type);
- assertEquals(0, rtmsg.flags);
-
- assertEquals(routeMsg.getDestination(), TEST_IPV6_GLOBAL_PREFIX);
- assertEquals(735, routeMsg.getInterfaceIndex());
- assertEquals((Inet6Address) routeMsg.getGateway(), TEST_IPV6_LINK_LOCAL_GATEWAY);
-
- assertNotNull(routeMsg.getRtaCacheInfo());
- }
-
- @Test
- public void testParseRtmRouteMessage() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
-
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkRouteMessage);
- final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
- assertRtmRouteMessage(routeMsg);
- }
-
- private static final String RTM_NEWROUTE_PACK_HEX =
- "4C000000180000060000000000000000" // struct nlmsghr
- + "0A400000FC02000100000000" // struct rtmsg
- + "1400010020010DB8000100000000000000000000" // RTA_DST
- + "14000500FE800000000000000000000000000001" // RTA_GATEWAY
- + "08000400DF020000" // RTA_OIF
- + "24000C0000000000000000005EEA000000000000" // RTA_CACHEINFO
- + "00000000000000000000000000000000";
-
- @Test
- public void testPackRtmNewRoute() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_PACK_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkRouteMessage);
- final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
-
- final ByteBuffer packBuffer = ByteBuffer.allocate(112);
- packBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- routeMsg.pack(packBuffer);
- assertEquals(RTM_NEWROUTE_PACK_HEX, HexDump.toHexString(packBuffer.array()));
- }
-
- private static final String RTM_NEWROUTE_TRUNCATED_HEX =
- "48000000180000060000000000000000" // struct nlmsghr
- + "0A400000FC02000100000000" // struct rtmsg
- + "1400010020010DB8000100000000000000000000" // RTA_DST
- + "10000500FE8000000000000000000000" // RTA_GATEWAY(truncated)
- + "08000400DF020000"; // RTA_OIF
-
- @Test
- public void testTruncatedRtmNewRoute() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_TRUNCATED_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- // Parsing RTM_NEWROUTE with truncated RTA_GATEWAY attribute returns null.
- assertNull(msg);
- }
-
- private static final String RTM_NEWROUTE_IPV4_MAPPED_IPV6_GATEWAY_HEX =
- "4C000000180000060000000000000000" // struct nlmsghr
- + "0A400000FC02000100000000" // struct rtmsg
- + "1400010020010DB8000100000000000000000000" // RTA_DST(2001:db8:1::/64)
- + "1400050000000000000000000000FFFF0A010203" // RTA_GATEWAY(::ffff:10.1.2.3)
- + "08000400DF020000"; // RTA_OIF
-
- @Test
- public void testParseRtmRouteMessage_IPv4MappedIPv6Gateway() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_IPV4_MAPPED_IPV6_GATEWAY_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- // Parsing RTM_NEWROUTE with IPv4-mapped IPv6 gateway address, which doesn't match
- // rtm_family after address parsing.
- assertNull(msg);
- }
-
- private static final String RTM_NEWROUTE_IPV4_MAPPED_IPV6_DST_HEX =
- "4C000000180000060000000000000000" // struct nlmsghr
- + "0A780000FC02000100000000" // struct rtmsg
- + "1400010000000000000000000000FFFF0A000000" // RTA_DST(::ffff:10.0.0.0/120)
- + "14000500FE800000000000000000000000000001" // RTA_GATEWAY(fe80::1)
- + "08000400DF020000"; // RTA_OIF
-
- @Test
- public void testParseRtmRouteMessage_IPv4MappedIPv6Destination() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_IPV4_MAPPED_IPV6_DST_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- // Parsing RTM_NEWROUTE with IPv4-mapped IPv6 destination prefix, which doesn't match
- // rtm_family after address parsing.
- assertNull(msg);
- }
-
- // An example of the full RTM_NEWADDR message.
- private static final String RTM_NEWADDR_HEX =
- "48000000140000000000000000000000" // struct nlmsghr
- + "0A4080FD1E000000" // struct ifaddrmsg
- + "14000100FE800000000000002C415CFFFE096665" // IFA_ADDRESS
- + "14000600100E0000201C00002A70000045700000" // IFA_CACHEINFO
- + "0800080080000000"; // IFA_FLAGS
-
- @Test
- public void testParseMultipleRtmMessagesInOneByteBuffer() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX + RTM_NEWADDR_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
-
- // Try to parse the RTM_NEWROUTE message.
- NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkRouteMessage);
- final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
- assertRtmRouteMessage(routeMsg);
-
- // Try to parse the RTM_NEWADDR message.
- msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkAddressMessage);
- }
-
- @Test
- public void testToString() {
- final ByteBuffer byteBuffer = toByteBuffer(RTM_NEWROUTE_HEX);
- byteBuffer.order(ByteOrder.LITTLE_ENDIAN); // For testing.
- final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, NETLINK_ROUTE);
- assertNotNull(msg);
- assertTrue(msg instanceof RtNetlinkRouteMessage);
- final RtNetlinkRouteMessage routeMsg = (RtNetlinkRouteMessage) msg;
- final String expected = "RtNetlinkRouteMessage{ "
- + "nlmsghdr{"
- + "StructNlMsgHdr{ nlmsg_len{136}, nlmsg_type{24(RTM_NEWROUTE)}, "
- + "nlmsg_flags{1536(NLM_F_MATCH)}, nlmsg_seq{0}, nlmsg_pid{0} }}, "
- + "Rtmsg{"
- + "family: 10, dstLen: 64, srcLen: 0, tos: 0, table: 252, protocol: 2, "
- + "scope: 0, type: 1, flags: 0}, "
- + "destination{2001:db8:1::}, "
- + "gateway{fe80::1}, "
- + "ifindex{735}, "
- + "rta_cacheinfo{clntref: 0, lastuse: 0, expires: 59998, error: 0, used: 0, "
- + "id: 0, ts: 0, tsage: 0} "
- + "}";
- assertEquals(expected, routeMsg.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/StructInetDiagSockIdTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/StructInetDiagSockIdTest.java
deleted file mode 100644
index ce190f2c..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/StructInetDiagSockIdTest.java
+++ /dev/null
@@ -1,225 +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.
- */
-
-package com.android.net.module.util.netlink;
-
-import static android.system.OsConstants.AF_INET;
-import static android.system.OsConstants.AF_INET6;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import android.net.InetAddresses;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet4Address;
-import java.net.Inet6Address;
-import java.net.InetSocketAddress;
-import java.nio.ByteBuffer;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class StructInetDiagSockIdTest {
- private static final Inet4Address IPV4_SRC_ADDR =
- (Inet4Address) InetAddresses.parseNumericAddress("192.0.2.1");
- private static final Inet4Address IPV4_DST_ADDR =
- (Inet4Address) InetAddresses.parseNumericAddress("198.51.100.1");
- private static final Inet6Address IPV6_SRC_ADDR =
- (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::1");
- private static final Inet6Address IPV6_DST_ADDR =
- (Inet6Address) InetAddresses.parseNumericAddress("2001:db8::2");
- private static final int SRC_PORT = 65297;
- private static final int DST_PORT = 443;
- private static final int IF_INDEX = 7;
- private static final long COOKIE = 561;
-
- private static final byte[] INET_DIAG_SOCKET_ID_IPV4 =
- new byte[] {
- // src port, dst port
- (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb,
- // src address
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // dst address
- (byte) 0xc6, (byte) 0x33, (byte) 0x64, (byte) 0x01,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // if index
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // cookie
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff
- };
-
- private static final byte[] INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE =
- new byte[] {
- // src port, dst port
- (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb,
- // src address
- (byte) 0xc0, (byte) 0x00, (byte) 0x02, (byte) 0x01,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // dst address
- (byte) 0xc6, (byte) 0x33, (byte) 0x64, (byte) 0x01,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // if index
- (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // cookie
- (byte) 0x31, (byte) 0x02, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- };
-
- private static final byte[] INET_DIAG_SOCKET_ID_IPV6 =
- new byte[] {
- // src port, dst port
- (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb,
- // src address
- (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
- // dst address
- (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02,
- // if index
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // cookie
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff
- };
-
- private static final byte[] INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE =
- new byte[] {
- // src port, dst port
- (byte) 0xff, (byte) 0x11, (byte) 0x01, (byte) 0xbb,
- // src address
- (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
- // dst address
- (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02,
- // if index
- (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- // cookie
- (byte) 0x31, (byte) 0x02, (byte) 0x00, (byte) 0x00,
- (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
- };
-
- @Test
- public void testPackStructInetDiagSockIdWithIpv4() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr);
- final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE);
- sockId.pack(buffer);
- assertArrayEquals(INET_DIAG_SOCKET_ID_IPV4, buffer.array());
- }
-
- @Test
- public void testPackStructInetDiagSockIdWithIpv6() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr);
- final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE);
- sockId.pack(buffer);
- assertArrayEquals(INET_DIAG_SOCKET_ID_IPV6, buffer.array());
- }
-
- @Test
- public void testPackStructInetDiagSockIdWithIpv4IfIndexCookie() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId =
- new StructInetDiagSockId(srcAddr, dstAddr, IF_INDEX, COOKIE);
- final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE);
- sockId.pack(buffer);
- assertArrayEquals(INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE, buffer.array());
- }
-
- @Test
- public void testPackStructInetDiagSockIdWithIpv6IfIndexCookie() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId =
- new StructInetDiagSockId(srcAddr, dstAddr, IF_INDEX, COOKIE);
- final ByteBuffer buffer = ByteBuffer.allocate(StructInetDiagSockId.STRUCT_SIZE);
- sockId.pack(buffer);
- assertArrayEquals(INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE, buffer.array());
- }
-
- @Test
- public void testParseStructInetDiagSockIdWithIpv4() {
- final ByteBuffer buffer = ByteBuffer.wrap(INET_DIAG_SOCKET_ID_IPV4_IF_COOKIE);
- final StructInetDiagSockId sockId = StructInetDiagSockId.parse(buffer, (byte) AF_INET);
-
- assertEquals(SRC_PORT, sockId.locSocketAddress.getPort());
- assertEquals(IPV4_SRC_ADDR, sockId.locSocketAddress.getAddress());
- assertEquals(DST_PORT, sockId.remSocketAddress.getPort());
- assertEquals(IPV4_DST_ADDR, sockId.remSocketAddress.getAddress());
- assertEquals(IF_INDEX, sockId.ifIndex);
- assertEquals(COOKIE, sockId.cookie);
- }
-
- @Test
- public void testParseStructInetDiagSockIdWithIpv6() {
- final ByteBuffer buffer = ByteBuffer.wrap(INET_DIAG_SOCKET_ID_IPV6_IF_COOKIE);
- final StructInetDiagSockId sockId = StructInetDiagSockId.parse(buffer, (byte) AF_INET6);
-
- assertEquals(SRC_PORT, sockId.locSocketAddress.getPort());
- assertEquals(IPV6_SRC_ADDR, sockId.locSocketAddress.getAddress());
- assertEquals(DST_PORT, sockId.remSocketAddress.getPort());
- assertEquals(IPV6_DST_ADDR, sockId.remSocketAddress.getAddress());
- assertEquals(IF_INDEX, sockId.ifIndex);
- assertEquals(COOKIE, sockId.cookie);
- }
-
- @Test
- public void testToStringStructInetDiagSockIdWithIpv4() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV4_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV4_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr);
- assertEquals("StructInetDiagSockId{ idiag_sport{65297}, idiag_dport{443},"
- + " idiag_src{192.0.2.1}, idiag_dst{198.51.100.1}, idiag_if{0},"
- + " idiag_cookie{INET_DIAG_NOCOOKIE}}", sockId.toString());
- }
-
- @Test
- public void testToStringStructInetDiagSockIdWithIpv6() {
- final InetSocketAddress srcAddr = new InetSocketAddress(IPV6_SRC_ADDR, SRC_PORT);
- final InetSocketAddress dstAddr = new InetSocketAddress(IPV6_DST_ADDR, DST_PORT);
- final StructInetDiagSockId sockId = new StructInetDiagSockId(srcAddr, dstAddr);
- assertEquals("StructInetDiagSockId{ idiag_sport{65297}, idiag_dport{443},"
- + " idiag_src{2001:db8::1}, idiag_dst{2001:db8::2}, idiag_if{0},"
- + " idiag_cookie{INET_DIAG_NOCOOKIE}}", sockId.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptPref64Test.java b/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptPref64Test.java
deleted file mode 100644
index beed8383..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptPref64Test.java
+++ /dev/null
@@ -1,202 +0,0 @@
-/*
- * Copyright (C) 2019 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.netlink;
-
-import static com.android.net.module.util.netlink.StructNdOptPref64.getScaledLifetimePlc;
-import static com.android.net.module.util.netlink.StructNdOptPref64.plcToPrefixLength;
-import static com.android.net.module.util.netlink.StructNdOptPref64.prefixLengthToPlc;
-import static com.android.testutils.MiscAsserts.assertThrows;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import android.annotation.SuppressLint;
-import android.net.IpPrefix;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class StructNdOptPref64Test {
-
- private static final String PREFIX1 = "64:ff9b::";
- private static final String PREFIX2 = "2001:db8:1:2:3:64::";
-
- private static byte[] prefixBytes(String addrString) throws Exception {
- InetAddress addr = InetAddress.getByName(addrString);
- byte[] prefixBytes = new byte[12];
- System.arraycopy(addr.getAddress(), 0, prefixBytes, 0, 12);
- return prefixBytes;
- }
-
- @SuppressLint("NewApi")
- private static IpPrefix prefix(String addrString, int prefixLength) throws Exception {
- return new IpPrefix(InetAddress.getByName(addrString), prefixLength);
- }
-
- private void assertPref64OptMatches(int lifetime, IpPrefix prefix, StructNdOptPref64 opt) {
- assertEquals(StructNdOptPref64.TYPE, opt.type);
- assertEquals(2, opt.length);
- assertEquals(lifetime, opt.lifetime);
- assertEquals(prefix, opt.prefix);
- }
-
- private void assertToByteBufferMatches(StructNdOptPref64 opt, String expected) {
- String actual = HexEncoding.encodeToString(opt.toByteBuffer().array());
- assertEquals(expected, actual);
- }
-
- private ByteBuffer makeNdOptPref64(int lifetime, byte[] prefix, int prefixLengthCode) {
- if (prefix.length != 12) throw new IllegalArgumentException("Prefix must be 12 bytes");
-
- ByteBuffer buf = ByteBuffer.allocate(16)
- .put((byte) StructNdOptPref64.TYPE)
- .put((byte) StructNdOptPref64.LENGTH)
- .putShort(getScaledLifetimePlc(lifetime, prefixLengthCode))
- .put(prefix, 0, 12);
-
- buf.flip();
- return buf;
- }
-
- @Test
- public void testParseCannedOption() throws Exception {
- String hexBytes = "2602" // type=38, len=2 (16 bytes)
- + "0088" // lifetime=136, PLC=0 (/96)
- + "20010DB80003000400050006"; // 2001:db8:3:4:5:6/96
- byte[] rawBytes = HexEncoding.decode(hexBytes);
- StructNdOptPref64 opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
- assertPref64OptMatches(136, prefix("2001:DB8:3:4:5:6::", 96), opt);
- assertToByteBufferMatches(opt, hexBytes);
-
- hexBytes = "2602" // type=38, len=2 (16 bytes)
- + "2752" // lifetime=10064, PLC=2 (/56)
- + "0064FF9B0000000000000000"; // 64:ff9b::/56
- rawBytes = HexEncoding.decode(hexBytes);
- opt = StructNdOptPref64.parse(ByteBuffer.wrap(rawBytes));
- assertPref64OptMatches(10064, prefix("64:FF9B::", 56), opt);
- assertToByteBufferMatches(opt, hexBytes);
- }
-
- @Test
- public void testParsing() throws Exception {
- // Valid.
- ByteBuffer buf = makeNdOptPref64(600, prefixBytes(PREFIX1), 0);
- StructNdOptPref64 opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(600, prefix(PREFIX1, 96), opt);
-
- // Valid, zero lifetime, /64.
- buf = makeNdOptPref64(0, prefixBytes(PREFIX1), 1);
- opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(0, prefix(PREFIX1, 64), opt);
-
- // Valid, low lifetime, /56.
- buf = makeNdOptPref64(8, prefixBytes(PREFIX2), 2);
- opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(8, prefix(PREFIX2, 56), opt);
- assertEquals(new IpPrefix("2001:db8:1::/56"), opt.prefix); // Prefix is truncated.
-
- // Valid, maximum lifetime, /32.
- buf = makeNdOptPref64(65528, prefixBytes(PREFIX2), 5);
- opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(65528, prefix(PREFIX2, 32), opt);
- assertEquals(new IpPrefix("2001:db8::/32"), opt.prefix); // Prefix is truncated.
-
- // Lifetime not divisible by 8.
- buf = makeNdOptPref64(300, prefixBytes(PREFIX2), 0);
- opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(296, prefix(PREFIX2, 96), opt);
-
- // Invalid prefix length codes.
- buf = makeNdOptPref64(600, prefixBytes(PREFIX1), 6);
- assertNull(StructNdOptPref64.parse(buf));
- buf = makeNdOptPref64(600, prefixBytes(PREFIX1), 7);
- assertNull(StructNdOptPref64.parse(buf));
-
- // Truncated to varying lengths...
- buf = makeNdOptPref64(600, prefixBytes(PREFIX1), 3);
- final int len = buf.limit();
- for (int i = 0; i < buf.limit() - 1; i++) {
- buf.flip();
- buf.limit(i);
- assertNull("Option truncated to " + i + " bytes, should have returned null",
- StructNdOptPref64.parse(buf));
- }
- buf.flip();
- buf.limit(len);
- // ... but otherwise OK.
- opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(600, prefix(PREFIX1, 48), opt);
- }
-
- @Test
- public void testToString() throws Exception {
- ByteBuffer buf = makeNdOptPref64(600, prefixBytes(PREFIX1), 4);
- StructNdOptPref64 opt = StructNdOptPref64.parse(buf);
- assertPref64OptMatches(600, prefix(PREFIX1, 40), opt);
- assertEquals("NdOptPref64(64:ff9b::/40, 600)", opt.toString());
- }
-
- private void assertInvalidPlc(int plc) {
- assertThrows(IllegalArgumentException.class, () -> plcToPrefixLength(plc));
- }
-
- @Test
- public void testPrefixLengthToPlc() {
- for (int i = 0; i < 6; i++) {
- assertEquals(i, prefixLengthToPlc(plcToPrefixLength(i)));
- }
- assertInvalidPlc(-1);
- assertInvalidPlc(6);
- assertInvalidPlc(7);
- assertEquals(0, prefixLengthToPlc(96));
- }
-
-
- private void assertInvalidParameters(IpPrefix prefix, int lifetime) {
- assertThrows(IllegalArgumentException.class, () -> new StructNdOptPref64(prefix, lifetime));
- }
-
- @Test
- public void testToByteBuffer() throws Exception {
- final IpPrefix prefix1 = prefix(PREFIX1, 56);
- final IpPrefix prefix2 = prefix(PREFIX2, 96);
-
- StructNdOptPref64 opt = new StructNdOptPref64(prefix1, 600);
- assertToByteBufferMatches(opt, "2602025A0064FF9B0000000000000000");
- assertEquals(new IpPrefix("64:ff9b::/56"), opt.prefix);
- assertEquals(600, opt.lifetime);
-
- opt = new StructNdOptPref64(prefix2, 65519);
- assertToByteBufferMatches(opt, "2602FFE820010DB80001000200030064");
- assertEquals(new IpPrefix("2001:db8:1:2:3:64::/96"), opt.prefix);
- assertEquals(65512, opt.lifetime);
-
- assertInvalidParameters(prefix1, 65535);
- assertInvalidParameters(prefix2, -1);
- assertInvalidParameters(prefix("1.2.3.4", 32), 600);
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptRdnssTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptRdnssTest.java
deleted file mode 100644
index 1dcb9b59..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/StructNdOptRdnssTest.java
+++ /dev/null
@@ -1,195 +0,0 @@
-/*
- * Copyright (C) 2021 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.netlink;
-
-import static com.android.net.module.util.NetworkStackConstants.ICMPV6_ND_OPTION_RDNSS;
-import static com.android.testutils.MiscAsserts.assertThrows;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import android.net.InetAddresses;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import com.android.net.module.util.structs.RdnssOption;
-
-import libcore.util.HexEncoding;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import java.net.Inet6Address;
-import java.nio.ByteBuffer;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class StructNdOptRdnssTest {
- private static final String DNS_SERVER1 = "2001:4860:4860::64";
- private static final String DNS_SERVER2 = "2001:4860:4860::6464";
-
- private static final Inet6Address[] DNS_SERVER_ADDRESSES = new Inet6Address[] {
- (Inet6Address) InetAddresses.parseNumericAddress(DNS_SERVER1),
- (Inet6Address) InetAddresses.parseNumericAddress(DNS_SERVER2),
- };
-
- private static final String RDNSS_OPTION_BYTES =
- "1905" // type=25, len=5 (40 bytes)
- + "0000" // reserved
- + "00000E10" // lifetime=3600
- + "20014860486000000000000000000064" // 2001:4860:4860::64
- + "20014860486000000000000000006464"; // 2001:4860:4860::6464
-
- private static final String RDNSS_INFINITY_LIFETIME_OPTION_BYTES =
- "1905" // type=25, len=3 (24 bytes)
- + "0000" // reserved
- + "FFFFFFFF" // lifetime=0xffffffff
- + "20014860486000000000000000000064" // 2001:4860:4860::64
- + "20014860486000000000000000006464"; // 2001:4860:4860::6464
-
- private void assertRdnssOptMatches(final StructNdOptRdnss opt, int length, long lifetime,
- final Inet6Address[] servers) {
- assertEquals(StructNdOptRdnss.TYPE, opt.type);
- assertEquals(length, opt.length);
- assertEquals(lifetime, opt.header.lifetime);
- assertEquals(servers, opt.servers);
- }
-
- private ByteBuffer makeRdnssOption(byte type, byte length, long lifetime, String... servers)
- throws Exception {
- final ByteBuffer buf = ByteBuffer.allocate(8 + servers.length * 16)
- .put(type)
- .put(length)
- .putShort((short) 0) // Reserved
- .putInt((int) (lifetime & 0xFFFFFFFFL));
- for (int i = 0; i < servers.length; i++) {
- final byte[] rawBytes =
- ((Inet6Address) InetAddresses.parseNumericAddress(servers[i])).getAddress();
- buf.put(rawBytes);
- }
- buf.flip();
- return buf;
- }
-
- private void assertToByteBufferMatches(StructNdOptRdnss opt, String expected) {
- String actual = HexEncoding.encodeToString(opt.toByteBuffer().array());
- assertEquals(expected, actual);
- }
-
- private void doRdnssOptionParsing(final String optionHexString, int length, long lifetime,
- final Inet6Address[] servers) {
- final byte[] rawBytes = HexEncoding.decode(optionHexString);
- final StructNdOptRdnss opt = StructNdOptRdnss.parse(ByteBuffer.wrap(rawBytes));
- assertRdnssOptMatches(opt, length, lifetime, servers);
- assertToByteBufferMatches(opt, optionHexString);
- }
-
- @Test
- public void testParsing() throws Exception {
- doRdnssOptionParsing(RDNSS_OPTION_BYTES, 5 /* length */, 3600 /* lifetime */,
- DNS_SERVER_ADDRESSES);
- }
-
- @Test
- public void testParsing_infinityLifetime() throws Exception {
- doRdnssOptionParsing(RDNSS_INFINITY_LIFETIME_OPTION_BYTES, 5 /* length */,
- 0xffffffffL /* lifetime */, DNS_SERVER_ADDRESSES);
- }
-
- @Test
- public void testToByteBuffer() {
- final StructNdOptRdnss rdnss = new StructNdOptRdnss(DNS_SERVER_ADDRESSES, 3600);
- assertToByteBufferMatches(rdnss, RDNSS_OPTION_BYTES);
- }
-
- @Test
- public void testToByteBuffer_infinityLifetime() {
- final StructNdOptRdnss rdnss = new StructNdOptRdnss(DNS_SERVER_ADDRESSES, 0xffffffffL);
- assertToByteBufferMatches(rdnss, RDNSS_INFINITY_LIFETIME_OPTION_BYTES);
- }
-
- @Test
- public void testParsing_invalidType() throws Exception {
- final ByteBuffer buf = makeRdnssOption((byte) 38, (byte) 5 /* length */,
- 3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- assertNull(StructNdOptRdnss.parse(buf));
- }
-
- @Test
- public void testParsing_smallOptionLength() throws Exception {
- final ByteBuffer buf = makeRdnssOption((byte) ICMPV6_ND_OPTION_RDNSS,
- (byte) 2 /* length */, 3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- assertNull(StructNdOptRdnss.parse(buf));
- }
-
- @Test
- public void testParsing_oddOptionLength() throws Exception {
- final ByteBuffer buf = makeRdnssOption((byte) ICMPV6_ND_OPTION_RDNSS,
- (byte) 6 /* length */, 3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- assertNull(StructNdOptRdnss.parse(buf));
- }
-
- @Test
- public void testParsing_truncatedByteBuffer() throws Exception {
- ByteBuffer buf = makeRdnssOption((byte) ICMPV6_ND_OPTION_RDNSS,
- (byte) 5 /* length */, 3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- final int len = buf.limit();
- for (int i = 0; i < buf.limit() - 1; i++) {
- buf.flip();
- buf.limit(i);
- assertNull("Option truncated to " + i + " bytes, should have returned null",
- StructNdOptRdnss.parse(buf));
- }
- buf.flip();
- buf.limit(len);
-
- final StructNdOptRdnss opt = StructNdOptRdnss.parse(buf);
- assertRdnssOptMatches(opt, 5 /* length */, 3600 /* lifetime */, DNS_SERVER_ADDRESSES);
- }
-
- @Test
- public void testParsing_invalidByteBufferLength() throws Exception {
- final ByteBuffer buf = makeRdnssOption((byte) ICMPV6_ND_OPTION_RDNSS,
- (byte) 5 /* length */, 3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- buf.limit(20); // less than MIN_OPT_LEN * 8
- assertNull(StructNdOptRdnss.parse(buf));
- }
-
- @Test
- public void testConstructor_nullDnsServerAddressArray() {
- assertThrows(NullPointerException.class,
- () -> new StructNdOptRdnss(null /* servers */, 3600 /* lifetime */));
- }
-
- @Test
- public void testConstructor_emptyDnsServerAddressArray() {
- assertThrows(IllegalArgumentException.class,
- () -> new StructNdOptRdnss(new Inet6Address[0] /* empty server array */,
- 3600 /* lifetime*/));
- }
-
- @Test
- public void testToString() {
- final ByteBuffer buf = RdnssOption.build(3600 /* lifetime */, DNS_SERVER1, DNS_SERVER2);
- final StructNdOptRdnss opt = StructNdOptRdnss.parse(buf);
- final String expected = "NdOptRdnss(type: 25, length: 5, reserved: 0, lifetime: 3600,"
- + "servers:[2001:4860:4860::64,2001:4860:4860::6464])";
- assertRdnssOptMatches(opt, 5 /* length */, 3600 /* lifetime */, DNS_SERVER_ADDRESSES);
- assertEquals(expected, opt.toString());
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/StructNlAttrTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/StructNlAttrTest.java
deleted file mode 100644
index af3fac2e..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/StructNlAttrTest.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (C) 2021 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.netlink;
-
-import static com.android.net.module.util.netlink.RtNetlinkAddressMessage.IFA_FLAGS;
-import static com.android.net.module.util.netlink.RtNetlinkLinkMessage.IFLA_ADDRESS;
-import static com.android.net.module.util.netlink.RtNetlinkLinkMessage.IFLA_IFNAME;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import android.net.MacAddress;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class StructNlAttrTest {
- private static final MacAddress TEST_MAC_ADDRESS = MacAddress.fromString("00:11:22:33:44:55");
- private static final String TEST_INTERFACE_NAME = "wlan0";
- private static final int TEST_ADDR_FLAGS = 0x80;
-
- @Test
- public void testGetValueAsMacAddress() {
- final StructNlAttr attr1 = new StructNlAttr(IFLA_ADDRESS, TEST_MAC_ADDRESS);
- final MacAddress address1 = attr1.getValueAsMacAddress();
- assertEquals(address1, TEST_MAC_ADDRESS);
-
- // Invalid mac address byte array.
- final byte[] array = new byte[] {
- (byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33,
- (byte) 0x44, (byte) 0x55, (byte) 0x66,
- };
- final StructNlAttr attr2 = new StructNlAttr(IFLA_ADDRESS, array);
- final MacAddress address2 = attr2.getValueAsMacAddress();
- assertNull(address2);
- }
-
- @Test
- public void testGetValueAsString() {
- final StructNlAttr attr1 = new StructNlAttr(IFLA_IFNAME, TEST_INTERFACE_NAME);
- final String str1 = attr1.getValueAsString();
- assertEquals(str1, TEST_INTERFACE_NAME);
-
- final byte[] array = new byte[] {
- (byte) 0x77, (byte) 0x6c, (byte) 0x61, (byte) 0x6E, (byte) 0x30, (byte) 0x00,
- };
- final StructNlAttr attr2 = new StructNlAttr(IFLA_IFNAME, array);
- final String str2 = attr2.getValueAsString();
- assertEquals(str2, TEST_INTERFACE_NAME);
- }
-
- @Test
- public void testGetValueAsIntger() {
- final StructNlAttr attr1 = new StructNlAttr(IFA_FLAGS, TEST_ADDR_FLAGS);
- final Integer integer1 = attr1.getValueAsInteger();
- final int int1 = attr1.getValueAsInt(0x08 /* default value */);
- assertEquals(integer1, new Integer(TEST_ADDR_FLAGS));
- assertEquals(int1, TEST_ADDR_FLAGS);
-
- // Malformed attribute.
- final byte[] malformed_int = new byte[] { (byte) 0x0, (byte) 0x0, (byte) 0x80, };
- final StructNlAttr attr2 = new StructNlAttr(IFA_FLAGS, malformed_int);
- final Integer integer2 = attr2.getValueAsInteger();
- final int int2 = attr2.getValueAsInt(0x08 /* default value */);
- assertNull(integer2);
- assertEquals(int2, 0x08 /* default value */);
-
- // Null attribute value.
- final byte[] null_int = null;
- final StructNlAttr attr3 = new StructNlAttr(IFA_FLAGS, null_int);
- final Integer integer3 = attr3.getValueAsInteger();
- final int int3 = attr3.getValueAsInt(0x08 /* default value */);
- assertNull(integer3);
- assertEquals(int3, 0x08 /* default value */);
- }
-}
diff --git a/common/tests/unit/src/com/android/net/module/util/netlink/StructNlMsgHdrTest.java b/common/tests/unit/src/com/android/net/module/util/netlink/StructNlMsgHdrTest.java
deleted file mode 100644
index b7f68c62..00000000
--- a/common/tests/unit/src/com/android/net/module/util/netlink/StructNlMsgHdrTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright (C) 2020 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.netlink;
-
-import static org.junit.Assert.fail;
-
-import android.system.OsConstants;
-
-import androidx.test.filters.SmallTest;
-import androidx.test.runner.AndroidJUnit4;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-@RunWith(AndroidJUnit4.class)
-@SmallTest
-public class StructNlMsgHdrTest {
-
- public static final short TEST_NLMSG_LEN = 16;
- public static final short TEST_NLMSG_FLAGS = StructNlMsgHdr.NLM_F_REQUEST
- | StructNlMsgHdr.NLM_F_MULTI | StructNlMsgHdr.NLM_F_ACK | StructNlMsgHdr.NLM_F_ECHO;
- public static final short TEST_NLMSG_SEQ = 1234;
- public static final short TEST_NLMSG_PID = 5678;
-
- // Checking the header string nlmsg_{len, ..} of the number can make sure that the checking
- // number comes from the expected element.
- // TODO: Verify more flags once StructNlMsgHdr can distinguish the flags which have the same
- // value. For example, NLM_F_MATCH (0x200) and NLM_F_EXCL (0x200) can't be distinguished.
- // See StructNlMsgHdrTest#stringForNlMsgFlags.
- public static final String TEST_NLMSG_LEN_STR = "nlmsg_len{16}";
- public static final String TEST_NLMSG_FLAGS_STR =
- "NLM_F_REQUEST|NLM_F_MULTI|NLM_F_ACK|NLM_F_ECHO";
- public static final String TEST_NLMSG_SEQ_STR = "nlmsg_seq{1234}";
- public static final String TEST_NLMSG_PID_STR = "nlmsg_pid{5678}";
-
- private StructNlMsgHdr makeStructNlMsgHdr(short type) {
- final StructNlMsgHdr struct = new StructNlMsgHdr();
- struct.nlmsg_len = TEST_NLMSG_LEN;
- struct.nlmsg_type = type;
- struct.nlmsg_flags = TEST_NLMSG_FLAGS;
- struct.nlmsg_seq = TEST_NLMSG_SEQ;
- struct.nlmsg_pid = TEST_NLMSG_PID;
- return struct;
- }
-
- private static void assertContains(String actualValue, String expectedSubstring) {
- if (actualValue.contains(expectedSubstring)) return;
- fail("\"" + actualValue + "\" does not contain \"" + expectedSubstring + "\"");
- }
-
- @Test
- public void testToString() {
- StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_NEWADDR);
- String s = struct.toString();
- assertContains(s, TEST_NLMSG_LEN_STR);
- assertContains(s, TEST_NLMSG_FLAGS_STR);
- assertContains(s, TEST_NLMSG_SEQ_STR);
- assertContains(s, TEST_NLMSG_PID_STR);
- assertContains(s, "nlmsg_type{20()}");
-
- struct = makeStructNlMsgHdr(NetlinkConstants.SOCK_DIAG_BY_FAMILY);
- s = struct.toString();
- assertContains(s, TEST_NLMSG_LEN_STR);
- assertContains(s, TEST_NLMSG_FLAGS_STR);
- assertContains(s, TEST_NLMSG_SEQ_STR);
- assertContains(s, TEST_NLMSG_PID_STR);
- assertContains(s, "nlmsg_type{20()}");
- }
-
- @Test
- public void testToStringWithNetlinkFamily() {
- StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_NEWADDR);
- String s = struct.toString(OsConstants.NETLINK_ROUTE);
- assertContains(s, TEST_NLMSG_LEN_STR);
- assertContains(s, TEST_NLMSG_FLAGS_STR);
- assertContains(s, TEST_NLMSG_SEQ_STR);
- assertContains(s, TEST_NLMSG_PID_STR);
- assertContains(s, "nlmsg_type{20(RTM_NEWADDR)}");
-
- struct = makeStructNlMsgHdr(NetlinkConstants.SOCK_DIAG_BY_FAMILY);
- s = struct.toString(OsConstants.NETLINK_INET_DIAG);
- assertContains(s, TEST_NLMSG_LEN_STR);
- assertContains(s, TEST_NLMSG_FLAGS_STR);
- assertContains(s, TEST_NLMSG_SEQ_STR);
- assertContains(s, TEST_NLMSG_PID_STR);
- assertContains(s, "nlmsg_type{20(SOCK_DIAG_BY_FAMILY)}");
- }
-}