Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 619c28cd authored by Xiao Ma's avatar Xiao Ma
Browse files

Add tests to verify whether IpClient crashes when wlan interface doesn't exist.

Bug: 113350007
Test: atest NetworkStackIntegrationTests

Change-Id: I60ba097997193d9150567dca7156750004425602
parent dc9ef640
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -1289,7 +1289,8 @@ public class IpClient extends StateMachine {
            return;
        }

        if (params.defaultMtu != mInterfaceParams.defaultMtu) {
        if (params.defaultMtu == mInterfaceParams.defaultMtu) return;

        try {
            mNetd.interfaceSetMtu(mInterfaceName, mInterfaceParams.defaultMtu);
        } catch (RemoteException | ServiceSpecificException e) {
@@ -1297,7 +1298,6 @@ public class IpClient extends StateMachine {
                    + params.defaultMtu + " to " + mInterfaceParams.defaultMtu, e);
        }
    }
    }

    class StoppedState extends State {
        @Override
+60 −13
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.INetd;
import android.net.InetAddresses;
import android.net.LinkProperties;
import android.net.NetworkStackIpMemoryStore;
import android.net.TestNetworkInterface;
import android.net.TestNetworkManager;
@@ -69,6 +70,8 @@ import android.os.HandlerThread;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.system.ErrnoException;
import android.system.Os;

import androidx.annotation.Nullable;
import androidx.test.InstrumentationRegistry;
@@ -395,8 +398,8 @@ public class IpClientIntegrationTest {
        mDependencies.setDhcpRapidCommitEnabled(isDhcpRapidCommitEnabled);
        mIpc.setL2KeyAndGroupHint(TEST_L2KEY, TEST_GROUPHINT);
        mIpc.startProvisioning(config);
        verify(mCb, times(1)).setNeighborDiscoveryOffload(true);
        verify(mCb, timeout(TEST_TIMEOUT_MS).times(1)).setFallbackMulticastFilter(false);
        verify(mCb).setNeighborDiscoveryOffload(true);
        verify(mCb, timeout(TEST_TIMEOUT_MS)).setFallbackMulticastFilter(false);
        verify(mCb, never()).onProvisioningFailure(any());
    }

@@ -478,7 +481,22 @@ public class IpClientIntegrationTest {
        return getNextDhcpPacket();
    }

    private void doRestoreInitialMtuTest(final boolean shouldChangeMtu) throws Exception {
    private void removeTapInterface(final FileDescriptor fd) {
        try {
            Os.close(fd);
        } catch (ErrnoException e) {
            fail("Fail to close file descriptor: " + e);
        }
    }

    private void verifyAfterIpClientShutdown() throws RemoteException {
        final LinkProperties emptyLp = new LinkProperties();
        emptyLp.setInterfaceName(mIfaceName);
        verify(mCb, timeout(TEST_TIMEOUT_MS)).onLinkPropertiesChange(emptyLp);
    }

    private void doRestoreInitialMtuTest(final boolean shouldChangeMtu,
            final boolean shouldRemoveTapInterface) throws Exception {
        final long currentTime = System.currentTimeMillis();
        int mtu = TEST_DEFAULT_MTU;

@@ -493,11 +511,21 @@ public class IpClientIntegrationTest {
            assertEquals(NetworkInterface.getByName(mIfaceName).getMTU(), mtu);
        }

        if (shouldRemoveTapInterface) removeTapInterface(mPacketReader.createFd());
        try {
            mIpc.shutdown();
            HandlerUtilsKt.waitForIdle(mIpc.getHandler(), TEST_TIMEOUT_MS);
            if (shouldRemoveTapInterface) {
                verify(mMockNetd, never()).interfaceSetMtu(mIfaceName, TEST_DEFAULT_MTU);
            } else {
                // Verify that MTU indeed has been restored or not.
        verify(mMockNetd, times(shouldChangeMtu ? 1 : 0)).interfaceSetMtu(mIfaceName,
                TEST_DEFAULT_MTU);
                verify(mMockNetd, times(shouldChangeMtu ? 1 : 0))
                        .interfaceSetMtu(mIfaceName, TEST_DEFAULT_MTU);
            }
            verifyAfterIpClientShutdown();
        } catch (Exception e) {
            fail("Exception should not have been thrown after shutdown: " + e);
        }
    }

    @Test
@@ -628,20 +656,39 @@ public class IpClientIntegrationTest {

    @Test
    public void testRestoreInitialInterfaceMtu() throws Exception {
        doRestoreInitialMtuTest(true /* shouldChangeMtu */);
        doRestoreInitialMtuTest(true /* shouldChangeMtu */, false /* shouldRemoveTapInterface */);
    }

    @Test
    public void testRestoreInitialInterfaceMtuWithoutChange() throws Exception {
        doRestoreInitialMtuTest(false /* shouldChangeMtu */);
    public void testRestoreInitialInterfaceMtu_WithoutMtuChange() throws Exception {
        doRestoreInitialMtuTest(false /* shouldChangeMtu */, false /* shouldRemoveTapInterface */);
    }

    @Test
    public void testRestoreInitialInterfaceMtuWithException() throws Exception {
    public void testRestoreInitialInterfaceMtu_WithException() throws Exception {
        doThrow(new RemoteException("NetdNativeService::interfaceSetMtu")).when(mMockNetd)
                .interfaceSetMtu(mIfaceName, TEST_DEFAULT_MTU);

        doRestoreInitialMtuTest(true /* shouldChangeMtu */);
        doRestoreInitialMtuTest(true /* shouldChangeMtu */, false /* shouldRemoveTapInterface */);
        assertEquals(NetworkInterface.getByName(mIfaceName).getMTU(), TEST_MIN_MTU);
    }

    @Test
    public void testRestoreInitialInterfaceMtu_NotFoundInterfaceWhenStopping() throws Exception {
        doRestoreInitialMtuTest(true /* shouldChangeMtu */, true /* shouldRemoveTapInterface */);
    }

    @Test
    public void testRestoreInitialInterfaceMtu_NotFoundInterfaceWhenStartingProvisioning()
            throws Exception {
        removeTapInterface(mPacketReader.createFd());
        ProvisioningConfiguration config = new ProvisioningConfiguration.Builder()
                .withoutIpReachabilityMonitor()
                .withoutIPv6()
                .build();

        mIpc.startProvisioning(config);
        verify(mCb).onProvisioningFailure(any());
        verify(mCb, never()).setNeighborDiscoveryOffload(true);
    }
}