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 com.android.net.module.util.netlink; 18 19 import static com.android.net.module.util.netlink.StructNlMsgHdr.NLM_F_DUMP; 20 import static org.junit.Assert.fail; 21 22 import android.system.OsConstants; 23 24 import androidx.test.filters.SmallTest; 25 import androidx.test.runner.AndroidJUnit4; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 @RunWith(AndroidJUnit4.class) 31 @SmallTest 32 public class StructNlMsgHdrTest { 33 34 public static final short TEST_NLMSG_LEN = 16; 35 public static final short TEST_NLMSG_FLAGS = StructNlMsgHdr.NLM_F_REQUEST 36 | StructNlMsgHdr.NLM_F_MULTI | StructNlMsgHdr.NLM_F_ACK | StructNlMsgHdr.NLM_F_ECHO; 37 public static final short TEST_NLMSG_SEQ = 1234; 38 public static final short TEST_NLMSG_PID = 5678; 39 40 // Checking the header string nlmsg_{len, ..} of the number can make sure that the checking 41 // number comes from the expected element. 42 // TODO: Verify more flags once StructNlMsgHdr can distinguish the flags which have the same 43 // value. For example, NLM_F_MATCH (0x200) and NLM_F_EXCL (0x200) can't be distinguished. 44 // See StructNlMsgHdrTest#stringForNlMsgFlags. 45 public static final String TEST_NLMSG_LEN_STR = "nlmsg_len{16}"; 46 public static final String TEST_NLMSG_FLAGS_STR = 47 "NLM_F_REQUEST|NLM_F_MULTI|NLM_F_ACK|NLM_F_ECHO"; 48 public static final String TEST_NLMSG_SEQ_STR = "nlmsg_seq{1234}"; 49 public static final String TEST_NLMSG_PID_STR = "nlmsg_pid{5678}"; 50 makeStructNlMsgHdr(short type)51 private StructNlMsgHdr makeStructNlMsgHdr(short type) { 52 return makeStructNlMsgHdr(type, TEST_NLMSG_FLAGS); 53 } 54 makeStructNlMsgHdr(short type, short flags)55 private StructNlMsgHdr makeStructNlMsgHdr(short type, short flags) { 56 final StructNlMsgHdr struct = new StructNlMsgHdr(); 57 struct.nlmsg_len = TEST_NLMSG_LEN; 58 struct.nlmsg_type = type; 59 struct.nlmsg_flags = flags; 60 struct.nlmsg_seq = TEST_NLMSG_SEQ; 61 struct.nlmsg_pid = TEST_NLMSG_PID; 62 return struct; 63 } 64 assertContains(String actualValue, String expectedSubstring)65 private static void assertContains(String actualValue, String expectedSubstring) { 66 if (actualValue.contains(expectedSubstring)) return; 67 fail("\"" + actualValue + "\" does not contain \"" + expectedSubstring + "\""); 68 } 69 assertNotContains(String actualValue, String unexpectedSubstring)70 private static void assertNotContains(String actualValue, String unexpectedSubstring) { 71 if (!actualValue.contains(unexpectedSubstring)) return; 72 fail("\"" + actualValue + "\" contains \"" + unexpectedSubstring + "\""); 73 } 74 75 @Test testToString()76 public void testToString() { 77 StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_NEWADDR); 78 String s = struct.toString(); 79 assertContains(s, TEST_NLMSG_LEN_STR); 80 assertContains(s, TEST_NLMSG_FLAGS_STR); 81 assertContains(s, TEST_NLMSG_SEQ_STR); 82 assertContains(s, TEST_NLMSG_PID_STR); 83 assertContains(s, "nlmsg_type{20()}"); 84 85 struct = makeStructNlMsgHdr(NetlinkConstants.SOCK_DIAG_BY_FAMILY); 86 s = struct.toString(); 87 assertContains(s, TEST_NLMSG_LEN_STR); 88 assertContains(s, TEST_NLMSG_FLAGS_STR); 89 assertContains(s, TEST_NLMSG_SEQ_STR); 90 assertContains(s, TEST_NLMSG_PID_STR); 91 assertContains(s, "nlmsg_type{20()}"); 92 } 93 94 @Test testToStringWithNetlinkFamily()95 public void testToStringWithNetlinkFamily() { 96 StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_NEWADDR); 97 String s = struct.toString(OsConstants.NETLINK_ROUTE); 98 assertContains(s, TEST_NLMSG_LEN_STR); 99 assertContains(s, TEST_NLMSG_FLAGS_STR); 100 assertContains(s, TEST_NLMSG_SEQ_STR); 101 assertContains(s, TEST_NLMSG_PID_STR); 102 assertContains(s, "nlmsg_type{20(RTM_NEWADDR)}"); 103 104 struct = makeStructNlMsgHdr(NetlinkConstants.SOCK_DIAG_BY_FAMILY); 105 s = struct.toString(OsConstants.NETLINK_INET_DIAG); 106 assertContains(s, TEST_NLMSG_LEN_STR); 107 assertContains(s, TEST_NLMSG_FLAGS_STR); 108 assertContains(s, TEST_NLMSG_SEQ_STR); 109 assertContains(s, TEST_NLMSG_PID_STR); 110 assertContains(s, "nlmsg_type{20(SOCK_DIAG_BY_FAMILY)}"); 111 } 112 113 @Test testToString_flags_dumpRequest()114 public void testToString_flags_dumpRequest() { 115 final short flags = StructNlMsgHdr.NLM_F_REQUEST | StructNlMsgHdr.NLM_F_DUMP; 116 StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_GETROUTE, flags); 117 118 String s = struct.toString(OsConstants.NETLINK_ROUTE); 119 120 assertContains(s, "RTM_GETROUTE"); 121 assertContains(s, "NLM_F_REQUEST"); 122 assertContains(s, "NLM_F_DUMP"); 123 // NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH; 124 assertNotContains(s, "NLM_F_MATCH"); 125 assertNotContains(s, "NLM_F_ROOT"); 126 } 127 128 @Test testToString_flags_root()129 public void testToString_flags_root() { 130 final short flags = StructNlMsgHdr.NLM_F_ROOT; 131 StructNlMsgHdr struct = makeStructNlMsgHdr(NetlinkConstants.RTM_GETROUTE, flags); 132 133 String s = struct.toString(OsConstants.NETLINK_ROUTE); 134 135 assertContains(s, "NLM_F_ROOT"); 136 // NLM_F_DUMP = NLM_F_ROOT | NLM_F_MATCH; 137 assertNotContains(s, "NLM_F_DUMP"); 138 } 139 } 140