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 |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 25 | import com.android.testutils.DevSdkIgnoreRule |
| 26 | import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo |
chiachangwang | 8af0487 | 2023-06-07 06:44:51 +0000 | [diff] [blame^] | 27 | import com.android.testutils.assertEqualBothWays |
Remi NGUYEN VAN | 8bc3696 | 2022-01-18 12:36:25 +0900 | [diff] [blame] | 28 | import com.android.testutils.assertParcelingIsLossless |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 29 | import com.android.testutils.parcelingRoundTrip |
| 30 | import java.net.InetAddress |
| 31 | import org.junit.Assert.assertEquals |
| 32 | import org.junit.Assert.assertNotEquals |
| 33 | import org.junit.Assert.fail |
| 34 | import org.junit.Rule |
| 35 | import org.junit.Test |
| 36 | import org.junit.runner.RunWith |
| 37 | |
| 38 | @RunWith(AndroidJUnit4::class) |
| 39 | @SmallTest |
| 40 | class NattKeepalivePacketDataTest { |
| 41 | @Rule @JvmField |
| 42 | val ignoreRule: DevSdkIgnoreRule = DevSdkIgnoreRule() |
| 43 | |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 44 | 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() { |
| 60 | try { |
| 61 | nattKeepalivePacket(dstPort = TEST_PORT) |
| 62 | fail("Dst port is not NATT port should cause exception") |
| 63 | } catch (e: InvalidPacketException) { |
| 64 | assertEquals(e.error, ERROR_INVALID_PORT) |
| 65 | } |
| 66 | |
| 67 | try { |
| 68 | nattKeepalivePacket(srcAddress = TEST_ADDRV6) |
| 69 | fail("A v6 srcAddress should cause exception") |
| 70 | } catch (e: InvalidPacketException) { |
| 71 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 72 | } |
| 73 | |
| 74 | try { |
| 75 | nattKeepalivePacket(dstAddress = TEST_ADDRV6) |
| 76 | fail("A v6 dstAddress should cause exception") |
| 77 | } catch (e: InvalidPacketException) { |
| 78 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 79 | } |
| 80 | |
| 81 | try { |
| 82 | parcelingRoundTrip( |
| 83 | NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT, |
| 84 | byteArrayOf(12, 31, 22, 44))) |
| 85 | fail("Invalid data should cause exception") |
| 86 | } catch (e: IllegalArgumentException) { } |
| 87 | } |
| 88 | |
| 89 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 90 | fun testParcel() { |
Remi NGUYEN VAN | 8bc3696 | 2022-01-18 12:36:25 +0900 | [diff] [blame] | 91 | assertParcelingIsLossless(nattKeepalivePacket()) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 95 | fun testEquals() { |
| 96 | assertEqualBothWays(nattKeepalivePacket(), nattKeepalivePacket()) |
| 97 | assertNotEquals(nattKeepalivePacket(dstAddress = TEST_SRC_ADDRV4), nattKeepalivePacket()) |
| 98 | assertNotEquals(nattKeepalivePacket(srcAddress = TEST_DST_ADDRV4), nattKeepalivePacket()) |
| 99 | // Test src port only because dst port have to be NATT_PORT |
| 100 | assertNotEquals(nattKeepalivePacket(srcPort = TEST_PORT2), nattKeepalivePacket()) |
Chiachang Wang | 5b71061 | 2020-03-13 16:37:04 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 104 | fun testHashCode() { |
| 105 | assertEquals(nattKeepalivePacket().hashCode(), nattKeepalivePacket().hashCode()) |
| 106 | } |
chiachangwang | 8af0487 | 2023-06-07 06:44:51 +0000 | [diff] [blame^] | 107 | } |