diff --git a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java index aac71a0d7bb3c7836d7502314ef08173feb1e38b..c2326974a4b83c1f99c0abbcaceffe7be3fb0e3c 100644 --- a/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java +++ b/Tethering/tests/integration/base/android/net/EthernetTetheringTestBase.java @@ -57,8 +57,6 @@ import android.net.TetheringManager.TetheringEventCallback; import android.net.TetheringManager.TetheringRequest; import android.net.TetheringTester.TetheredDevice; import android.net.cts.util.CtsNetUtils; -import android.net.cts.util.CtsTetheringUtils; -import android.net.cts.util.CtsTetheringUtils.TestTetheringEventCallback; import android.os.Handler; import android.os.HandlerThread; import android.os.SystemClock; @@ -172,18 +170,24 @@ public abstract class EthernetTetheringTestBase { // 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. - final CtsTetheringUtils utils = new CtsTetheringUtils(sContext); - final TestTetheringEventCallback callback = utils.registerTetheringEventCallback(); + MyTetheringEventCallback callback = null; + TestNetworkInterface testIface = null; try { - if (!callback.isWifiTetheringSupported(sContext)) return; + // If the physical ethernet interface is available, do nothing. + if (isInterfaceForTetheringAvailable()) return; - callback.expectNoTetheringActive(); + testIface = createTestInterface(); + setIncludeTestInterfaces(true); - utils.startWifiTethering(callback); - callback.getCurrentValidUpstream(); - utils.stopWifiTethering(callback); + callback = enableEthernetTethering(testIface.getInterfaceName(), null); + callback.awaitUpstreamChanged(true /* throwTimeoutException */); + } catch (TimeoutException e) { + Log.d(TAG, "WARNNING " + e); } finally { - utils.unregisterTetheringEventCallback(callback); + maybeCloseTestInterface(testIface); + maybeUnregisterTetheringEventCallback(callback); + + setIncludeTestInterfaces(false); } } @@ -213,7 +217,7 @@ public abstract class EthernetTetheringTestBase { } } - protected void maybeCloseTestInterface(final TestNetworkInterface testInterface) + protected static void maybeCloseTestInterface(final TestNetworkInterface testInterface) throws Exception { if (testInterface != null) { testInterface.getFileDescriptor().close(); @@ -221,8 +225,8 @@ public abstract class EthernetTetheringTestBase { } } - protected void maybeUnregisterTetheringEventCallback(final MyTetheringEventCallback callback) - throws Exception { + protected static void maybeUnregisterTetheringEventCallback( + final MyTetheringEventCallback callback) throws Exception { if (callback != null) { callback.awaitInterfaceUntethered(); callback.unregister(); @@ -307,13 +311,13 @@ public abstract class EthernetTetheringTestBase { } } - protected void setIncludeTestInterfaces(boolean include) { + protected static void setIncludeTestInterfaces(boolean include) { runAsShell(NETWORK_SETTINGS, () -> { sEm.setIncludeTestInterfaces(include); }); } - protected void setPreferTestNetworks(boolean prefer) { + protected static void setPreferTestNetworks(boolean prefer) { runAsShell(NETWORK_SETTINGS, () -> { sTm.setPreferTestNetworks(prefer); }); @@ -355,7 +359,7 @@ public abstract class EthernetTetheringTestBase { private final TetheringInterface mIface; private final Network mExpectedUpstream; - private boolean mAcceptAnyUpstream = false; + private final boolean mAcceptAnyUpstream; private volatile boolean mInterfaceWasTethered = false; private volatile boolean mInterfaceWasLocalOnly = false; @@ -369,13 +373,16 @@ public abstract class EthernetTetheringTestBase { private static final int EXPANDED_TIMEOUT_MS = 30000; MyTetheringEventCallback(String iface) { - this(iface, null); + mIface = new TetheringInterface(TETHERING_ETHERNET, iface); + mExpectedUpstream = null; mAcceptAnyUpstream = true; } - MyTetheringEventCallback(String iface, Network expectedUpstream) { + MyTetheringEventCallback(String iface, @NonNull Network expectedUpstream) { + Objects.requireNonNull(expectedUpstream); mIface = new TetheringInterface(TETHERING_ETHERNET, iface); mExpectedUpstream = expectedUpstream; + mAcceptAnyUpstream = false; } public void unregister() { @@ -503,6 +510,11 @@ public abstract class EthernetTetheringTestBase { Log.d(TAG, "Got upstream changed: " + network); mUpstream = network; + // The callback always updates the current tethering status when it's first registered. + // If the caller registers the callback before tethering starts, the null upstream + // would be updated. Filtering out the null case because it's not a valid upstream that + // we care about. + if (mUpstream == null) return; if (mAcceptAnyUpstream || Objects.equals(mUpstream, mExpectedUpstream)) { mUpstreamLatch.countDown(); } @@ -643,7 +655,7 @@ public abstract class EthernetTetheringTestBase { } } - protected TestNetworkInterface createTestInterface() throws Exception { + protected static TestNetworkInterface createTestInterface() throws Exception { TestNetworkManager tnm = runAsShell(MANAGE_TEST_NETWORKS, () -> sContext.getSystemService(TestNetworkManager.class)); TestNetworkInterface iface = runAsShell(MANAGE_TEST_NETWORKS, () ->