Merge "Fix the ethernet setting API bug."
This commit is contained in:
@@ -586,14 +586,18 @@ public class EthernetTracker {
|
||||
}
|
||||
}
|
||||
|
||||
private class InterfaceObserver extends BaseNetdUnsolicitedEventListener {
|
||||
@VisibleForTesting
|
||||
class InterfaceObserver extends BaseNetdUnsolicitedEventListener {
|
||||
|
||||
@Override
|
||||
public void onInterfaceLinkStateChanged(String iface, boolean up) {
|
||||
if (DBG) {
|
||||
Log.i(TAG, "interfaceLinkStateChanged, iface: " + iface + ", up: " + up);
|
||||
}
|
||||
mHandler.post(() -> updateInterfaceState(iface, up));
|
||||
mHandler.post(() -> {
|
||||
if (mEthernetState == ETHERNET_STATE_DISABLED) return;
|
||||
updateInterfaceState(iface, up);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -601,7 +605,10 @@ public class EthernetTracker {
|
||||
if (DBG) {
|
||||
Log.i(TAG, "onInterfaceAdded, iface: " + iface);
|
||||
}
|
||||
mHandler.post(() -> maybeTrackInterface(iface));
|
||||
mHandler.post(() -> {
|
||||
if (mEthernetState == ETHERNET_STATE_DISABLED) return;
|
||||
maybeTrackInterface(iface);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -609,7 +616,10 @@ public class EthernetTracker {
|
||||
if (DBG) {
|
||||
Log.i(TAG, "onInterfaceRemoved, iface: " + iface);
|
||||
}
|
||||
mHandler.post(() -> stopTrackingInterface(iface));
|
||||
mHandler.post(() -> {
|
||||
if (mEthernetState == ETHERNET_STATE_DISABLED) return;
|
||||
stopTrackingInterface(iface);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -888,6 +898,8 @@ public class EthernetTracker {
|
||||
void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) {
|
||||
postAndWaitForRunnable(() -> {
|
||||
pw.println(getClass().getSimpleName());
|
||||
pw.println("Ethernet State: "
|
||||
+ (mEthernetState == ETHERNET_STATE_ENABLED ? "enabled" : "disabled"));
|
||||
pw.println("Ethernet interface name filter: " + mIfaceMatch);
|
||||
pw.println("Default interface: " + mDefaultInterface);
|
||||
pw.println("Default interface mode: " + mDefaultInterfaceMode);
|
||||
|
||||
@@ -29,22 +29,23 @@ import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.net.EthernetManager;
|
||||
import android.net.InetAddresses;
|
||||
import android.net.INetworkInterfaceOutcomeReceiver;
|
||||
import android.net.IEthernetServiceListener;
|
||||
import android.net.INetd;
|
||||
import android.net.INetworkInterfaceOutcomeReceiver;
|
||||
import android.net.InetAddresses;
|
||||
import android.net.InterfaceConfigurationParcel;
|
||||
import android.net.IpConfiguration;
|
||||
import android.net.IpConfiguration.IpAssignment;
|
||||
import android.net.IpConfiguration.ProxySettings;
|
||||
import android.net.InterfaceConfigurationParcel;
|
||||
import android.net.LinkAddress;
|
||||
import android.net.NetworkCapabilities;
|
||||
import android.net.StaticIpConfiguration;
|
||||
@@ -54,13 +55,13 @@ import android.os.RemoteException;
|
||||
import androidx.test.filters.SmallTest;
|
||||
import androidx.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.android.connectivity.resources.R;
|
||||
import com.android.testutils.HandlerUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
@@ -410,18 +411,24 @@ public class EthernetTrackerTest {
|
||||
IpConfiguration configuration) { }
|
||||
}
|
||||
|
||||
private InterfaceConfigurationParcel createMockedIfaceParcel(final String ifname,
|
||||
final String hwAddr) {
|
||||
final InterfaceConfigurationParcel ifaceParcel = new InterfaceConfigurationParcel();
|
||||
ifaceParcel.ifName = ifname;
|
||||
ifaceParcel.hwAddr = hwAddr;
|
||||
ifaceParcel.flags = new String[] {INetd.IF_STATE_UP};
|
||||
return ifaceParcel;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListenEthernetStateChange() throws Exception {
|
||||
final String testIface = "testtap123";
|
||||
final String testHwAddr = "11:22:33:44:55:66";
|
||||
final InterfaceConfigurationParcel ifaceParcel = new InterfaceConfigurationParcel();
|
||||
ifaceParcel.ifName = testIface;
|
||||
ifaceParcel.hwAddr = testHwAddr;
|
||||
ifaceParcel.flags = new String[] {INetd.IF_STATE_UP};
|
||||
|
||||
tracker.setIncludeTestInterfaces(true);
|
||||
waitForIdle();
|
||||
|
||||
final String testIface = "testtap123";
|
||||
final String testHwAddr = "11:22:33:44:55:66";
|
||||
final InterfaceConfigurationParcel ifaceParcel = createMockedIfaceParcel(testIface,
|
||||
testHwAddr);
|
||||
when(mNetd.interfaceGetList()).thenReturn(new String[] {testIface});
|
||||
when(mNetd.interfaceGetCfg(eq(testIface))).thenReturn(ifaceParcel);
|
||||
doReturn(new String[] {testIface}).when(mFactory).getAvailableInterfaces(anyBoolean());
|
||||
@@ -453,4 +460,43 @@ public class EthernetTrackerTest {
|
||||
verify(listener).onInterfaceStateChanged(eq(testIface), eq(EthernetManager.STATE_LINK_UP),
|
||||
anyInt(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListenEthernetStateChange_unsolicitedEventListener() throws Exception {
|
||||
when(mNetd.interfaceGetList()).thenReturn(new String[] {});
|
||||
doReturn(new String[] {}).when(mFactory).getAvailableInterfaces(anyBoolean());
|
||||
|
||||
tracker.setIncludeTestInterfaces(true);
|
||||
tracker.start();
|
||||
|
||||
final ArgumentCaptor<EthernetTracker.InterfaceObserver> captor =
|
||||
ArgumentCaptor.forClass(EthernetTracker.InterfaceObserver.class);
|
||||
verify(mNetd, timeout(TIMEOUT_MS)).registerUnsolicitedEventListener(captor.capture());
|
||||
final EthernetTracker.InterfaceObserver observer = captor.getValue();
|
||||
|
||||
tracker.setEthernetEnabled(false);
|
||||
waitForIdle();
|
||||
reset(mFactory);
|
||||
reset(mNetd);
|
||||
|
||||
final String testIface = "testtap1";
|
||||
observer.onInterfaceAdded(testIface);
|
||||
verify(mFactory, never()).addInterface(eq(testIface), anyString(), any(), any());
|
||||
observer.onInterfaceRemoved(testIface);
|
||||
verify(mFactory, never()).removeInterface(eq(testIface));
|
||||
|
||||
final String testHwAddr = "11:22:33:44:55:66";
|
||||
final InterfaceConfigurationParcel testIfaceParce =
|
||||
createMockedIfaceParcel(testIface, testHwAddr);
|
||||
when(mNetd.interfaceGetList()).thenReturn(new String[] {testIface});
|
||||
when(mNetd.interfaceGetCfg(eq(testIface))).thenReturn(testIfaceParce);
|
||||
doReturn(new String[] {testIface}).when(mFactory).getAvailableInterfaces(anyBoolean());
|
||||
tracker.setEthernetEnabled(true);
|
||||
waitForIdle();
|
||||
reset(mFactory);
|
||||
|
||||
final String testIface2 = "testtap2";
|
||||
observer.onInterfaceRemoved(testIface2);
|
||||
verify(mFactory, timeout(TIMEOUT_MS)).removeInterface(eq(testIface2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user