blob: 92ffc5c6ec3335d16b4ec6b9ac913e70931f84dd [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
20import android.os.Build
21import androidx.test.filters.SmallTest
22import androidx.test.runner.AndroidJUnit4
23import com.android.testutils.DevSdkIgnoreRule
24import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
25import java.net.InetAddress
26import java.util.Arrays
27import org.junit.Assert.assertEquals
28import org.junit.Assert.assertTrue
29import org.junit.Assert.fail
30import org.junit.Rule
31import org.junit.Test
32import org.junit.runner.RunWith
33
34@RunWith(AndroidJUnit4::class)
35@SmallTest
36class KeepalivePacketDataTest {
37 @Rule @JvmField
38 val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule()
39
40 private val INVALID_PORT = 65537
41 private val TEST_DST_PORT = 4244
42 private val TEST_SRC_PORT = 4243
43
44 private val TESTBYTES = byteArrayOf(12, 31, 22, 44)
45 private val TEST_SRC_ADDRV4 = "198.168.0.2".address()
46 private val TEST_DST_ADDRV4 = "198.168.0.1".address()
47 private val TEST_ADDRV6 = "2001:db8::1".address()
48
49 private fun String.address() = InetAddresses.parseNumericAddress(this)
50
51 // Add for test because constructor of KeepalivePacketData is protected.
52 private inner class TestKeepalivePacketData(
Colin Cross4e5a0612023-08-09 10:39:26 -070053 srcAddress: InetAddress = TEST_SRC_ADDRV4,
Chiachang Wang13c97eb2020-03-12 14:45:47 +080054 srcPort: Int = TEST_SRC_PORT,
Colin Cross4e5a0612023-08-09 10:39:26 -070055 dstAddress: InetAddress = TEST_DST_ADDRV4,
Chiachang Wang13c97eb2020-03-12 14:45:47 +080056 dstPort: Int = TEST_DST_PORT,
57 data: ByteArray = TESTBYTES
58 ) : KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, data)
59
60 @Test
61 @IgnoreUpTo(Build.VERSION_CODES.Q)
62 fun testConstructor() {
63 var data: TestKeepalivePacketData
64
65 try {
Chiachang Wang13c97eb2020-03-12 14:45:47 +080066 data = TestKeepalivePacketData(dstAddress = TEST_ADDRV6)
67 fail("Ip family mismatched should cause exception")
68 } catch (e: InvalidPacketException) {
69 assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
70 }
71
72 try {
73 data = TestKeepalivePacketData(srcPort = INVALID_PORT)
74 fail("Invalid srcPort should cause exception")
75 } catch (e: InvalidPacketException) {
76 assertEquals(e.error, ERROR_INVALID_PORT)
77 }
78
79 try {
80 data = TestKeepalivePacketData(dstPort = INVALID_PORT)
81 fail("Invalid dstPort should cause exception")
82 } catch (e: InvalidPacketException) {
83 assertEquals(e.error, ERROR_INVALID_PORT)
84 }
85 }
86
87 @Test
88 @IgnoreUpTo(Build.VERSION_CODES.Q)
89 fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress)
90
91 @Test
92 @IgnoreUpTo(Build.VERSION_CODES.Q)
93 fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress)
94
95 @Test
96 @IgnoreUpTo(Build.VERSION_CODES.Q)
97 fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort)
98
99 @Test
100 @IgnoreUpTo(Build.VERSION_CODES.Q)
101 fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort)
102
103 @Test
104 @IgnoreUpTo(Build.VERSION_CODES.Q)
105 fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet))
Colin Cross4e5a0612023-08-09 10:39:26 -0700106}