blob: f464ec6cf0e549ffe0c980c8b396f86d696a7df1 [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(
53 srcAddress: InetAddress? = TEST_SRC_ADDRV4,
54 srcPort: Int = TEST_SRC_PORT,
55 dstAddress: InetAddress? = TEST_DST_ADDRV4,
56 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 {
66 data = TestKeepalivePacketData(srcAddress = null)
67 fail("Null src address should cause exception")
68 } catch (e: InvalidPacketException) {
69 assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
70 }
71
72 try {
73 data = TestKeepalivePacketData(dstAddress = null)
74 fail("Null dst address should cause exception")
75 } catch (e: InvalidPacketException) {
76 assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
77 }
78
79 try {
80 data = TestKeepalivePacketData(dstAddress = TEST_ADDRV6)
81 fail("Ip family mismatched should cause exception")
82 } catch (e: InvalidPacketException) {
83 assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
84 }
85
86 try {
87 data = TestKeepalivePacketData(srcPort = INVALID_PORT)
88 fail("Invalid srcPort should cause exception")
89 } catch (e: InvalidPacketException) {
90 assertEquals(e.error, ERROR_INVALID_PORT)
91 }
92
93 try {
94 data = TestKeepalivePacketData(dstPort = INVALID_PORT)
95 fail("Invalid dstPort should cause exception")
96 } catch (e: InvalidPacketException) {
97 assertEquals(e.error, ERROR_INVALID_PORT)
98 }
99 }
100
101 @Test
102 @IgnoreUpTo(Build.VERSION_CODES.Q)
103 fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress)
104
105 @Test
106 @IgnoreUpTo(Build.VERSION_CODES.Q)
107 fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress)
108
109 @Test
110 @IgnoreUpTo(Build.VERSION_CODES.Q)
111 fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort)
112
113 @Test
114 @IgnoreUpTo(Build.VERSION_CODES.Q)
115 fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort)
116
117 @Test
118 @IgnoreUpTo(Build.VERSION_CODES.Q)
119 fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet))
120}