Merge "Add some tests back which were removed for fixing kotlin nullable errors" into main

This commit is contained in:
Treehugger Robot
2023-08-24 04:14:28 +00:00
committed by Gerrit Code Review
3 changed files with 62 additions and 12 deletions

View File

@@ -22,6 +22,7 @@ import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.NonNullTestUtils
import java.net.InetAddress
import java.util.Arrays
import org.junit.Assert.assertEquals
@@ -50,34 +51,47 @@ class KeepalivePacketDataTest {
// Add for test because constructor of KeepalivePacketData is protected.
private inner class TestKeepalivePacketData(
srcAddress: InetAddress = TEST_SRC_ADDRV4,
srcAddress: InetAddress? = TEST_SRC_ADDRV4,
srcPort: Int = TEST_SRC_PORT,
dstAddress: InetAddress = TEST_DST_ADDRV4,
dstAddress: InetAddress? = TEST_DST_ADDRV4,
dstPort: Int = TEST_DST_PORT,
data: ByteArray = TESTBYTES
) : KeepalivePacketData(srcAddress, srcPort, dstAddress, dstPort, data)
) : KeepalivePacketData(NonNullTestUtils.nullUnsafe(srcAddress), srcPort,
NonNullTestUtils.nullUnsafe(dstAddress), dstPort, data)
@Test
@IgnoreUpTo(Build.VERSION_CODES.Q)
fun testConstructor() {
var data: TestKeepalivePacketData
try {
TestKeepalivePacketData(srcAddress = null)
fail("Null src address should cause exception")
} catch (e: InvalidPacketException) {
assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
}
try {
data = TestKeepalivePacketData(dstAddress = TEST_ADDRV6)
TestKeepalivePacketData(dstAddress = null)
fail("Null dst address should cause exception")
} catch (e: InvalidPacketException) {
assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
}
try {
TestKeepalivePacketData(dstAddress = TEST_ADDRV6)
fail("Ip family mismatched should cause exception")
} catch (e: InvalidPacketException) {
assertEquals(e.error, ERROR_INVALID_IP_ADDRESS)
}
try {
data = TestKeepalivePacketData(srcPort = INVALID_PORT)
TestKeepalivePacketData(srcPort = INVALID_PORT)
fail("Invalid srcPort should cause exception")
} catch (e: InvalidPacketException) {
assertEquals(e.error, ERROR_INVALID_PORT)
}
try {
data = TestKeepalivePacketData(dstPort = INVALID_PORT)
TestKeepalivePacketData(dstPort = INVALID_PORT)
fail("Invalid dstPort should cause exception")
} catch (e: InvalidPacketException) {
assertEquals(e.error, ERROR_INVALID_PORT)

View File

@@ -16,12 +16,12 @@
package android.net.cts
import android.os.Build
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.net.NetworkInfo.DetailedState
import android.net.NetworkInfo.State
import android.os.Build
import android.telephony.TelephonyManager
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
@@ -29,16 +29,17 @@ import androidx.test.runner.AndroidJUnit4
import com.android.modules.utils.build.SdkLevel
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.NonNullTestUtils
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertFails
import kotlin.test.assertFailsWith
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.runner.RunWith
import org.junit.Test
import kotlin.reflect.jvm.isAccessible
import kotlin.test.assertFails
import kotlin.test.assertFailsWith
import org.junit.runner.RunWith
const val TYPE_MOBILE = ConnectivityManager.TYPE_MOBILE
const val TYPE_WIFI = ConnectivityManager.TYPE_WIFI
@@ -104,6 +105,15 @@ class NetworkInfoTest {
NetworkInfo(ConnectivityManager.MAX_NETWORK_TYPE + 1,
TelephonyManager.NETWORK_TYPE_LTE, MOBILE_TYPE_NAME, LTE_SUBTYPE_NAME)
}
if (SdkLevel.isAtLeastT()) {
assertFailsWith<NullPointerException> {
NetworkInfo(NonNullTestUtils.nullUnsafe<NetworkInfo>(null))
}
} else {
// Doesn't immediately crash on S-
NetworkInfo(NonNullTestUtils.nullUnsafe<NetworkInfo>(null))
}
}
@Test
@@ -126,11 +136,23 @@ class NetworkInfoTest {
constructor.isAccessible = true
val incorrectDetailedState = constructor.newInstance("any", 200) as DetailedState
if (SdkLevel.isAtLeastT()) {
assertFailsWith<NullPointerException> {
NetworkInfo(NonNullTestUtils.nullUnsafe<NetworkInfo>(null))
}
assertFailsWith<NullPointerException> {
networkInfo.setDetailedState(NonNullTestUtils.nullUnsafe<DetailedState>(null),
"reason", "extraInfo")
}
// This actually throws ArrayOutOfBoundsException because of the implementation of
// EnumMap, but that's an implementation detail so accept any crash.
assertFails {
networkInfo.setDetailedState(incorrectDetailedState, "reason", "extraInfo")
}
} else {
// Doesn't immediately crash on S-
NetworkInfo(NonNullTestUtils.nullUnsafe<NetworkInfo>(null))
networkInfo.setDetailedState(NonNullTestUtils.nullUnsafe<DetailedState>(null),
"reason", "extraInfo")
}
}
}

View File

@@ -49,6 +49,7 @@ import android.os.Build
import android.telephony.TelephonyManager
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import com.android.testutils.NonNullTestUtils
import com.android.testutils.assertParcelSane
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@@ -218,6 +219,19 @@ class NetworkTemplateTest {
templateNullWifiKey.assertDoesNotMatch(identWifiNullKey)
}
@DevSdkIgnoreRule.IgnoreAfter(Build.VERSION_CODES.TIRAMISU)
@Test
fun testBuildTemplateMobileAll_nullSubscriberId() {
val templateMobileAllWithNullImsi =
buildTemplateMobileAll(NonNullTestUtils.nullUnsafe<String>(null))
val setWithNull = HashSet<String?>().apply {
add(null)
}
val templateFromBuilder = NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES)
.setSubscriberIds(setWithNull).build()
assertEquals(templateFromBuilder, templateMobileAllWithNullImsi)
}
@Test
fun testMobileMatches() {
val templateMobileImsi1 = buildTemplateMobileAll(TEST_IMSI1)