Inline tetherInterface and untetherInterface
This is a no-op refactoring which inline the functionalities
to IpServer, enabling subsequent changes to move
networkAddInterface part to ConnectivityService when the
tethering is enabled.
Test: atest TetheringTests:android.net.ip.IpServerTest
atest NetdStaticLibTests:com.android.net.module.util.NetdUtilsTest
Bug: 349487600
Change-Id: I6d0e5d349691df7a15bee852d5fd1dd19f5da6f6
diff --git a/Tethering/src/android/net/ip/IpServer.java b/Tethering/src/android/net/ip/IpServer.java
index d2fd103..d0ba431 100644
--- a/Tethering/src/android/net/ip/IpServer.java
+++ b/Tethering/src/android/net/ip/IpServer.java
@@ -1125,7 +1125,11 @@
}
try {
- NetdUtils.tetherInterface(mNetd, LOCAL_NET_ID, mIfaceName);
+ // Enable IPv6, disable accepting RA, etc. See TetherController::tetherInterface()
+ // for more detail.
+ mNetd.tetherInterfaceAdd(mIfaceName);
+ NetdUtils.networkAddInterface(mNetd, LOCAL_NET_ID, mIfaceName,
+ 20 /* maxAttempts */, 50 /* pollingIntervalMs */);
// Activate a route to dest and IPv6 link local.
NetdUtils.modifyRoute(mNetd, NetdUtils.ModifyOperation.ADD, LOCAL_NET_ID,
new RouteInfo(asIpPrefix(mIpv4Address), null, mIfaceName, RTN_UNICAST));
@@ -1152,8 +1156,13 @@
// all in sequence.
stopIPv6();
+ // Reset interface for tethering.
try {
- NetdUtils.untetherInterface(mNetd, LOCAL_NET_ID, mIfaceName);
+ try {
+ mNetd.tetherInterfaceRemove(mIfaceName);
+ } finally {
+ mNetd.networkRemoveInterface(LOCAL_NET_ID, mIfaceName);
+ }
} catch (RemoteException | ServiceSpecificException e) {
mLastError = TETHER_ERROR_UNTETHER_IFACE_ERROR;
mLog.e("Failed to untether interface: " + e);
diff --git a/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java b/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java
index 7e261ba..4af516d 100644
--- a/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java
+++ b/staticlibs/client-libs/netd/com/android/net/module/util/NetdUtils.java
@@ -148,20 +148,6 @@
netd.tetherStartWithConfiguration(config);
}
- /** Setup interface for tethering. */
- public static void tetherInterface(final INetd netd, int netId, final String iface)
- throws RemoteException, ServiceSpecificException {
- tetherInterface(netd, netId, iface, 20 /* maxAttempts */, 50 /* pollingIntervalMs */);
- }
-
- /** Setup interface with configurable retries for tethering. */
- public static void tetherInterface(final INetd netd, int netId, final String iface,
- int maxAttempts, int pollingIntervalMs)
- throws RemoteException, ServiceSpecificException {
- netd.tetherInterfaceAdd(iface);
- networkAddInterface(netd, netId, iface, maxAttempts, pollingIntervalMs);
- }
-
/**
* Retry Netd#networkAddInterface for EBUSY error code.
* If the same interface (e.g., wlan0) is in client mode and then switches to tethered mode.
@@ -169,7 +155,7 @@
* in use in netd because the ConnectivityService thread hasn't processed the disconnect yet.
* See b/158269544 for detail.
*/
- private static void networkAddInterface(final INetd netd, int netId, final String iface,
+ public static void networkAddInterface(final INetd netd, int netId, final String iface,
int maxAttempts, int pollingIntervalMs)
throws ServiceSpecificException, RemoteException {
for (int i = 1; i <= maxAttempts; i++) {
@@ -188,16 +174,6 @@
}
}
- /** Reset interface for tethering. */
- public static void untetherInterface(final INetd netd, int netId, String iface)
- throws RemoteException, ServiceSpecificException {
- try {
- netd.tetherInterfaceRemove(iface);
- } finally {
- netd.networkRemoveInterface(netId, iface);
- }
- }
-
/** Add |routes| to the given network. */
public static void addRoutesToNetwork(final INetd netd, int netId, final String iface,
final List<RouteInfo> routes) {
diff --git a/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java b/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java
index 03128fa..0241d0a 100644
--- a/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java
+++ b/staticlibs/client-libs/tests/unit/src/com/android/net/module/util/NetdUtilsTest.java
@@ -24,13 +24,9 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
-import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -148,20 +144,21 @@
}
@Test
- public void testTetherInterfaceSuccessful() throws Exception {
+ public void testNetworkAddInterfaceSuccessful() throws Exception {
// Expect #networkAddInterface successful at first tries.
- verifyTetherInterfaceSucceeds(1);
+ verifyNetworkAddInterfaceSucceeds(1);
// Expect #networkAddInterface successful after 10 tries.
- verifyTetherInterfaceSucceeds(10);
+ verifyNetworkAddInterfaceSucceeds(10);
}
- private void runTetherInterfaceWithServiceSpecificException(int expectedTries,
+ private void runNetworkAddInterfaceWithServiceSpecificException(int expectedTries,
int expectedCode) throws Exception {
setNetworkAddInterfaceOutcome(new ServiceSpecificException(expectedCode), expectedTries);
try {
- NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE, 20, 0);
+ NetdUtils.networkAddInterface(mNetd, TEST_NET_ID, IFACE,
+ 20 /* maxAttempts */, 0 /* pollingIntervalMs */);
fail("Expect throw ServiceSpecificException");
} catch (ServiceSpecificException e) {
assertEquals(e.errorCode, expectedCode);
@@ -171,11 +168,12 @@
reset(mNetd);
}
- private void runTetherInterfaceWithRemoteException(int expectedTries) throws Exception {
+ private void runNetworkAddInterfaceWithRemoteException(int expectedTries) throws Exception {
setNetworkAddInterfaceOutcome(new RemoteException(), expectedTries);
try {
- NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE, 20, 0);
+ NetdUtils.networkAddInterface(mNetd, TEST_NET_ID, IFACE,
+ 20 /* maxAttempts */, 0 /* pollingIntervalMs */);
fail("Expect throw RemoteException");
} catch (RemoteException e) { }
@@ -184,40 +182,37 @@
}
private void verifyNetworkAddInterfaceFails(int expectedTries) throws Exception {
- verify(mNetd).tetherInterfaceAdd(IFACE);
verify(mNetd, times(expectedTries)).networkAddInterface(TEST_NET_ID, IFACE);
- verify(mNetd, never()).networkAddRoute(anyInt(), anyString(), any(), any());
-
verifyNoMoreInteractions(mNetd);
}
- private void verifyTetherInterfaceSucceeds(int expectedTries) throws Exception {
+ private void verifyNetworkAddInterfaceSucceeds(int expectedTries) throws Exception {
setNetworkAddInterfaceOutcome(null, expectedTries);
- NetdUtils.tetherInterface(mNetd, TEST_NET_ID, IFACE);
- verify(mNetd).tetherInterfaceAdd(IFACE);
+ NetdUtils.networkAddInterface(mNetd, TEST_NET_ID, IFACE,
+ 20 /* maxAttempts */, 0 /* pollingIntervalMs */);
verify(mNetd, times(expectedTries)).networkAddInterface(TEST_NET_ID, IFACE);
verifyNoMoreInteractions(mNetd);
reset(mNetd);
}
@Test
- public void testTetherInterfaceFailOnNetworkAddInterface() throws Exception {
+ public void testFailOnNetworkAddInterface() throws Exception {
// Test throwing ServiceSpecificException with EBUSY failure.
- runTetherInterfaceWithServiceSpecificException(20, EBUSY);
+ runNetworkAddInterfaceWithServiceSpecificException(20, EBUSY);
// Test throwing ServiceSpecificException with unexpectedError.
final int unexpectedError = 999;
- runTetherInterfaceWithServiceSpecificException(1, unexpectedError);
+ runNetworkAddInterfaceWithServiceSpecificException(1, unexpectedError);
// Test throwing ServiceSpecificException with unexpectedError after 7 tries.
- runTetherInterfaceWithServiceSpecificException(7, unexpectedError);
+ runNetworkAddInterfaceWithServiceSpecificException(7, unexpectedError);
// Test throwing RemoteException.
- runTetherInterfaceWithRemoteException(1);
+ runNetworkAddInterfaceWithRemoteException(1);
// Test throwing RemoteException after 3 tries.
- runTetherInterfaceWithRemoteException(3);
+ runNetworkAddInterfaceWithRemoteException(3);
}
@Test