[SB Refactor] Add null checks around network capabilities.

It's still not clear to me how the old code didn't NPE but ag/22820241
started causing some NPEs, because ConnectivityManager specifies that
the NetworkCapabilities from #onCapabilitiesChanged will be non-null
(and the old code didn't check nullability). But, let's fix the crashes
now anyway.

I don't believe we need to fix the new pipeline because I haven't seen
any crash clusters come through.

Fixes: 280169520
Test: atest WifiStatusTrackerTest
Change-Id: I1f100b9d799d220505ac4ebf133ee7aae551f833
This commit is contained in:
Caitlin Shkuratov
2023-05-04 15:46:00 +00:00
parent e6d80348a5
commit a0052e490e
2 changed files with 27 additions and 3 deletions

View File

@@ -343,7 +343,12 @@ public class WifiStatusTracker {
}
@Nullable
private WifiInfo getMainOrUnderlyingWifiInfo(NetworkCapabilities networkCapabilities) {
private WifiInfo getMainOrUnderlyingWifiInfo(
@Nullable NetworkCapabilities networkCapabilities) {
if (networkCapabilities == null) {
return null;
}
WifiInfo mainWifiInfo = getMainWifiInfo(networkCapabilities);
if (mainWifiInfo != null) {
return mainWifiInfo;
@@ -376,7 +381,10 @@ public class WifiStatusTracker {
}
@Nullable
private WifiInfo getMainWifiInfo(NetworkCapabilities networkCapabilities) {
private WifiInfo getMainWifiInfo(@Nullable NetworkCapabilities networkCapabilities) {
if (networkCapabilities == null) {
return null;
}
boolean canHaveWifiInfo = networkCapabilities.hasTransport(TRANSPORT_WIFI)
|| networkCapabilities.hasTransport(TRANSPORT_CELLULAR);
if (!canHaveWifiInfo) {
@@ -402,7 +410,11 @@ public class WifiStatusTracker {
getMainOrUnderlyingWifiInfo(networkCapabilities));
}
private boolean connectionIsWifi(NetworkCapabilities networkCapabilities, WifiInfo wifiInfo) {
private boolean connectionIsWifi(
@Nullable NetworkCapabilities networkCapabilities, WifiInfo wifiInfo) {
if (networkCapabilities == null) {
return false;
}
return wifiInfo != null || networkCapabilities.hasTransport(TRANSPORT_WIFI);
}

View File

@@ -305,4 +305,16 @@ public class WifiStatusTrackerTest {
assertThat(mWifiStatusTracker.isDefaultNetwork).isTrue();
}
/** Regression test for b/280169520. */
@Test
public void networkCallbackNullCapabilities_noCrash() {
Network primaryNetwork = Mockito.mock(Network.class);
// WHEN the network capabilities are null
mNetworkCallbackCaptor.getValue().onCapabilitiesChanged(
primaryNetwork, /* networkCapabilities= */ null);
// THEN there's no crash (no assert needed)
}
}