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