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

Commit eeb63e46 authored by Hungming Chen's avatar Hungming Chen Committed by Automerger Merge Worker
Browse files

[NFCT.NS.5] Add toString test for StructNlMsgHdr am: ece691dc am: 1705f476 am: 4027f0b7

Original change: https://android-review.googlesource.com/c/platform/packages/modules/NetworkStack/+/1481217

Change-Id: Icf3abac3c690f97fcb556f4212543027712117df
parents 91ba625e 4027f0b7
Loading
Loading
Loading
Loading
+5 −2
Original line number Original line Diff line number Diff line
@@ -47,7 +47,8 @@ public class StructNlMsgHdr {
    public static final short NLM_F_CREATE    = 0x400;
    public static final short NLM_F_CREATE    = 0x400;
    public static final short NLM_F_APPEND    = 0x800;
    public static final short NLM_F_APPEND    = 0x800;



    // TODO: Probably need to distinguish the flags which have the same value. For example,
    // NLM_F_MATCH (0x200) and NLM_F_EXCL (0x200).
    public static String stringForNlMsgFlags(short flags) {
    public static String stringForNlMsgFlags(short flags) {
        final StringBuilder sb = new StringBuilder();
        final StringBuilder sb = new StringBuilder();
        if ((flags & NLM_F_REQUEST) != 0) {
        if ((flags & NLM_F_REQUEST) != 0) {
@@ -133,7 +134,9 @@ public class StructNlMsgHdr {
    /**
    /**
     * Transform a netlink header into a string. The netlink family is required for transforming
     * Transform a netlink header into a string. The netlink family is required for transforming
     * a netlink type integer into a string.
     * a netlink type integer into a string.
     * @param nlFamily netlink family.
     * @param nlFamily netlink family. Using Integer will not incur autoboxing penalties because
     *                 family values are small, and all Integer objects between -128 and 127 are
     *                 statically cached. See Integer.IntegerCache.
     * @return A list of header elements.
     * @return A list of header elements.
     */
     */
    @NonNull
    @NonNull
+102 −0
Original line number Original line Diff line number Diff line
/*
 * 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 android.net.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)}");
    }
}