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 |
| 25 | import com.android.testutils.assertEqualBothWays |
| 26 | import com.android.testutils.assertFieldCountEquals |
| 27 | import com.android.testutils.assertParcelSane |
| 28 | import com.android.testutils.DevSdkIgnoreRule |
| 29 | import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo |
| 30 | import com.android.testutils.parcelingRoundTrip |
| 31 | import java.net.InetAddress |
| 32 | import org.junit.Assert.assertEquals |
| 33 | import org.junit.Assert.assertNotEquals |
| 34 | import org.junit.Assert.fail |
| 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 | |
| 45 | /* Refer to the definition in {@code NattKeepalivePacketData} */ |
| 46 | private val IPV4_HEADER_LENGTH = 20 |
| 47 | private val UDP_HEADER_LENGTH = 8 |
| 48 | |
| 49 | private val TEST_PORT = 4243 |
| 50 | private val TEST_PORT2 = 4244 |
| 51 | private val TEST_SRC_ADDRV4 = "198.168.0.2".address() |
| 52 | private val TEST_DST_ADDRV4 = "198.168.0.1".address() |
| 53 | private val TEST_ADDRV6 = "2001:db8::1".address() |
| 54 | |
| 55 | private fun String.address() = InetAddresses.parseNumericAddress(this) |
| 56 | private fun nattKeepalivePacket( |
| 57 | srcAddress: InetAddress? = TEST_SRC_ADDRV4, |
| 58 | srcPort: Int = TEST_PORT, |
| 59 | dstAddress: InetAddress? = TEST_DST_ADDRV4, |
| 60 | dstPort: Int = NATT_PORT |
| 61 | ) = NattKeepalivePacketData.nattKeepalivePacket(srcAddress, srcPort, dstAddress, dstPort) |
| 62 | |
| 63 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 64 | fun testConstructor() { |
| 65 | try { |
| 66 | nattKeepalivePacket(dstPort = TEST_PORT) |
| 67 | fail("Dst port is not NATT port should cause exception") |
| 68 | } catch (e: InvalidPacketException) { |
| 69 | assertEquals(e.error, ERROR_INVALID_PORT) |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | nattKeepalivePacket(srcAddress = TEST_ADDRV6) |
| 74 | fail("A v6 srcAddress should cause exception") |
| 75 | } catch (e: InvalidPacketException) { |
| 76 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | nattKeepalivePacket(dstAddress = TEST_ADDRV6) |
| 81 | fail("A v6 dstAddress should cause exception") |
| 82 | } catch (e: InvalidPacketException) { |
| 83 | assertEquals(e.error, ERROR_INVALID_IP_ADDRESS) |
| 84 | } |
| 85 | |
| 86 | try { |
| 87 | parcelingRoundTrip( |
| 88 | NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT, |
| 89 | byteArrayOf(12, 31, 22, 44))) |
| 90 | fail("Invalid data should cause exception") |
| 91 | } catch (e: IllegalArgumentException) { } |
| 92 | } |
| 93 | |
| 94 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 95 | fun testParcel() { |
| 96 | assertParcelSane(nattKeepalivePacket(), 0) |
| 97 | } |
| 98 | |
| 99 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 100 | fun testEquals() { |
| 101 | assertEqualBothWays(nattKeepalivePacket(), nattKeepalivePacket()) |
| 102 | assertNotEquals(nattKeepalivePacket(dstAddress = TEST_SRC_ADDRV4), nattKeepalivePacket()) |
| 103 | assertNotEquals(nattKeepalivePacket(srcAddress = TEST_DST_ADDRV4), nattKeepalivePacket()) |
| 104 | // Test src port only because dst port have to be NATT_PORT |
| 105 | assertNotEquals(nattKeepalivePacket(srcPort = TEST_PORT2), nattKeepalivePacket()) |
| 106 | // Make sure the parceling test is updated if fields are added in the base class. |
| 107 | assertFieldCountEquals(5, KeepalivePacketData::class.java) |
| 108 | } |
| 109 | |
| 110 | @Test @IgnoreUpTo(Build.VERSION_CODES.Q) |
| 111 | fun testHashCode() { |
| 112 | assertEquals(nattKeepalivePacket().hashCode(), nattKeepalivePacket().hashCode()) |
| 113 | } |
| 114 | } |