Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 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 | package android.net |
| 17 | |
| 18 | import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS |
| 19 | import android.net.InvalidPacketException.ERROR_INVALID_PORT |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 20 | import androidx.test.filters.SmallTest |
| 21 | import androidx.test.runner.AndroidJUnit4 |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 22 | import com.android.testutils.NonNullTestUtils |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 23 | import java.net.InetAddress |
| 24 | import java.util.Arrays |
| 25 | import org.junit.Assert.assertEquals |
| 26 | import org.junit.Assert.assertTrue |
| 27 | import org.junit.Assert.fail |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 28 | import org.junit.Test |
| 29 | import org.junit.runner.RunWith |
| 30 | |
| 31 | @RunWith(AndroidJUnit4::class) |
| 32 | @SmallTest |
| 33 | class KeepalivePacketDataTest { |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 34 | private val INVALID_PORT = 65537 |
| 35 | private val TEST_DST_PORT = 4244 |
| 36 | private val TEST_SRC_PORT = 4243 |
| 37 | |
| 38 | private val TESTBYTES = byteArrayOf(12, 31, 22, 44) |
| 39 | private val TEST_SRC_ADDRV4 = "198.168.0.2".address() |
| 40 | private val TEST_DST_ADDRV4 = "198.168.0.1".address() |
| 41 | private val TEST_ADDRV6 = "2001:db8::1".address() |
| 42 | |
| 43 | private fun String.address() = InetAddresses.parseNumericAddress(this) |
| 44 | |
| 45 | // Add for test because constructor of KeepalivePacketData is protected. |
| 46 | private inner class TestKeepalivePacketData( |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 47 | srcAddress: InetAddress? = TEST_SRC_ADDRV4, |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 48 | srcPort: Int = TEST_SRC_PORT, |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 49 | dstAddress: InetAddress? = TEST_DST_ADDRV4, |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 50 | dstPort: Int = TEST_DST_PORT, |
| 51 | data: ByteArray = TESTBYTES |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 52 | ) : KeepalivePacketData(NonNullTestUtils.nullUnsafe(srcAddress), srcPort, |
| 53 | NonNullTestUtils.nullUnsafe(dstAddress), dstPort, data) |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 54 | |
| 55 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 56 | fun testConstructor() { |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 57 | try { |
| 58 | TestKeepalivePacketData(srcAddress = null) |
| 59 | fail("Null src address should cause exception") |
| 60 | } catch (e: InvalidPacketException) { |
| 61 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 62 | } |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 63 | |
| 64 | try { |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 65 | TestKeepalivePacketData(dstAddress = null) |
| 66 | fail("Null dst address should cause exception") |
| 67 | } catch (e: InvalidPacketException) { |
| 68 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 69 | } |
| 70 | |
| 71 | try { |
| 72 | TestKeepalivePacketData(dstAddress = TEST_ADDRV6) |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 73 | fail("Ip family mismatched should cause exception") |
| 74 | } catch (e: InvalidPacketException) { |
| 75 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 76 | } |
| 77 | |
| 78 | try { |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 79 | TestKeepalivePacketData(srcPort = INVALID_PORT) |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 80 | fail("Invalid srcPort should cause exception") |
| 81 | } catch (e: InvalidPacketException) { |
| 82 | assertEquals(e.error, ERROR_INVALID_PORT) |
| 83 | } |
| 84 | |
| 85 | try { |
Motomu Utsumi | 5324475 | 2023-08-22 18:28:14 +0900 | [diff] [blame] | 86 | TestKeepalivePacketData(dstPort = INVALID_PORT) |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 87 | fail("Invalid dstPort should cause exception") |
| 88 | } catch (e: InvalidPacketException) { |
| 89 | assertEquals(e.error, ERROR_INVALID_PORT) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 94 | fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress) |
| 95 | |
| 96 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 97 | fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress) |
| 98 | |
| 99 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 100 | fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort) |
| 101 | |
| 102 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 103 | fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort) |
| 104 | |
| 105 | @Test |
Chiachang Wang | 13c97eb | 2020-03-12 14:45:47 +0800 | [diff] [blame] | 106 | fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet)) |
Colin Cross | 4e5a061 | 2023-08-09 10:39:26 -0700 | [diff] [blame] | 107 | } |