ethernet: add test for removing interface while in server mode

Bug: 236892130
Test: atest EthernetManagerTest#testRemoveInterface_whileInServerMode
Change-Id: I3ca71cd87301da8e96d31f7632b69d318ee97d4a
This commit is contained in:
Patrick Rohr 2022-06-22 17:21:30 -07:00
commit f34945bf6f

View file

@ -68,14 +68,15 @@ import com.android.testutils.TestableNetworkCallback
import com.android.testutils.runAsShell
import com.android.testutils.waitForIdle
import org.junit.After
import org.junit.Assume.assumeTrue
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.net.Inet6Address
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeoutException
import java.util.concurrent.TimeUnit
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
@ -458,6 +459,21 @@ class EthernetManagerTest {
}
}
private fun assumeNoInterfaceForTetheringAvailable() {
// Interfaces that have configured NetworkCapabilities will never be used for tethering,
// see aosp/2123900.
try {
// assumeException does not exist.
requestTetheredInterface().expectOnAvailable()
// interface used for tethering is available, throw an assumption error.
assumeTrue(false)
} catch (e: TimeoutException) {
// do nothing -- the TimeoutException indicates that no interface is available for
// tethering.
releaseTetheredInterface()
}
}
// TODO: this function is now used in two places (EthernetManagerTest and
// EthernetTetheringTest), so it should be moved to testutils.
private fun isAdbOverNetwork(): Boolean {
@ -469,6 +485,7 @@ class EthernetManagerTest {
@Test
fun testCallbacks_forServerModeInterfaces() {
// do not run this test when adb might be connected over ethernet.
// TODO: Consider using assumeNoInterfaceForTetheringAvailable() instead, so this runs on CF
assumeFalse(isAdbOverNetwork())
val listener = EthernetStateListener()
@ -651,4 +668,26 @@ class EthernetManagerTest {
iface.setCarrierEnabled(false)
cb.eventuallyExpectLost()
}
@Test
fun testRemoveInterface_whileInServerMode() {
assumeNoInterfaceForTetheringAvailable()
val listener = EthernetStateListener()
addInterfaceStateListener(listener)
val iface = createInterface()
val ifaceName = requestTetheredInterface().expectOnAvailable()
assertEquals(iface.name, ifaceName)
listener.eventuallyExpect(iface, STATE_LINK_UP, ROLE_SERVER)
removeInterface(iface)
// Note: removeInterface already verifies that a STATE_ABSENT, ROLE_NONE callback is
// received, but it can't hurt to explicitly check for it.
listener.expectCallback(iface, STATE_ABSENT, ROLE_NONE)
releaseTetheredInterface()
listener.assertNoCallback()
}
}