Merge "Use assertFailsWith to verify exception cases" am: f9e014317e

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2618635

Change-Id: I9089fb6515fc47919efbe746b7d8fca5a517152e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2023-06-09 11:22:08 +00:00
committed by Automerger Merge Worker

View File

@@ -28,9 +28,9 @@ import com.android.testutils.assertEqualBothWays
import com.android.testutils.assertParcelingIsLossless import com.android.testutils.assertParcelingIsLossless
import com.android.testutils.parcelingRoundTrip import com.android.testutils.parcelingRoundTrip
import java.net.InetAddress import java.net.InetAddress
import kotlin.test.assertFailsWith
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNotEquals
import org.junit.Assert.fail
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
@@ -57,33 +57,30 @@ class NattKeepalivePacketDataTest {
@Test @IgnoreUpTo(Build.VERSION_CODES.Q) @Test @IgnoreUpTo(Build.VERSION_CODES.Q)
fun testConstructor() { fun testConstructor() {
try { assertFailsWith<InvalidPacketException>(
"Dst port is not NATT port should cause exception") {
nattKeepalivePacket(dstPort = TEST_PORT) nattKeepalivePacket(dstPort = TEST_PORT)
fail("Dst port is not NATT port should cause exception") }.let {
} catch (e: InvalidPacketException) { assertEquals(it.error, ERROR_INVALID_PORT)
assertEquals(e.error, ERROR_INVALID_PORT)
} }
try { assertFailsWith<InvalidPacketException>("A v6 srcAddress should cause exception") {
nattKeepalivePacket(srcAddress = TEST_ADDRV6) nattKeepalivePacket(srcAddress = TEST_ADDRV6)
fail("A v6 srcAddress should cause exception") }.let {
} catch (e: InvalidPacketException) { assertEquals(it.error, ERROR_INVALID_IP_ADDRESS)
assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
} }
try { assertFailsWith<InvalidPacketException>("A v6 dstAddress should cause exception") {
nattKeepalivePacket(dstAddress = TEST_ADDRV6) nattKeepalivePacket(dstAddress = TEST_ADDRV6)
fail("A v6 dstAddress should cause exception") }.let {
} catch (e: InvalidPacketException) { assertEquals(it.error, ERROR_INVALID_IP_ADDRESS)
assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
} }
try { assertFailsWith<IllegalArgumentException>("Invalid data should cause exception") {
parcelingRoundTrip( parcelingRoundTrip(
NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT, NattKeepalivePacketData(TEST_SRC_ADDRV4, TEST_PORT, TEST_DST_ADDRV4, TEST_PORT,
byteArrayOf(12, 31, 22, 44))) byteArrayOf(12, 31, 22, 44)))
fail("Invalid data should cause exception") }
} catch (e: IllegalArgumentException) { }
} }
@Test @IgnoreUpTo(Build.VERSION_CODES.Q) @Test @IgnoreUpTo(Build.VERSION_CODES.Q)