1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.netlink; 18 19 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_DELETE; 20 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET; 21 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_CTRZERO; 22 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_DYING; 23 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_STATS; 24 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_STATS_CPU; 25 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_GET_UNCONFIRMED; 26 import static android.net.netlink.NetlinkConstants.IPCTNL_MSG_CT_NEW; 27 import static android.net.netlink.NetlinkConstants.NFNL_SUBSYS_CTNETLINK; 28 import static android.net.netlink.NetlinkConstants.NLMSG_DONE; 29 import static android.net.netlink.NetlinkConstants.NLMSG_ERROR; 30 import static android.net.netlink.NetlinkConstants.NLMSG_NOOP; 31 import static android.net.netlink.NetlinkConstants.NLMSG_OVERRUN; 32 import static android.net.netlink.NetlinkConstants.RTM_DELADDR; 33 import static android.net.netlink.NetlinkConstants.RTM_DELLINK; 34 import static android.net.netlink.NetlinkConstants.RTM_DELNEIGH; 35 import static android.net.netlink.NetlinkConstants.RTM_DELROUTE; 36 import static android.net.netlink.NetlinkConstants.RTM_DELRULE; 37 import static android.net.netlink.NetlinkConstants.RTM_GETADDR; 38 import static android.net.netlink.NetlinkConstants.RTM_GETLINK; 39 import static android.net.netlink.NetlinkConstants.RTM_GETNEIGH; 40 import static android.net.netlink.NetlinkConstants.RTM_GETROUTE; 41 import static android.net.netlink.NetlinkConstants.RTM_GETRULE; 42 import static android.net.netlink.NetlinkConstants.RTM_NEWADDR; 43 import static android.net.netlink.NetlinkConstants.RTM_NEWLINK; 44 import static android.net.netlink.NetlinkConstants.RTM_NEWNDUSEROPT; 45 import static android.net.netlink.NetlinkConstants.RTM_NEWNEIGH; 46 import static android.net.netlink.NetlinkConstants.RTM_NEWROUTE; 47 import static android.net.netlink.NetlinkConstants.RTM_NEWRULE; 48 import static android.net.netlink.NetlinkConstants.RTM_SETLINK; 49 import static android.net.netlink.NetlinkConstants.SOCK_DIAG_BY_FAMILY; 50 import static android.net.netlink.NetlinkConstants.stringForNlMsgType; 51 import static android.system.OsConstants.NETLINK_INET_DIAG; 52 import static android.system.OsConstants.NETLINK_NETFILTER; 53 import static android.system.OsConstants.NETLINK_ROUTE; 54 55 import static org.junit.Assert.assertEquals; 56 import static org.junit.Assert.assertTrue; 57 58 import androidx.test.filters.SmallTest; 59 import androidx.test.runner.AndroidJUnit4; 60 61 import org.junit.Test; 62 import org.junit.runner.RunWith; 63 64 @RunWith(AndroidJUnit4.class) 65 @SmallTest 66 public class NetlinkConstantsTest { 67 private static final short UNKNOWN_FAMILY = 1234; 68 makeCtType(short msgType)69 private short makeCtType(short msgType) { 70 return (short) (NFNL_SUBSYS_CTNETLINK << 8 | (byte) msgType); 71 } 72 73 @Test testStringForNlMsgType()74 public void testStringForNlMsgType() { 75 assertEquals("RTM_NEWLINK", stringForNlMsgType(RTM_NEWLINK, NETLINK_ROUTE)); 76 assertEquals("RTM_DELLINK", stringForNlMsgType(RTM_DELLINK, NETLINK_ROUTE)); 77 assertEquals("RTM_GETLINK", stringForNlMsgType(RTM_GETLINK, NETLINK_ROUTE)); 78 assertEquals("RTM_SETLINK", stringForNlMsgType(RTM_SETLINK, NETLINK_ROUTE)); 79 assertEquals("RTM_NEWADDR", stringForNlMsgType(RTM_NEWADDR, NETLINK_ROUTE)); 80 assertEquals("RTM_DELADDR", stringForNlMsgType(RTM_DELADDR, NETLINK_ROUTE)); 81 assertEquals("RTM_GETADDR", stringForNlMsgType(RTM_GETADDR, NETLINK_ROUTE)); 82 assertEquals("RTM_NEWROUTE", stringForNlMsgType(RTM_NEWROUTE, NETLINK_ROUTE)); 83 assertEquals("RTM_DELROUTE", stringForNlMsgType(RTM_DELROUTE, NETLINK_ROUTE)); 84 assertEquals("RTM_GETROUTE", stringForNlMsgType(RTM_GETROUTE, NETLINK_ROUTE)); 85 assertEquals("RTM_NEWNEIGH", stringForNlMsgType(RTM_NEWNEIGH, NETLINK_ROUTE)); 86 assertEquals("RTM_DELNEIGH", stringForNlMsgType(RTM_DELNEIGH, NETLINK_ROUTE)); 87 assertEquals("RTM_GETNEIGH", stringForNlMsgType(RTM_GETNEIGH, NETLINK_ROUTE)); 88 assertEquals("RTM_NEWRULE", stringForNlMsgType(RTM_NEWRULE, NETLINK_ROUTE)); 89 assertEquals("RTM_DELRULE", stringForNlMsgType(RTM_DELRULE, NETLINK_ROUTE)); 90 assertEquals("RTM_GETRULE", stringForNlMsgType(RTM_GETRULE, NETLINK_ROUTE)); 91 assertEquals("RTM_NEWNDUSEROPT", stringForNlMsgType(RTM_NEWNDUSEROPT, NETLINK_ROUTE)); 92 93 assertEquals("SOCK_DIAG_BY_FAMILY", 94 stringForNlMsgType(SOCK_DIAG_BY_FAMILY, NETLINK_INET_DIAG)); 95 96 assertEquals("IPCTNL_MSG_CT_NEW", 97 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_NEW), NETLINK_NETFILTER)); 98 assertEquals("IPCTNL_MSG_CT_GET", 99 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET), NETLINK_NETFILTER)); 100 assertEquals("IPCTNL_MSG_CT_DELETE", 101 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_DELETE), NETLINK_NETFILTER)); 102 assertEquals("IPCTNL_MSG_CT_GET_CTRZERO", 103 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_CTRZERO), NETLINK_NETFILTER)); 104 assertEquals("IPCTNL_MSG_CT_GET_STATS_CPU", 105 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_STATS_CPU), NETLINK_NETFILTER)); 106 assertEquals("IPCTNL_MSG_CT_GET_STATS", 107 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_STATS), NETLINK_NETFILTER)); 108 assertEquals("IPCTNL_MSG_CT_GET_DYING", 109 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_DYING), NETLINK_NETFILTER)); 110 assertEquals("IPCTNL_MSG_CT_GET_UNCONFIRMED", 111 stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_GET_UNCONFIRMED), NETLINK_NETFILTER)); 112 } 113 114 @Test testStringForNlMsgType_ControlMessage()115 public void testStringForNlMsgType_ControlMessage() { 116 for (int family : new int[]{NETLINK_ROUTE, NETLINK_INET_DIAG, NETLINK_NETFILTER}) { 117 assertEquals("NLMSG_NOOP", stringForNlMsgType(NLMSG_NOOP, family)); 118 assertEquals("NLMSG_ERROR", stringForNlMsgType(NLMSG_ERROR, family)); 119 assertEquals("NLMSG_DONE", stringForNlMsgType(NLMSG_DONE, family)); 120 assertEquals("NLMSG_OVERRUN", stringForNlMsgType(NLMSG_OVERRUN, family)); 121 } 122 } 123 124 @Test testStringForNlMsgType_UnknownFamily()125 public void testStringForNlMsgType_UnknownFamily() { 126 assertTrue(stringForNlMsgType(RTM_NEWLINK, UNKNOWN_FAMILY).startsWith("unknown")); 127 assertTrue(stringForNlMsgType(SOCK_DIAG_BY_FAMILY, UNKNOWN_FAMILY).startsWith("unknown")); 128 assertTrue(stringForNlMsgType(makeCtType(IPCTNL_MSG_CT_NEW), UNKNOWN_FAMILY) 129 .startsWith("unknown")); 130 } 131 } 132