1 /* 2 * Copyright (C) 2019 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.util 18 19 import android.system.NetlinkSocketAddress 20 import android.system.Os 21 import android.system.OsConstants.AF_INET 22 import android.system.OsConstants.ETH_P_ALL 23 import android.system.OsConstants.IPPROTO_UDP 24 import android.system.OsConstants.RTMGRP_NEIGH 25 import android.system.OsConstants.SOCK_DGRAM 26 import android.system.PacketSocketAddress 27 import androidx.test.filters.SmallTest 28 import androidx.test.runner.AndroidJUnit4 29 import org.junit.Assert.assertEquals 30 import org.junit.Assert.assertFalse 31 import org.junit.Assert.assertTrue 32 import org.junit.Assert.fail 33 import org.junit.Test 34 import org.junit.runner.RunWith 35 36 private const val TEST_INDEX = 123 37 private const val TEST_PORT = 555 38 private const val FF_BYTE = 0xff.toByte() 39 40 @RunWith(AndroidJUnit4::class) 41 @SmallTest 42 class SocketUtilsTest { 43 @Test testMakeNetlinkSocketAddressnull44 fun testMakeNetlinkSocketAddress() { 45 val nlAddress = SocketUtils.makeNetlinkSocketAddress(TEST_PORT, RTMGRP_NEIGH) 46 if (nlAddress is NetlinkSocketAddress) { 47 assertEquals(TEST_PORT, nlAddress.getPortId()) 48 assertEquals(RTMGRP_NEIGH, nlAddress.getGroupsMask()) 49 } else { 50 fail("Not NetlinkSocketAddress object") 51 } 52 } 53 54 @Test testMakePacketSocketAddress_Qnull55 fun testMakePacketSocketAddress_Q() { 56 val pkAddress = SocketUtils.makePacketSocketAddress(ETH_P_ALL, TEST_INDEX) 57 assertTrue("Not PacketSocketAddress object", pkAddress is PacketSocketAddress) 58 59 val pkAddress2 = SocketUtils.makePacketSocketAddress(TEST_INDEX, ByteArray(6) { FF_BYTE }) 60 assertTrue("Not PacketSocketAddress object", pkAddress2 is PacketSocketAddress) 61 } 62 63 @Test testMakePacketSocketAddressnull64 fun testMakePacketSocketAddress() { 65 val pkAddress = SocketUtils.makePacketSocketAddress( 66 ETH_P_ALL, TEST_INDEX, ByteArray(6) { FF_BYTE }) 67 assertTrue("Not PacketSocketAddress object", pkAddress is PacketSocketAddress) 68 } 69 70 @Test testCloseSocketnull71 fun testCloseSocket() { 72 // Expect no exception happening with null object. 73 SocketUtils.closeSocket(null) 74 75 val fd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP) 76 assertTrue(fd.valid()) 77 SocketUtils.closeSocket(fd) 78 assertFalse(fd.valid()) 79 // Expecting socket should be still invalid even closed socket again. 80 SocketUtils.closeSocket(fd) 81 assertFalse(fd.valid()) 82 } 83 } 84