Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 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 | |
| 17 | package android.net |
| 18 | |
| 19 | import android.net.InvalidPacketException.ERROR_INVALID_IP_ADDRESS |
| 20 | import android.net.InvalidPacketException.ERROR_INVALID_PORT |
| 21 | import android.net.NattSocketKeepalive.NATT_PORT |
| 22 | import android.os.Build |
| 23 | import androidx.test.filters.SmallTest |
| 24 | import androidx.test.runner.AndroidJUnit4 |
chiachangwang | e91f8dc | 2023-06-15 01:56:47 +0000 | [diff] [blame^] | 25 | import com.android.testutils.ConnectivityModuleTest |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 26 | import com.android.testutils.DevSdkIgnoreRule |
| 27 | import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo |
chiachangwang | 8af0487 | 2023-06-07 06:44:51 +0000 | [diff] [blame] | 28 | import com.android.testutils.assertEqualBothWays |
Remi NGUYEN VAN | 8bc3696 | 2022-01-18 12:36:25 +0900 | [diff] [blame] | 29 | import com.android.testutils.assertParcelingIsLossless |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 30 | import com.android.testutils.parcelingRoundTrip |
| 31 | import java.net.InetAddress |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 32 | import kotlin.test.assertFailsWith |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 33 | import org.junit.Assert.assertEquals |
| 34 | import org.junit.Assert.assertNotEquals |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 35 | import org.junit.Rule |
| 36 | import org.junit.Test |
| 37 | import org.junit.runner.RunWith |
| 38 | |
| 39 | @RunWith(AndroidJUnit4::class) |
| 40 | @SmallTest |
| 41 | class NattKeepalivePacketDataTest { |
| 42 | @Rule @JvmField |
| 43 | val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() |
| 44 | |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 45 | private val TEST_PORT = 4243 |
| 46 | private val TEST_PORT2 = 4244 |
| 47 | private val TEST_SRC_ADDRV4 = "198.168.0.2".address() |
| 48 | private val TEST_DST_ADDRV4 = "198.168.0.1".address() |
| 49 | private val TEST_ADDRV6 = "2001:db8::1".address() |
chiachangwang | c18f0bb | 2023-06-09 06:09:44 +0000 | [diff] [blame] | 50 | private val TEST_ADDRV4MAPPEDV6 = "::ffff:1.2.3.4".address() |
| 51 | private val TEST_ADDRV4 = "1.2.3.4".address() |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 52 | |
| 53 | private fun String.address() = InetAddresses.parseNumericAddress(this) |
| 54 | private fun nattKeepalivePacket( |
| 55 | srcAddress: InetAddress? = TEST_SRC_ADDRV4, |
| 56 | srcPort: Int = TEST_PORT, |
| 57 | dstAddress: InetAddress? = TEST_DST_ADDRV4, |
| 58 | dstPort: Int = NATT_PORT |
| 59 | ) = NattKeepalivePacketData.nattKeepalivePacket(srcAddress, srcPort, dstAddress, dstPort) |
| 60 | |
| 61 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 62 | fun testConstructor() { |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 63 | assertFailsWith<InvalidPacketException>( |
| 64 | "Dst port is not NATT port should cause exception") { |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 65 | nattKeepalivePacket(dstPort = TEST_PORT) |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 66 | }.let { |
| 67 | assertEquals(it.error, ERROR_INVALID_PORT) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 68 | } |
| 69 | |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 70 | assertFailsWith<InvalidPacketException>("A v6 srcAddress should cause exception") { |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 71 | nattKeepalivePacket(srcAddress = TEST_ADDRV6) |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 72 | }.let { |
| 73 | assertEquals(it.error, ERROR_INVALID_IP_ADDRESS) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 74 | } |
| 75 | |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 76 | assertFailsWith<InvalidPacketException>("A v6 dstAddress should cause exception") { |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 77 | nattKeepalivePacket(dstAddress = TEST_ADDRV6) |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 78 | }.let { |
| 79 | assertEquals(it.error, ERROR_INVALID_IP_ADDRESS) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 80 | } |
| 81 | |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 82 | assertFailsWith<IllegalArgumentException>("Invalid data should cause exception") { |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 83 | parcelingRoundTrip( |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 84 | NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT, |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 85 | byteArrayOf(12, 31, 22, 44))) |
chiachangwang | 60179ec | 2023-06-09 06:08:39 +0000 | [diff] [blame] | 86 | } |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 87 | } |
| 88 | |
chiachangwang | e91f8dc | 2023-06-15 01:56:47 +0000 | [diff] [blame^] | 89 | @Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest |
chiachangwang | c18f0bb | 2023-06-09 06:09:44 +0000 | [diff] [blame] | 90 | fun testConstructor_afterR() { |
| 91 | // v4 mapped v6 will be translated to a v4 address. |
| 92 | assertFailsWith<InvalidPacketException> { |
| 93 | nattKeepalivePacket(srcAddress = TEST_ADDRV6, dstAddress = TEST_ADDRV4MAPPEDV6) |
| 94 | } |
| 95 | assertFailsWith<InvalidPacketException> { |
| 96 | nattKeepalivePacket(srcAddress = TEST_ADDRV4MAPPEDV6, dstAddress = TEST_ADDRV6) |
| 97 | } |
| 98 | |
| 99 | // Both src and dst address will be v4 after translation, so it won't cause exception. |
| 100 | val packet1 = nattKeepalivePacket( |
| 101 | dstAddress = TEST_ADDRV4MAPPEDV6, srcAddress = TEST_ADDRV4MAPPEDV6) |
| 102 | assertEquals(TEST_ADDRV4, packet1.srcAddress) |
| 103 | assertEquals(TEST_ADDRV4, packet1.dstAddress) |
| 104 | |
| 105 | // Packet with v6 src and v6 dst address is valid. |
| 106 | val packet2 = nattKeepalivePacket(srcAddress = TEST_ADDRV6, dstAddress = TEST_ADDRV6) |
| 107 | assertEquals(TEST_ADDRV6, packet2.srcAddress) |
| 108 | assertEquals(TEST_ADDRV6, packet2.dstAddress) |
| 109 | } |
| 110 | |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 111 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 112 | fun testParcel() { |
Remi NGUYEN VAN | 8bc3696 | 2022-01-18 12:36:25 +0900 | [diff] [blame] | 113 | assertParcelingIsLossless(nattKeepalivePacket()) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 117 | fun testEquals() { |
| 118 | assertEqualBothWays(nattKeepalivePacket(), nattKeepalivePacket()) |
| 119 | assertNotEquals(nattKeepalivePacket(dstAddress = TEST_SRC_ADDRV4), nattKeepalivePacket()) |
| 120 | assertNotEquals(nattKeepalivePacket(srcAddress = TEST_DST_ADDRV4), nattKeepalivePacket()) |
| 121 | // Test src port only because dst port have to be NATT_PORT |
| 122 | assertNotEquals(nattKeepalivePacket(srcPort = TEST_PORT2), nattKeepalivePacket()) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 126 | fun testHashCode() { |
| 127 | assertEquals(nattKeepalivePacket().hashCode(), nattKeepalivePacket().hashCode()) |
| 128 | } |
chiachangwang | 8af0487 | 2023-06-07 06:44:51 +0000 | [diff] [blame] | 129 | } |