Use helper function getIndexForValue in MulticastRoutingCoordinator.

The SparseArray.indexOfValue() uses == to compare values,
MulticastRoutingCoordinator uses SparseArray with String type values,
and the indexOfValue function would sometimes return -1 when looking for
a value in the SparseArray.

Added a helper function getIndexForValue in CollectionUtils to get index
by comparing values with equals, and used this function in MulticastRoutingCoordinator.

Updated
MulticastRoutingCoordinatorServiceTest#testMulticastRouting_applyConfigNone_removesMfc
test case to use copies of interface names when updating the multicast
configs, so the test would fail without this fix.

Test: atest NetworkStaticLibTests:com.android.net.moduletests.util.CollectionUtilsTest#testGetIndexForValue
atest android.net.connectivity.com.android.server.connectivity.MulticastRoutingCoordinatorServiceTest#testMulticastRouting_applyConfigNone_removesMfc

Change-Id: I862242d6be01b8552cdd6af6c9691948a9f0bfe0
This commit is contained in:
Yang Sun 2024-04-14 16:24:37 +08:00
commit 4e7baadc71

View file

@ -27,6 +27,8 @@ import static android.system.OsConstants.SOCK_CLOEXEC;
import static android.system.OsConstants.SOCK_NONBLOCK;
import static android.system.OsConstants.SOCK_RAW;
import static com.android.net.module.util.CollectionUtils.getIndexForValue;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.MulticastRoutingConfig;
@ -150,7 +152,7 @@ public class MulticastRoutingCoordinatorService {
}
private Integer getInterfaceIndex(String ifName) {
int mapIndex = mInterfaces.indexOfValue(ifName);
int mapIndex = getIndexForValue(mInterfaces, ifName);
if (mapIndex < 0) return null;
return mInterfaces.keyAt(mapIndex);
}
@ -246,7 +248,7 @@ public class MulticastRoutingCoordinatorService {
if (virtualIndex == null) return;
updateMfcs();
mInterfaces.removeAt(mInterfaces.indexOfValue(ifName));
mInterfaces.removeAt(getIndexForValue(mInterfaces, ifName));
mVirtualInterfaces.remove(virtualIndex);
try {
mDependencies.setsockoptMrt6DelMif(mMulticastRoutingFd, virtualIndex);
@ -270,7 +272,7 @@ public class MulticastRoutingCoordinatorService {
@VisibleForTesting
public Integer getVirtualInterfaceIndex(String ifName) {
int mapIndex = mVirtualInterfaces.indexOfValue(ifName);
int mapIndex = getIndexForValue(mVirtualInterfaces, ifName);
if (mapIndex < 0) return null;
return mVirtualInterfaces.keyAt(mapIndex);
}
@ -291,7 +293,7 @@ public class MulticastRoutingCoordinatorService {
private void maybeAddAndTrackInterface(String ifName) {
checkOnHandlerThread();
if (mVirtualInterfaces.indexOfValue(ifName) >= 0) return;
if (getIndexForValue(mVirtualInterfaces, ifName) >= 0) return;
int nextVirtualIndex = getNextAvailableVirtualIndex();
int ifIndex = mDependencies.getInterfaceIndex(ifName);