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