Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df402656 authored by Hungming Chen's avatar Hungming Chen
Browse files

[NFCT.NS.9] Parse CTA_TUPLE_ORIG and CTA_TUPLE_REPLY

Test: atest TetheringCoverageTests
Change-Id: Ic2c85f3cbe4a8834a5b7fbc01e16da5a34133128
parent 9140fe3b
Loading
Loading
Loading
Loading
+155 −4
Original line number Original line Diff line number Diff line
@@ -16,6 +16,8 @@


package android.net.netlink;
package android.net.netlink;


import static android.net.netlink.StructNlAttr.findNextAttrOfType;
import static android.net.netlink.StructNlAttr.makeNestedType;
import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK;
import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK;
import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE;
import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE;
import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
@@ -23,9 +25,11 @@ import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
import static java.nio.ByteOrder.BIG_ENDIAN;
import static java.nio.ByteOrder.BIG_ENDIAN;


import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.system.OsConstants;
import android.system.OsConstants;


import java.net.Inet4Address;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ByteOrder;


@@ -59,6 +63,37 @@ public class ConntrackMessage extends NetlinkMessage {
    public static final short CTA_PROTO_SRC_PORT = 2;
    public static final short CTA_PROTO_SRC_PORT = 2;
    public static final short CTA_PROTO_DST_PORT = 3;
    public static final short CTA_PROTO_DST_PORT = 3;


    /**
     * A tuple for the conntrack connection information.
     *
     * see also CTA_TUPLE_ORIG and CTA_TUPLE_REPLY.
     */
    public static class Tuple {
        public final Inet4Address srcIp;
        public final Inet4Address dstIp;
        // TODO: tuple proto for CTA_TUPLE_PROTO.

        public Tuple(TupleIpv4 ip) {
            this.srcIp = ip.src;
            this.dstIp = ip.dst;
        }
    }

    /**
     * A tuple for the conntrack connection address.
     *
     * see also CTA_TUPLE_IP.
     */
    public static class TupleIpv4 {
        public final Inet4Address src;
        public final Inet4Address dst;

        public TupleIpv4(Inet4Address src, Inet4Address dst) {
            this.src = src;
            this.dst = dst;
        }
    }

    public static byte[] newIPv4TimeoutUpdateRequest(
    public static byte[] newIPv4TimeoutUpdateRequest(
            int proto, Inet4Address src, int sport, Inet4Address dst, int dport, int timeoutSec) {
            int proto, Inet4Address src, int sport, Inet4Address dst, int dport, int timeoutSec) {
        // *** STYLE WARNING ***
        // *** STYLE WARNING ***
@@ -113,19 +148,33 @@ public class ConntrackMessage extends NetlinkMessage {
        }
        }


        final int baseOffset = byteBuffer.position();
        final int baseOffset = byteBuffer.position();
        StructNlAttr nlAttr = StructNlAttr.findNextAttrOfType(CTA_STATUS, byteBuffer);
        StructNlAttr nlAttr = findNextAttrOfType(CTA_STATUS, byteBuffer);
        int status = 0;
        int status = 0;
        if (nlAttr != null) {
        if (nlAttr != null) {
            status = nlAttr.getValueAsBe32(0);
            status = nlAttr.getValueAsBe32(0);
        }
        }


        byteBuffer.position(baseOffset);
        byteBuffer.position(baseOffset);
        nlAttr = StructNlAttr.findNextAttrOfType(CTA_TIMEOUT, byteBuffer);
        nlAttr = findNextAttrOfType(CTA_TIMEOUT, byteBuffer);
        int timeoutSec = 0;
        int timeoutSec = 0;
        if (nlAttr != null) {
        if (nlAttr != null) {
            timeoutSec = nlAttr.getValueAsBe32(0);
            timeoutSec = nlAttr.getValueAsBe32(0);
        }
        }


        byteBuffer.position(baseOffset);
        nlAttr = findNextAttrOfType(makeNestedType(CTA_TUPLE_ORIG), byteBuffer);
        Tuple tupleOrig = null;
        if (nlAttr != null) {
            tupleOrig = parseTuple(nlAttr.getValueAsByteBuffer());
        }

        byteBuffer.position(baseOffset);
        nlAttr = findNextAttrOfType(makeNestedType(CTA_TUPLE_REPLY), byteBuffer);
        Tuple tupleReply = null;
        if (nlAttr != null) {
            tupleReply = parseTuple(nlAttr.getValueAsByteBuffer());
        }

        // Advance to the end of the message.
        // Advance to the end of the message.
        byteBuffer.position(baseOffset);
        byteBuffer.position(baseOffset);
        final int kMinConsumed = StructNlMsgHdr.STRUCT_SIZE + StructNfGenMsg.STRUCT_SIZE;
        final int kMinConsumed = StructNlMsgHdr.STRUCT_SIZE + StructNfGenMsg.STRUCT_SIZE;
@@ -136,13 +185,108 @@ public class ConntrackMessage extends NetlinkMessage {
        }
        }
        byteBuffer.position(baseOffset + kAdditionalSpace);
        byteBuffer.position(baseOffset + kAdditionalSpace);


        return new ConntrackMessage(header, nfGenMsg, status, timeoutSec);
        return new ConntrackMessage(header, nfGenMsg, tupleOrig, tupleReply, status, timeoutSec);
    }

    /**
     * Parses a conntrack tuple from a {@link ByteBuffer}.
     *
     * The attribute parsing is interesting on:
     * - CTA_TUPLE_IP
     *     CTA_IP_V4_SRC
     *     CTA_IP_V4_DST
     * - CTA_TUPLE_PROTO
     *     CTA_PROTO_NUM
     *     CTA_PROTO_SRC_PORT
     *     CTA_PROTO_DST_PORT
     *
     * Assume that the minimum size is the sum of CTA_TUPLE_IP (size: 20) and CTA_TUPLE_PROTO
     * (size: 28). Here is an example for an expected CTA_TUPLE_ORIG message in raw data:
     * +--------------------------------------------------------------------------------------+
     * | CTA_TUPLE_ORIG                                                                       |
     * +--------------------------+-----------------------------------------------------------+
     * | 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              |
     * | 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=433 (big endian)    |
     * +--------------------------+-----------------------------------------------------------+
     *
     * The position of the byte buffer doesn't set to the end when the function returns. It is okay
     * because the caller ConntrackMessage#parse has passed a copy which is used for this parser
     * only. Moreover, the parser behavior is the same as other existing netlink struct class
     * parser. Ex: StructInetDiagMsg#parse.
     */
    @Nullable
    private static Tuple parseTuple(@Nullable ByteBuffer byteBuffer) {
        if (byteBuffer == null) return null;

        TupleIpv4 tupleIpv4 = null;

        final int baseOffset = byteBuffer.position();
        StructNlAttr nlAttr = findNextAttrOfType(makeNestedType(CTA_TUPLE_IP), byteBuffer);
        if (nlAttr != null) {
            tupleIpv4 = parseTupleIpv4(nlAttr.getValueAsByteBuffer());
        }
        if (tupleIpv4 == null) return null;

        return new Tuple(tupleIpv4);
    }

    @Nullable
    private static Inet4Address castToInet4Address(@Nullable InetAddress address) {
        if (address == null || !(address instanceof Inet4Address)) return null;
        return (Inet4Address) address;
    }

    @Nullable
    private static TupleIpv4 parseTupleIpv4(@Nullable ByteBuffer byteBuffer) {
        if (byteBuffer == null) return null;

        Inet4Address src = null;
        Inet4Address dst = null;

        final int baseOffset = byteBuffer.position();
        StructNlAttr nlAttr = findNextAttrOfType(CTA_IP_V4_SRC, byteBuffer);
        if (nlAttr != null) {
            src = castToInet4Address(nlAttr.getValueAsInetAddress());
        }
        if (src == null) return null;

        byteBuffer.position(baseOffset);
        nlAttr = findNextAttrOfType(CTA_IP_V4_DST, byteBuffer);
        if (nlAttr != null) {
            dst = castToInet4Address(nlAttr.getValueAsInetAddress());
        }
        if (dst == null) return null;

        return new TupleIpv4(src, dst);
    }
    }


    /**
    /**
     * Netfilter header.
     * Netfilter header.
     */
     */
    public final StructNfGenMsg nfGenMsg;
    public final StructNfGenMsg nfGenMsg;
    /**
     * Original direction conntrack tuple.
     *
     * The tuple is determined by the parsed attribute value CTA_TUPLE_ORIG, or null if the
     * tuple could not be parsed successfully (for example, if it was truncated or absent).
     */
    @Nullable
    public final Tuple tupleOrig;
    /**
     * Reply direction conntrack tuple.
     *
     * The tuple is determined by the parsed attribute value CTA_TUPLE_REPLY, or null if the
     * tuple could not be parsed successfully (for example, if it was truncated or absent).
     */
    @Nullable
    public final Tuple tupleReply;
    /**
    /**
     * Connection status. A bitmask of ip_conntrack_status enum flags.
     * Connection status. A bitmask of ip_conntrack_status enum flags.
     *
     *
@@ -165,14 +309,21 @@ public class ConntrackMessage extends NetlinkMessage {
    private ConntrackMessage() {
    private ConntrackMessage() {
        super(new StructNlMsgHdr());
        super(new StructNlMsgHdr());
        nfGenMsg = new StructNfGenMsg((byte) OsConstants.AF_INET);
        nfGenMsg = new StructNfGenMsg((byte) OsConstants.AF_INET);

        // This constructor is only used by #newIPv4TimeoutUpdateRequest which doesn't use these
        // data member for packing message. Simply fill them to null or 0.
        tupleOrig = null;
        tupleReply = null;
        status = 0;
        status = 0;
        timeoutSec = 0;
        timeoutSec = 0;
    }
    }


    private ConntrackMessage(@NonNull StructNlMsgHdr header, @NonNull StructNfGenMsg nfGenMsg,
    private ConntrackMessage(@NonNull StructNlMsgHdr header, @NonNull StructNfGenMsg nfGenMsg,
            int status, int timeoutSec) {
            @Nullable Tuple tupleOrig, @Nullable Tuple tupleReply, int status, int timeoutSec) {
        super(header);
        super(header);
        this.nfGenMsg = nfGenMsg;
        this.nfGenMsg = nfGenMsg;
        this.tupleOrig = tupleOrig;
        this.tupleReply = tupleReply;
        this.status = status;
        this.status = status;
        this.timeoutSec = timeoutSec;
        this.timeoutSec = timeoutSec;
    }
    }
+130 −9
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.net.netlink.NetlinkConstants.NFNL_SUBSYS_CTNETLINK;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.junit.Assume.assumeTrue;


@@ -39,6 +40,7 @@ import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ByteOrder;
import java.util.Arrays;


@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
@SmallTest
@SmallTest
@@ -166,7 +168,13 @@ public class ConntrackMessageTest {
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((short) 0, nfmsgHdr.res_id);
        assertEquals((short) 0, nfmsgHdr.res_id);


        // TODO: Parse the CTA_TUPLE_ORIG.
        assertEquals(InetAddress.parseNumericAddress("192.168.43.209"),
                conntrackMessage.tupleOrig.srcIp);
        assertEquals(InetAddress.parseNumericAddress("23.211.13.26"),
                conntrackMessage.tupleOrig.dstIp);

        assertNull(conntrackMessage.tupleReply);

        assertEquals(0 /* absent */, conntrackMessage.status);
        assertEquals(0 /* absent */, conntrackMessage.status);
        assertEquals(432000, conntrackMessage.timeoutSec);
        assertEquals(432000, conntrackMessage.timeoutSec);
    }
    }
@@ -206,18 +214,24 @@ public class ConntrackMessageTest {
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((short) 0, nfmsgHdr.res_id);
        assertEquals((short) 0, nfmsgHdr.res_id);


        // TODO: Parse the CTA_TUPLE_ORIG.
        assertEquals(InetAddress.parseNumericAddress("100.96.167.146"),
                conntrackMessage.tupleOrig.srcIp);
        assertEquals(InetAddress.parseNumericAddress("216.58.197.10"),
                conntrackMessage.tupleOrig.dstIp);

        assertNull(conntrackMessage.tupleReply);

        assertEquals(0 /* absent */, conntrackMessage.status);
        assertEquals(0 /* absent */, conntrackMessage.status);
        assertEquals(180, conntrackMessage.timeoutSec);
        assertEquals(180, conntrackMessage.timeoutSec);
    }
    }


    // TODO: Add conntrack message attributes to have further verification.
    // TODO: Add conntrack message attributes to have further verification.
    public static final String CT_V4NEW_HEX =
    public static final String CT_V4NEW_TCP_HEX =
            // CHECKSTYLE:OFF IndentationCheck
            // CHECKSTYLE:OFF IndentationCheck
            // struct nlmsghdr
            // struct nlmsghdr
            "24000000" +      // length = 36
            "8C000000" +      // length = 140
            "0001" +          // type = NFNL_SUBSYS_CTNETLINK (1) << 8 | IPCTNL_MSG_CT_NEW (0)
            "0001" +          // type = NFNL_SUBSYS_CTNETLINK (1) << 8 | IPCTNL_MSG_CT_NEW (0)
            "0006" +          // flags = NLM_F_CREATE | NLM_F_EXCL
            "0006" +          // flags = NLM_F_CREATE (1 << 10) | NLM_F_EXCL (1 << 9)
            "00000000" +      // seqno = 0
            "00000000" +      // seqno = 0
            "00000000" +      // pid = 0
            "00000000" +      // pid = 0
            // struct nfgenmsg
            // struct nfgenmsg
@@ -225,6 +239,34 @@ public class ConntrackMessageTest {
            "00" +            // version = NFNETLINK_V0
            "00" +            // version = NFNETLINK_V0
            "1234" +          // res_id = 0x1234 (big endian)
            "1234" +          // res_id = 0x1234 (big endian)
             // struct nlattr
             // 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
            "0800" +          // nla_len = 8
            "0300" +          // nla_type = CTA_STATUS
            "0300" +          // nla_type = CTA_STATUS
            "00000198" +      // nla_value = 0b110011000 (big endian)
            "00000198" +      // nla_value = 0b110011000 (big endian)
@@ -235,14 +277,14 @@ public class ConntrackMessageTest {
            "0700" +          // nla_type = CTA_TIMEOUT
            "0700" +          // nla_type = CTA_TIMEOUT
            "00000078";       // nla_value = 120 (big endian)
            "00000078";       // nla_value = 120 (big endian)
            // CHECKSTYLE:ON IndentationCheck
            // CHECKSTYLE:ON IndentationCheck
    public static final byte[] CT_V4NEW_BYTES =
    public static final byte[] CT_V4NEW_TCP_BYTES =
            HexEncoding.decode(CT_V4NEW_HEX.replaceAll(" ", "").toCharArray(), false);
            HexEncoding.decode(CT_V4NEW_TCP_HEX.replaceAll(" ", "").toCharArray(), false);


    @Test
    @Test
    public void testParseCtNew() {
    public void testParseCtNew() {
        assumeTrue(USING_LE);
        assumeTrue(USING_LE);


        final ByteBuffer byteBuffer = ByteBuffer.wrap(CT_V4NEW_BYTES);
        final ByteBuffer byteBuffer = ByteBuffer.wrap(CT_V4NEW_TCP_BYTES);
        byteBuffer.order(ByteOrder.nativeOrder());
        byteBuffer.order(ByteOrder.nativeOrder());
        final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
        final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer, OsConstants.NETLINK_NETFILTER);
        assertNotNull(msg);
        assertNotNull(msg);
@@ -251,7 +293,7 @@ public class ConntrackMessageTest {


        final StructNlMsgHdr hdr = conntrackMessage.getHeader();
        final StructNlMsgHdr hdr = conntrackMessage.getHeader();
        assertNotNull(hdr);
        assertNotNull(hdr);
        assertEquals(36, hdr.nlmsg_len);
        assertEquals(140, hdr.nlmsg_len);
        assertEquals(makeCtType(IPCTNL_MSG_CT_NEW), hdr.nlmsg_type);
        assertEquals(makeCtType(IPCTNL_MSG_CT_NEW), hdr.nlmsg_type);
        assertEquals((short) (StructNlMsgHdr.NLM_F_CREATE | StructNlMsgHdr.NLM_F_EXCL),
        assertEquals((short) (StructNlMsgHdr.NLM_F_CREATE | StructNlMsgHdr.NLM_F_EXCL),
                hdr.nlmsg_flags);
                hdr.nlmsg_flags);
@@ -264,7 +306,86 @@ public class ConntrackMessageTest {
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((byte) StructNfGenMsg.NFNETLINK_V0, nfmsgHdr.version);
        assertEquals((short) 0x1234, nfmsgHdr.res_id);
        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(InetAddress.parseNumericAddress("140.112.8.116"),
                conntrackMessage.tupleReply.srcIp);
        assertEquals(InetAddress.parseNumericAddress("100.81.179.1"),
                conntrackMessage.tupleReply.dstIp);

        assertEquals(0x198, conntrackMessage.status);
        assertEquals(0x198, conntrackMessage.status);
        assertEquals(120, conntrackMessage.timeoutSec);
        assertEquals(120, conntrackMessage.timeoutSec);

        // TODO: parse the attribute CTA_TUPLE_PROTO once ConntrackMessage supports.
    }

    @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";
            // 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(3, messageCount);
    }
    }
}
}