[Status Bar][Wifi] Protect against invalid levels from WifiTrackerLib.
Fixes: 294544380 Bug: 292534484 Test: flip flag on, turn wifi off then on again -> verify no crash Test: atest WifiRepositoryViaTrackerLibTest Change-Id: I040f8c30761c7510e54866a6f849f5bde30a2ed4
This commit is contained in:
@@ -45,6 +45,9 @@ import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkMode
|
||||
import com.android.wifitrackerlib.HotspotNetworkEntry
|
||||
import com.android.wifitrackerlib.MergedCarrierEntry
|
||||
import com.android.wifitrackerlib.WifiEntry
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MAX
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE
|
||||
import com.android.wifitrackerlib.WifiPickerTracker
|
||||
import java.util.concurrent.Executor
|
||||
import javax.inject.Inject
|
||||
@@ -204,6 +207,12 @@ constructor(
|
||||
}
|
||||
|
||||
private fun WifiEntry.convertNormalToModel(): WifiNetworkModel {
|
||||
if (this.level == WIFI_LEVEL_UNREACHABLE || this.level !in WIFI_LEVEL_MIN..WIFI_LEVEL_MAX) {
|
||||
// If our level means the network is unreachable or the level is otherwise invalid, we
|
||||
// don't have an active network.
|
||||
return WifiNetworkModel.Inactive
|
||||
}
|
||||
|
||||
val hotspotDeviceType =
|
||||
if (isInstantTetherEnabled && this is HotspotNetworkEntry) {
|
||||
this.deviceType.toHotspotDeviceType()
|
||||
|
||||
@@ -314,11 +314,15 @@ sealed class WifiNetworkModel : Diffable<WifiNetworkModel> {
|
||||
}
|
||||
|
||||
companion object {
|
||||
// TODO(b/292534484): Use [com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MAX] instead
|
||||
// once the migration to WifiTrackerLib is complete.
|
||||
@VisibleForTesting internal const val MAX_VALID_LEVEL = 4
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
// TODO(b/292534484): Use [com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN] instead
|
||||
// once the migration to WifiTrackerLib is complete.
|
||||
@VisibleForTesting internal const val MIN_VALID_LEVEL = 0
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,9 @@ import com.android.wifitrackerlib.HotspotNetworkEntry
|
||||
import com.android.wifitrackerlib.HotspotNetworkEntry.DeviceType
|
||||
import com.android.wifitrackerlib.MergedCarrierEntry
|
||||
import com.android.wifitrackerlib.WifiEntry
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MAX
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_MIN
|
||||
import com.android.wifitrackerlib.WifiEntry.WIFI_LEVEL_UNREACHABLE
|
||||
import com.android.wifitrackerlib.WifiPickerTracker
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
@@ -302,6 +305,88 @@ class WifiRepositoryViaTrackerLibTest : SysuiTestCase() {
|
||||
assertThat(latestActive.passpointProviderFriendlyName).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_unreachableLevel_inactiveNetwork() =
|
||||
testScope.runTest {
|
||||
val latest by collectLastValue(underTest.wifiNetwork)
|
||||
|
||||
val wifiEntry =
|
||||
mock<WifiEntry>().apply {
|
||||
whenever(this.isPrimaryNetwork).thenReturn(true)
|
||||
whenever(this.level).thenReturn(WIFI_LEVEL_UNREACHABLE)
|
||||
}
|
||||
whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
|
||||
getCallback().onWifiEntriesChanged()
|
||||
|
||||
assertThat(latest).isEqualTo(WifiNetworkModel.Inactive)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_levelTooHigh_inactiveNetwork() =
|
||||
testScope.runTest {
|
||||
val latest by collectLastValue(underTest.wifiNetwork)
|
||||
|
||||
val wifiEntry =
|
||||
mock<WifiEntry>().apply {
|
||||
whenever(this.isPrimaryNetwork).thenReturn(true)
|
||||
whenever(this.level).thenReturn(WIFI_LEVEL_MAX + 1)
|
||||
}
|
||||
whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
|
||||
getCallback().onWifiEntriesChanged()
|
||||
|
||||
assertThat(latest).isEqualTo(WifiNetworkModel.Inactive)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_levelTooLow_inactiveNetwork() =
|
||||
testScope.runTest {
|
||||
val latest by collectLastValue(underTest.wifiNetwork)
|
||||
|
||||
val wifiEntry =
|
||||
mock<WifiEntry>().apply {
|
||||
whenever(this.isPrimaryNetwork).thenReturn(true)
|
||||
whenever(this.level).thenReturn(WIFI_LEVEL_MIN - 1)
|
||||
}
|
||||
whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
|
||||
getCallback().onWifiEntriesChanged()
|
||||
|
||||
assertThat(latest).isEqualTo(WifiNetworkModel.Inactive)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_levelIsMax_activeNetworkWithMaxLevel() =
|
||||
testScope.runTest {
|
||||
val latest by collectLastValue(underTest.wifiNetwork)
|
||||
|
||||
val wifiEntry =
|
||||
mock<WifiEntry>().apply {
|
||||
whenever(this.isPrimaryNetwork).thenReturn(true)
|
||||
whenever(this.level).thenReturn(WIFI_LEVEL_MAX)
|
||||
}
|
||||
whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
|
||||
getCallback().onWifiEntriesChanged()
|
||||
|
||||
assertThat(latest).isInstanceOf(WifiNetworkModel.Active::class.java)
|
||||
assertThat((latest as WifiNetworkModel.Active).level).isEqualTo(WIFI_LEVEL_MAX)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_levelIsMin_activeNetworkWithMinLevel() =
|
||||
testScope.runTest {
|
||||
val latest by collectLastValue(underTest.wifiNetwork)
|
||||
|
||||
val wifiEntry =
|
||||
mock<WifiEntry>().apply {
|
||||
whenever(this.isPrimaryNetwork).thenReturn(true)
|
||||
whenever(this.level).thenReturn(WIFI_LEVEL_MIN)
|
||||
}
|
||||
whenever(wifiPickerTracker.connectedWifiEntry).thenReturn(wifiEntry)
|
||||
getCallback().onWifiEntriesChanged()
|
||||
|
||||
assertThat(latest).isInstanceOf(WifiNetworkModel.Active::class.java)
|
||||
assertThat((latest as WifiNetworkModel.Active).level).isEqualTo(WIFI_LEVEL_MIN)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun wifiNetwork_notHotspot_none() =
|
||||
testScope.runTest {
|
||||
|
||||
Reference in New Issue
Block a user