blob: 97a45fcc4fa5aed6fedbf10225129e14eca16298 [file] [log] [blame]
Chiachang Wang13c97eb2020-03-12 14:45:47 +08001/*
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 */
16package android.net
17
18import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS
19import android.net.InvalidPacketException.ERROR_INVALID_PORT
Chiachang Wang13c97eb2020-03-12 14:45:47 +080020import androidx.test.filters.SmallTest
21import androidx.test.runner.AndroidJUnit4
Motomu Utsumi53244752023-08-22 18:28:14 +090022import com.android.testutils.NonNullTestUtils
Chiachang Wang13c97eb2020-03-12 14:45:47 +080023import java.net.InetAddress
24import java.util.Arrays
25import org.junit.Assert.assertEquals
26import org.junit.Assert.assertTrue
27import org.junit.Assert.fail
Chiachang Wang13c97eb2020-03-12 14:45:47 +080028import org.junit.Test
29import org.junit.runner.RunWith
30
31@RunWith(AndroidJUnit4::class)
32@SmallTest
33class KeepalivePacketDataTest {
Chiachang Wang13c97eb2020-03-12 14:45:47 +080034 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 Utsumi53244752023-08-22 18:28:14 +090047 srcAddress: InetAddress? = TEST_SRC_ADDRV4,
Chiachang Wang13c97eb2020-03-12 14:45:47 +080048 srcPort: Int = TEST_SRC_PORT,
Motomu Utsumi53244752023-08-22 18:28:14 +090049 dstAddress: InetAddress? = TEST_DST_ADDRV4,
Chiachang Wang13c97eb2020-03-12 14:45:47 +080050 dstPort: Int = TEST_DST_PORT,
51 data: ByteArray = TESTBYTES
Motomu Utsumi53244752023-08-22 18:28:14 +090052 ) : KeepalivePacketData(NonNullTestUtils.nullUnsafe(srcAddress), srcPort,
53 NonNullTestUtils.nullUnsafe(dstAddress), dstPort, data)
Chiachang Wang13c97eb2020-03-12 14:45:47 +080054
55 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +080056 fun testConstructor() {
Motomu Utsumi53244752023-08-22 18:28:14 +090057 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 Wang13c97eb2020-03-12 14:45:47 +080063
64 try {
Motomu Utsumi53244752023-08-22 18:28:14 +090065 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 Wang13c97eb2020-03-12 14:45:47 +080073 fail("Ip family mismatched should cause exception")
74 } catch (e: InvalidPacketException) {
75 assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
76 }
77
78 try {
Motomu Utsumi53244752023-08-22 18:28:14 +090079 TestKeepalivePacketData(srcPort = INVALID_PORT)
Chiachang Wang13c97eb2020-03-12 14:45:47 +080080 fail("Invalid srcPort should cause exception")
81 } catch (e: InvalidPacketException) {
82 assertEquals(e.error, ERROR_INVALID_PORT)
83 }
84
85 try {
Motomu Utsumi53244752023-08-22 18:28:14 +090086 TestKeepalivePacketData(dstPort = INVALID_PORT)
Chiachang Wang13c97eb2020-03-12 14:45:47 +080087 fail("Invalid dstPort should cause exception")
88 } catch (e: InvalidPacketException) {
89 assertEquals(e.error, ERROR_INVALID_PORT)
90 }
91 }
92
93 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +080094 fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress)
95
96 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +080097 fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress)
98
99 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +0800100 fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort)
101
102 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +0800103 fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort)
104
105 @Test
Chiachang Wang13c97eb2020-03-12 14:45:47 +0800106 fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet))
Colin Cross4e5a0612023-08-09 10:39:26 -0700107}