Stop tracking left-over test interfaces

When setIncludeTestInterfaces(false) is called, any RTM_DELLINK
callback for a testtap interface is ignored, since the interface is no
longer considered to be a valid ethernet interface.

Change setIncludeTestInterfaces to go through all interfaces, and stop
tracking the ones which are no longer valid. Otherwise, they will
stick around forever (and break subsequent tests).

Bug: 390722214
Test: TH
Change-Id: Ied73e86c0452831f4d6b00f67908c51201af8bbd
This commit is contained in:
Patrick Rohr 2025-01-29 07:34:27 -08:00
commit d16138eeea
2 changed files with 20 additions and 13 deletions

View file

@ -397,6 +397,16 @@ public class EthernetTracker {
return mFactory.hasInterface(iface);
}
private List<String> getAllInterfaces() {
final ArrayList<String> interfaces = new ArrayList<>(
List.of(mFactory.getAvailableInterfaces(/* includeRestricted */ true)));
if (mTetheringInterfaceMode == INTERFACE_MODE_SERVER && mTetheringInterface != null) {
interfaces.add(mTetheringInterface);
}
return interfaces;
}
String[] getClientModeInterfaces(boolean includeRestricted) {
return mFactory.getAvailableInterfaces(includeRestricted);
}
@ -459,10 +469,16 @@ public class EthernetTracker {
public void setIncludeTestInterfaces(boolean include) {
mHandler.post(() -> {
mIncludeTestInterfaces = include;
if (!include) {
if (include) {
trackAvailableInterfaces();
} else {
removeTestData();
// remove all test interfaces
for (String iface : getAllInterfaces()) {
if (isValidEthernetInterface(iface)) continue;
stopTrackingInterface(iface);
}
}
trackAvailableInterfaces();
});
}
@ -952,17 +968,7 @@ public class EthernetTracker {
if (mIsEthernetEnabled == enabled) return;
mIsEthernetEnabled = enabled;
// Interface in server mode should also be included.
ArrayList<String> interfaces =
new ArrayList<>(
List.of(mFactory.getAvailableInterfaces(/* includeRestricted */ true)));
if (mTetheringInterfaceMode == INTERFACE_MODE_SERVER && mTetheringInterface != null) {
interfaces.add(mTetheringInterface);
}
for (String iface : interfaces) {
for (String iface : getAllInterfaces()) {
setInterfaceUpState(iface, enabled);
}
broadcastEthernetStateChange(mIsEthernetEnabled);

View file

@ -79,6 +79,7 @@ public class EthernetTrackerTest {
initMockResources();
doReturn(false).when(mFactory).updateInterfaceLinkState(anyString(), anyBoolean());
doReturn(new String[0]).when(mNetd).interfaceGetList();
doReturn(new String[0]).when(mFactory).getAvailableInterfaces(anyBoolean());
mHandlerThread = new HandlerThread(THREAD_NAME);
mHandlerThread.start();
tracker = new EthernetTracker(mContext, mHandlerThread.getThreadHandler(), mFactory, mNetd,