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