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
20 import androidx.test.filters.SmallTest
21 import androidx.test.runner.AndroidJUnit4
22 import com.android.testutils.NonNullTestUtils
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
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @RunWith(AndroidJUnit4::class)
32 @SmallTest
33 class KeepalivePacketDataTest {
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 
addressnull43     private fun String.address() = InetAddresses.parseNumericAddress(this)
44 
45     // Add for test because constructor of KeepalivePacketData is protected.
46     private inner class TestKeepalivePacketData(
47         srcAddress: InetAddress? = TEST_SRC_ADDRV4,
48         srcPort: Int = TEST_SRC_PORT,
49         dstAddress: InetAddress? = TEST_DST_ADDRV4,
50         dstPort: Int = TEST_DST_PORT,
51         data: ByteArray = TESTBYTES
52     ) : KeepalivePacketData(NonNullTestUtils.nullUnsafe(srcAddress), srcPort,
53             NonNullTestUtils.nullUnsafe(dstAddress), dstPort, data)
54 
55     @Test
56     fun testConstructor() {
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         }
63 
64         try {
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)
73             fail("Ip family mismatched should cause exception")
74         } catch (e: InvalidPacketException) {
75             assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
76         }
77 
78         try {
79             TestKeepalivePacketData(srcPort = INVALID_PORT)
80             fail("Invalid srcPort should cause exception")
81         } catch (e: InvalidPacketException) {
82             assertEquals(e.error, ERROR_INVALID_PORT)
83         }
84 
85         try {
86             TestKeepalivePacketData(dstPort = INVALID_PORT)
87             fail("Invalid dstPort should cause exception")
88         } catch (e: InvalidPacketException) {
89             assertEquals(e.error, ERROR_INVALID_PORT)
90         }
91     }
92 
93     @Test
testSrcAddressnull94     fun testSrcAddress() = assertEquals(TEST_SRC_ADDRV4, TestKeepalivePacketData().srcAddress)
95 
96     @Test
97     fun testDstAddress() = assertEquals(TEST_DST_ADDRV4, TestKeepalivePacketData().dstAddress)
98 
99     @Test
100     fun testSrcPort() = assertEquals(TEST_SRC_PORT, TestKeepalivePacketData().srcPort)
101 
102     @Test
103     fun testDstPort() = assertEquals(TEST_DST_PORT, TestKeepalivePacketData().dstPort)
104 
105     @Test
106     fun testPacket() = assertTrue(Arrays.equals(TESTBYTES, TestKeepalivePacketData().packet))
107 }
108