Merge "Move setupOnce from EthernetTetheringTestBase to EthernetTetheringTest" into main

This commit is contained in:
Mark Chien 2024-04-09 07:42:11 +00:00 • committed by Gerrit Code Review
commit 01e091b1fb
2 changed files with 31 additions and 30 deletions

View file

@ -74,7 +74,6 @@ import com.android.testutils.TestNetworkTracker;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import java.io.FileDescriptor;
import java.net.Inet4Address;
@ -144,7 +143,7 @@ public abstract class EthernetTetheringTestBase {
private static final Context sContext =
InstrumentationRegistry.getInstrumentation().getContext();
private static final EthernetManager sEm = sContext.getSystemService(EthernetManager.class);
protected static final EthernetManager sEm = sContext.getSystemService(EthernetManager.class);
private static final TetheringManager sTm = sContext.getSystemService(TetheringManager.class);
private static final PackageManager sPackageManager = sContext.getPackageManager();
private static final CtsNetUtils sCtsNetUtils = new CtsNetUtils(sContext);
@ -168,34 +167,6 @@ public abstract class EthernetTetheringTestBase {
return sContext;
}
@BeforeClass
public static void setUpOnce() throws Exception {
// The first test case may experience tethering restart with IP conflict handling.
// Tethering would cache the last upstreams so that the next enabled tethering avoids
// picking up the address that is in conflict with the upstreams. To protect subsequent
// tests, turn tethering on and off before running them.
MyTetheringEventCallback callback = null;
TestNetworkInterface testIface = null;
assumeTrue(sEm != null);
try {
// If the physical ethernet interface is available, do nothing.
if (isInterfaceForTetheringAvailable()) return;
testIface = createTestInterface();
setIncludeTestInterfaces(true);
callback = enableEthernetTethering(testIface.getInterfaceName(), null);
callback.awaitUpstreamChanged(true /* throwTimeoutException */);
} catch (TimeoutException e) {
Log.d(TAG, "WARNNING " + e);
} finally {
maybeCloseTestInterface(testIface);
maybeUnregisterTetheringEventCallback(callback);
setIncludeTestInterfaces(false);
}
}
@Before
public void setUp() throws Exception {
mHandlerThread = new HandlerThread(getClass().getSimpleName());

View file

@ -59,6 +59,7 @@ import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.NetworkStackModuleTest;
import com.android.testutils.TapPacketReader;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -150,6 +151,35 @@ public class EthernetTetheringTest extends EthernetTetheringTestBase {
(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04 /* Address: 1.2.3.4 */
};
/** Enable/disable tethering once before running the tests. */
@BeforeClass
public static void setUpOnce() throws Exception {
// The first test case may experience tethering restart with IP conflict handling.
// Tethering would cache the last upstreams so that the next enabled tethering avoids
// picking up the address that is in conflict with the upstreams. To protect subsequent
// tests, turn tethering on and off before running them.
MyTetheringEventCallback callback = null;
TestNetworkInterface testIface = null;
assumeTrue(sEm != null);
try {
// If the physical ethernet interface is available, do nothing.
if (isInterfaceForTetheringAvailable()) return;
testIface = createTestInterface();
setIncludeTestInterfaces(true);
callback = enableEthernetTethering(testIface.getInterfaceName(), null);
callback.awaitUpstreamChanged(true /* throwTimeoutException */);
} catch (TimeoutException e) {
Log.d(TAG, "WARNNING " + e);
} finally {
maybeCloseTestInterface(testIface);
maybeUnregisterTetheringEventCallback(callback);
setIncludeTestInterfaces(false);
}
}
@Test
public void testVirtualEthernetAlreadyExists() throws Exception {
// This test requires manipulating packets. Skip if there is a physical Ethernet connected.