blob: 36b9a91ffaf8d77a2796069d5b8241023fab9ffe [file] [log] [blame]
Chiachang Wang5b710612020-03-13 16:37:04 +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 */
16
17package android.net
18
19import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS
20import android.net.InvalidPacketException.ERROR_INVALID_PORT
21import android.net.NattSocketKeepalive.NATT_PORT
22import android.os.Build
23import androidx.test.filters.SmallTest
24import androidx.test.runner.AndroidJUnit4
Chiachang Wang5b710612020-03-13 16:37:04 +080025import com.android.testutils.DevSdkIgnoreRule
26import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
chiachangwang8af04872023-06-07 06:44:51 +000027import com.android.testutils.assertEqualBothWays
Remi NGUYEN VAN8bc36962022-01-18 12:36:25 +090028import com.android.testutils.assertParcelingIsLossless
Chiachang Wang5b710612020-03-13 16:37:04 +080029import com.android.testutils.parcelingRoundTrip
30import java.net.InetAddress
chiachangwang60179ec2023-06-09 06:08:39 +000031import kotlin.test.assertFailsWith
Chiachang Wang5b710612020-03-13 16:37:04 +080032import org.junit.Assert.assertEquals
33import org.junit.Assert.assertNotEquals
Chiachang Wang5b710612020-03-13 16:37:04 +080034import org.junit.Rule
35import org.junit.Test
36import org.junit.runner.RunWith
37
38@RunWith(AndroidJUnit4::class)
39@SmallTest
40class NattKeepalivePacketDataTest {
41 @Rule @JvmField
42 val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule()
43
Chiachang Wang5b710612020-03-13 16:37:04 +080044 private val TEST_PORT = 4243
45 private val TEST_PORT2 = 4244
46 private val TEST_SRC_ADDRV4 = "198.168.0.2".address()
47 private val TEST_DST_ADDRV4 = "198.168.0.1".address()
48 private val TEST_ADDRV6 = "2001:db8::1".address()
49
50 private fun String.address() = InetAddresses.parseNumericAddress(this)
51 private fun nattKeepalivePacket(
52 srcAddress: InetAddress? = TEST_SRC_ADDRV4,
53 srcPort: Int = TEST_PORT,
54 dstAddress: InetAddress? = TEST_DST_ADDRV4,
55 dstPort: Int = NATT_PORT
56 ) = NattKeepalivePacketData.nattKeepalivePacket(srcAddress, srcPort, dstAddress, dstPort)
57
58 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
59 fun testConstructor() {
chiachangwang60179ec2023-06-09 06:08:39 +000060 assertFailsWith<InvalidPacketException>(
61 "Dst port is not NATT port should cause exception") {
Chiachang Wang5b710612020-03-13 16:37:04 +080062 nattKeepalivePacket(dstPort = TEST_PORT)
chiachangwang60179ec2023-06-09 06:08:39 +000063 }.let {
64 assertEquals(it.error, ERROR_INVALID_PORT)
Chiachang Wang5b710612020-03-13 16:37:04 +080065 }
66
chiachangwang60179ec2023-06-09 06:08:39 +000067 assertFailsWith<InvalidPacketException>("A v6 srcAddress should cause exception") {
Chiachang Wang5b710612020-03-13 16:37:04 +080068 nattKeepalivePacket(srcAddress = TEST_ADDRV6)
chiachangwang60179ec2023-06-09 06:08:39 +000069 }.let {
70 assertEquals(it.error, ERROR_INVALID_IP_ADDRESS)
Chiachang Wang5b710612020-03-13 16:37:04 +080071 }
72
chiachangwang60179ec2023-06-09 06:08:39 +000073 assertFailsWith<InvalidPacketException>("A v6 dstAddress should cause exception") {
Chiachang Wang5b710612020-03-13 16:37:04 +080074 nattKeepalivePacket(dstAddress = TEST_ADDRV6)
chiachangwang60179ec2023-06-09 06:08:39 +000075 }.let {
76 assertEquals(it.error, ERROR_INVALID_IP_ADDRESS)
Chiachang Wang5b710612020-03-13 16:37:04 +080077 }
78
chiachangwang60179ec2023-06-09 06:08:39 +000079 assertFailsWith<IllegalArgumentException>("Invalid data should cause exception") {
Chiachang Wang5b710612020-03-13 16:37:04 +080080 parcelingRoundTrip(
chiachangwang60179ec2023-06-09 06:08:39 +000081 NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT,
Chiachang Wang5b710612020-03-13 16:37:04 +080082 byteArrayOf(12, 31, 22, 44)))
chiachangwang60179ec2023-06-09 06:08:39 +000083 }
Chiachang Wang5b710612020-03-13 16:37:04 +080084 }
85
86 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
87 fun testParcel() {
Remi NGUYEN VAN8bc36962022-01-18 12:36:25 +090088 assertParcelingIsLossless(nattKeepalivePacket())
Chiachang Wang5b710612020-03-13 16:37:04 +080089 }
90
91 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
92 fun testEquals() {
93 assertEqualBothWays(nattKeepalivePacket(), nattKeepalivePacket())
94 assertNotEquals(nattKeepalivePacket(dstAddress = TEST_SRC_ADDRV4), nattKeepalivePacket())
95 assertNotEquals(nattKeepalivePacket(srcAddress = TEST_DST_ADDRV4), nattKeepalivePacket())
96 // Test src port only because dst port have to be NATT_PORT
97 assertNotEquals(nattKeepalivePacket(srcPort = TEST_PORT2), nattKeepalivePacket())
Chiachang Wang5b710612020-03-13 16:37:04 +080098 }
99
100 @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
101 fun testHashCode() {
102 assertEquals(nattKeepalivePacket().hashCode(), nattKeepalivePacket().hashCode())
103 }
chiachangwang8af04872023-06-07 06:44:51 +0000104}