1 /* 2 * Copyright (C) 2015 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.NetlinkSocket.DEFAULT_RECV_BUFSIZE; 20 import static android.system.OsConstants.NETLINK_ROUTE; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNotNull; 24 import static org.junit.Assert.assertTrue; 25 26 import android.net.netlink.NetlinkSocket; 27 import android.net.netlink.RtNetlinkNeighborMessage; 28 import android.net.netlink.StructNlMsgHdr; 29 import android.system.NetlinkSocketAddress; 30 import android.system.Os; 31 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import libcore.io.IoUtils; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 import java.io.FileDescriptor; 41 import java.nio.ByteBuffer; 42 import java.nio.ByteOrder; 43 44 @RunWith(AndroidJUnit4.class) 45 @SmallTest 46 public class NetlinkSocketTest { 47 private final String TAG = "NetlinkSocketTest"; 48 49 @Test testBasicWorkingGetNeighborsQuery()50 public void testBasicWorkingGetNeighborsQuery() throws Exception { 51 final FileDescriptor fd = NetlinkSocket.forProto(NETLINK_ROUTE); 52 assertNotNull(fd); 53 54 NetlinkSocket.connectToKernel(fd); 55 56 final NetlinkSocketAddress localAddr = (NetlinkSocketAddress) Os.getsockname(fd); 57 assertNotNull(localAddr); 58 assertEquals(0, localAddr.getGroupsMask()); 59 assertTrue(0 != localAddr.getPortId()); 60 61 final int TEST_SEQNO = 5; 62 final byte[] req = RtNetlinkNeighborMessage.newGetNeighborsRequest(TEST_SEQNO); 63 assertNotNull(req); 64 65 final long TIMEOUT = 500; 66 assertEquals(req.length, NetlinkSocket.sendMessage(fd, req, 0, req.length, TIMEOUT)); 67 68 int neighMessageCount = 0; 69 int doneMessageCount = 0; 70 71 while (doneMessageCount == 0) { 72 ByteBuffer response = NetlinkSocket.recvMessage(fd, DEFAULT_RECV_BUFSIZE, TIMEOUT); 73 assertNotNull(response); 74 assertTrue(StructNlMsgHdr.STRUCT_SIZE <= response.limit()); 75 assertEquals(0, response.position()); 76 assertEquals(ByteOrder.nativeOrder(), response.order()); 77 78 // Verify the messages at least appears minimally reasonable. 79 while (response.remaining() > 0) { 80 final NetlinkMessage msg = NetlinkMessage.parse(response); 81 assertNotNull(msg); 82 final StructNlMsgHdr hdr = msg.getHeader(); 83 assertNotNull(hdr); 84 85 if (hdr.nlmsg_type == NetlinkConstants.NLMSG_DONE) { 86 doneMessageCount++; 87 continue; 88 } 89 90 assertEquals(NetlinkConstants.RTM_NEWNEIGH, hdr.nlmsg_type); 91 assertTrue(msg instanceof RtNetlinkNeighborMessage); 92 assertTrue((hdr.nlmsg_flags & StructNlMsgHdr.NLM_F_MULTI) != 0); 93 assertEquals(TEST_SEQNO, hdr.nlmsg_seq); 94 assertEquals(localAddr.getPortId(), hdr.nlmsg_pid); 95 96 neighMessageCount++; 97 } 98 } 99 100 assertEquals(1, doneMessageCount); 101 // TODO: make sure this test passes sanely in airplane mode. 102 assertTrue(neighMessageCount > 0); 103 104 IoUtils.closeQuietly(fd); 105 } 106 } 107