Skip to content

Commit

Permalink
Avoid null in kotlin.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadroyaL committed Feb 5, 2024
1 parent 72e423d commit 1caca19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import android.os.Build
import androidx.annotation.RequiresApi
import java.nio.ByteBuffer

enum class FastRoaming(val protocol: String) {
REQUIRE_ANDROID_R(""),
enum class FastRoaming(val protocol: String = "") {
ILLEGAL_ATTR,
REQUIRE_ANDROID_R,
K("802.11k"),
V("802.11v"),
R("802.11r");

companion object {
@RequiresApi(Build.VERSION_CODES.R)
fun transformOrNull(it: ScanResult.InformationElement): FastRoaming? =
fun transform(it: ScanResult.InformationElement): FastRoaming =
if (it.id == WiFiSignal.RM_ENABLED_CAPABILITIES_IE &&
validRoamingBit(
it.bytes,
Expand All @@ -47,7 +48,7 @@ enum class FastRoaming(val protocol: String) {
) V
else if (it.id == WiFiSignal.MOBILE_DOMAIN_IE
) R
else null
else ILLEGAL_ATTR

// some evil access point broadcasts illegal package, which may trigger oob exception
// make sure limit > index first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ internal class Transformer(private val cache: Cache) {

internal fun fastRoaming(scanResult: ScanResult): List<FastRoaming> =
if (minVersionR()) {
scanResult.informationElements.mapNotNull { FastRoaming.transformOrNull(it) }.sorted()
scanResult.informationElements.map { FastRoaming.transform(it) }
.filter { it != FastRoaming.ILLEGAL_ATTR }
.sorted()
} else {
listOf(FastRoaming.REQUIRE_ANDROID_R)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ class FastRoamingTest {
@Test
fun testFastRoaming() {
// execute
val actual = withFastRoaming.map { FastRoaming.transformOrNull(it) }
val actual = withFastRoaming.map { FastRoaming.transform(it) }
// validate
assertEquals(listOf(FastRoaming.K, FastRoaming.V, FastRoaming.R), actual)
}

@Test
fun testNoFastRoaming() {
// execute
val actual = withoutFastRoaming.map { FastRoaming.transformOrNull(it) }
val actual = withoutFastRoaming.map { FastRoaming.transform(it) }
// validate
assertEquals(List(5) { null }, actual)
assertEquals(List(5) { FastRoaming.ILLEGAL_ATTR }, actual)
}

private fun mockInformationElement(id: Int, idExt: Int, bytes: ByteArray)
Expand Down

0 comments on commit 1caca19

Please sign in to comment.