Create v4 mapped v6 correctly

Tests verifies the behavior with v4 mapped v6. The
taken parameter should be a v6 address. However,
InetAddresses.parseNumericAddress() will translate a v4
mapped v6 address into v4. This does not actually test
the behavior. The constant should be created via
Inet6Address.getByAddress() so that it will be an
expected Inet6Address.

Bug: 291869581
Test: atest NattKeepalivePacketDataTest
      w/ and w/o aosp/2626269
Change-Id: I2f086f0fe7d1dd038f5d850f2281deb807c837cf
This commit is contained in:
chiachangwang
2023-07-19 11:34:01 +00:00
parent 0b5fb22b1b
commit 65240048ce

View File

@@ -28,6 +28,7 @@ import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.assertEqualBothWays
import com.android.testutils.assertParcelingIsLossless
import com.android.testutils.parcelingRoundTrip
import java.net.Inet6Address
import java.net.InetAddress
import kotlin.test.assertFailsWith
import org.junit.Assert.assertEquals
@@ -44,10 +45,33 @@ class NattKeepalivePacketDataTest {
private val TEST_PORT = 4243
private val TEST_PORT2 = 4244
// ::FFFF:1.2.3.4
private val SRC_V4_MAPPED_V6_ADDRESS_BYTES = byteArrayOf(
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0x00.toByte(),
0xff.toByte(),
0xff.toByte(),
0x01.toByte(),
0x02.toByte(),
0x03.toByte(),
0x04.toByte()
)
private val TEST_SRC_ADDRV4 = "198.168.0.2".address()
private val TEST_DST_ADDRV4 = "198.168.0.1".address()
private val TEST_ADDRV6 = "2001:db8::1".address()
private val TEST_ADDRV4MAPPEDV6 = "::ffff:1.2.3.4".address()
// This constant requires to be an Inet6Address, but InetAddresses.parseNumericAddress() will
// convert v4 mapped v6 address into an Inet4Address. So use Inet6Address.getByAddress() to
// create the address.
private val TEST_ADDRV4MAPPEDV6 = Inet6Address.getByAddress(null /* host */,
SRC_V4_MAPPED_V6_ADDRESS_BYTES, -1 /* scope_id */)
private val TEST_ADDRV4 = "1.2.3.4".address()
private fun String.address() = InetAddresses.parseNumericAddress(this)