Merge "Put tether/untether calls into handler queue"

This commit is contained in:
Treehugger Robot
2021-04-15 14:46:25 +00:00
committed by Gerrit Code Review
4 changed files with 95 additions and 21 deletions

View File

@@ -748,8 +748,12 @@ public class Tethering {
} }
} }
int tether(String iface) { void tether(String iface, final IIntResultListener listener) {
return tether(iface, IpServer.STATE_TETHERED); mHandler.post(() -> {
try {
listener.onResult(tether(iface, IpServer.STATE_TETHERED));
} catch (RemoteException e) { }
});
} }
private int tether(String iface, int requestedState) { private int tether(String iface, int requestedState) {
@@ -782,6 +786,14 @@ public class Tethering {
} }
} }
void untether(String iface, final IIntResultListener listener) {
mHandler.post(() -> {
try {
listener.onResult(untether(iface));
} catch (RemoteException e) { }
});
}
int untether(String iface) { int untether(String iface) {
if (DBG) Log.d(TAG, "Untethering " + iface); if (DBG) Log.d(TAG, "Untethering " + iface);
synchronized (mPublicSync) { synchronized (mPublicSync) {

View File

@@ -105,9 +105,7 @@ public class TetheringService extends Service {
IIntResultListener listener) { IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, callingAttributionTag, listener)) return; if (checkAndNotifyCommonError(callerPkg, callingAttributionTag, listener)) return;
try { mTethering.tether(iface, listener);
listener.onResult(mTethering.tether(iface));
} catch (RemoteException e) { }
} }
@Override @Override
@@ -115,9 +113,7 @@ public class TetheringService extends Service {
IIntResultListener listener) { IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, callingAttributionTag, listener)) return; if (checkAndNotifyCommonError(callerPkg, callingAttributionTag, listener)) return;
try { mTethering.untether(iface, listener);
listener.onResult(mTethering.untether(iface));
} catch (RemoteException e) { }
} }
@Override @Override

View File

@@ -156,11 +156,9 @@ public final class TetheringServiceTest {
} }
private void runTether(final TestTetheringResult result) throws Exception { private void runTether(final TestTetheringResult result) throws Exception {
when(mTethering.tether(TEST_IFACE_NAME)).thenReturn(TETHER_ERROR_NO_ERROR);
mTetheringConnector.tether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); mTetheringConnector.tether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).tether(TEST_IFACE_NAME); verify(mTethering).tether(eq(TEST_IFACE_NAME), eq(result));
result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@Test @Test
@@ -186,12 +184,10 @@ public final class TetheringServiceTest {
} }
private void runUnTether(final TestTetheringResult result) throws Exception { private void runUnTether(final TestTetheringResult result) throws Exception {
when(mTethering.untether(TEST_IFACE_NAME)).thenReturn(TETHER_ERROR_NO_ERROR);
mTetheringConnector.untether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, mTetheringConnector.untether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG,
result); result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).untether(TEST_IFACE_NAME); verify(mTethering).untether(eq(TEST_IFACE_NAME), eq(result));
result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@Test @Test

View File

@@ -36,6 +36,7 @@ import static android.net.TetheringManager.ACTION_TETHER_STATE_CHANGED;
import static android.net.TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY; import static android.net.TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY;
import static android.net.TetheringManager.EXTRA_ACTIVE_TETHER; import static android.net.TetheringManager.EXTRA_ACTIVE_TETHER;
import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER; import static android.net.TetheringManager.EXTRA_AVAILABLE_TETHER;
import static android.net.TetheringManager.TETHERING_BLUETOOTH;
import static android.net.TetheringManager.TETHERING_ETHERNET; import static android.net.TetheringManager.TETHERING_ETHERNET;
import static android.net.TetheringManager.TETHERING_NCM; import static android.net.TetheringManager.TETHERING_NCM;
import static android.net.TetheringManager.TETHERING_USB; import static android.net.TetheringManager.TETHERING_USB;
@@ -93,6 +94,9 @@ import static org.mockito.Mockito.when;
import android.app.usage.NetworkStatsManager; import android.app.usage.NetworkStatsManager;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothProfile.ServiceListener;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
@@ -238,6 +242,8 @@ public class TetheringTest {
@Mock private TetheringNotificationUpdater mNotificationUpdater; @Mock private TetheringNotificationUpdater mNotificationUpdater;
@Mock private BpfCoordinator mBpfCoordinator; @Mock private BpfCoordinator mBpfCoordinator;
@Mock private PackageManager mPackageManager; @Mock private PackageManager mPackageManager;
@Mock private BluetoothAdapter mBluetoothAdapter;
@Mock private BluetoothPan mBluetoothPan;
private final MockIpServerDependencies mIpServerDependencies = private final MockIpServerDependencies mIpServerDependencies =
spy(new MockIpServerDependencies()); spy(new MockIpServerDependencies());
@@ -338,7 +344,8 @@ public class TetheringTest {
|| ifName.equals(TEST_MOBILE_IFNAME) || ifName.equals(TEST_MOBILE_IFNAME)
|| ifName.equals(TEST_P2P_IFNAME) || ifName.equals(TEST_P2P_IFNAME)
|| ifName.equals(TEST_NCM_IFNAME) || ifName.equals(TEST_NCM_IFNAME)
|| ifName.equals(TEST_ETH_IFNAME)); || ifName.equals(TEST_ETH_IFNAME)
|| ifName.equals(TEST_BT_IFNAME));
final String[] ifaces = new String[] { final String[] ifaces = new String[] {
TEST_USB_IFNAME, TEST_WLAN_IFNAME, TEST_WIFI_IFNAME, TEST_MOBILE_IFNAME, TEST_USB_IFNAME, TEST_WLAN_IFNAME, TEST_WIFI_IFNAME, TEST_MOBILE_IFNAME,
TEST_P2P_IFNAME, TEST_NCM_IFNAME, TEST_ETH_IFNAME}; TEST_P2P_IFNAME, TEST_NCM_IFNAME, TEST_ETH_IFNAME};
@@ -478,8 +485,7 @@ public class TetheringTest {
@Override @Override
public BluetoothAdapter getBluetoothAdapter() { public BluetoothAdapter getBluetoothAdapter() {
// TODO: add test for bluetooth tethering. return mBluetoothAdapter;
return null;
} }
@Override @Override
@@ -610,7 +616,7 @@ public class TetheringTest {
when(mNetd.interfaceGetList()) when(mNetd.interfaceGetList())
.thenReturn(new String[] { .thenReturn(new String[] {
TEST_MOBILE_IFNAME, TEST_WLAN_IFNAME, TEST_USB_IFNAME, TEST_P2P_IFNAME, TEST_MOBILE_IFNAME, TEST_WLAN_IFNAME, TEST_USB_IFNAME, TEST_P2P_IFNAME,
TEST_NCM_IFNAME, TEST_ETH_IFNAME}); TEST_NCM_IFNAME, TEST_ETH_IFNAME, TEST_BT_IFNAME});
when(mResources.getString(R.string.config_wifi_tether_enable)).thenReturn(""); when(mResources.getString(R.string.config_wifi_tether_enable)).thenReturn("");
mInterfaceConfiguration = new InterfaceConfigurationParcel(); mInterfaceConfiguration = new InterfaceConfigurationParcel();
mInterfaceConfiguration.flags = new String[0]; mInterfaceConfiguration.flags = new String[0];
@@ -678,7 +684,7 @@ public class TetheringTest {
when(mResources.getStringArray(R.array.config_tether_wifi_p2p_regexs)) when(mResources.getStringArray(R.array.config_tether_wifi_p2p_regexs))
.thenReturn(new String[] { "test_p2p-p2p\\d-.*" }); .thenReturn(new String[] { "test_p2p-p2p\\d-.*" });
when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs)) when(mResources.getStringArray(R.array.config_tether_bluetooth_regexs))
.thenReturn(new String[0]); .thenReturn(new String[] { "test_pan\\d" });
when(mResources.getStringArray(R.array.config_tether_ncm_regexs)) when(mResources.getStringArray(R.array.config_tether_ncm_regexs))
.thenReturn(new String[] { "test_ncm\\d" }); .thenReturn(new String[] { "test_ncm\\d" });
when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn(new int[0]); when(mResources.getIntArray(R.array.config_tether_upstream_types)).thenReturn(new int[0]);
@@ -2365,6 +2371,70 @@ public class TetheringTest {
return lease; return lease;
} }
@Test
public void testBluetoothTethering() throws Exception {
final ResultListener result = new ResultListener(TETHER_ERROR_NO_ERROR);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
mTethering.startTethering(createTetheringRequestParcel(TETHERING_BLUETOOTH), result);
mLooper.dispatchAll();
verifySetBluetoothTethering(true);
result.assertHasResult();
mTethering.interfaceAdded(TEST_BT_IFNAME);
mLooper.dispatchAll();
mTethering.interfaceStatusChanged(TEST_BT_IFNAME, false);
mTethering.interfaceStatusChanged(TEST_BT_IFNAME, true);
final ResultListener tetherResult = new ResultListener(TETHER_ERROR_NO_ERROR);
mTethering.tether(TEST_BT_IFNAME, tetherResult);
mLooper.dispatchAll();
tetherResult.assertHasResult();
verify(mNetd).tetherInterfaceAdd(TEST_BT_IFNAME);
verify(mNetd).networkAddInterface(INetd.LOCAL_NET_ID, TEST_BT_IFNAME);
verify(mNetd, times(2)).networkAddRoute(eq(INetd.LOCAL_NET_ID), eq(TEST_BT_IFNAME),
anyString(), anyString());
verify(mNetd).ipfwdEnableForwarding(TETHERING_NAME);
verify(mNetd).tetherStartWithConfiguration(any());
verify(mNetd, times(2)).networkAddRoute(eq(INetd.LOCAL_NET_ID), eq(TEST_BT_IFNAME),
anyString(), anyString());
verifyNoMoreInteractions(mNetd);
reset(mNetd);
when(mBluetoothAdapter.isEnabled()).thenReturn(true);
mTethering.stopTethering(TETHERING_BLUETOOTH);
mLooper.dispatchAll();
final ResultListener untetherResult = new ResultListener(TETHER_ERROR_NO_ERROR);
mTethering.untether(TEST_BT_IFNAME, untetherResult);
mLooper.dispatchAll();
untetherResult.assertHasResult();
verifySetBluetoothTethering(false);
verify(mNetd).tetherApplyDnsInterfaces();
verify(mNetd).tetherInterfaceRemove(TEST_BT_IFNAME);
verify(mNetd).networkRemoveInterface(INetd.LOCAL_NET_ID, TEST_BT_IFNAME);
verify(mNetd).interfaceSetCfg(any(InterfaceConfigurationParcel.class));
verify(mNetd).tetherStop();
verify(mNetd).ipfwdDisableForwarding(TETHERING_NAME);
verifyNoMoreInteractions(mNetd);
}
private void verifySetBluetoothTethering(final boolean enable) {
final ArgumentCaptor<ServiceListener> listenerCaptor =
ArgumentCaptor.forClass(ServiceListener.class);
verify(mBluetoothAdapter).isEnabled();
verify(mBluetoothAdapter).getProfileProxy(eq(mServiceContext), listenerCaptor.capture(),
eq(BluetoothProfile.PAN));
final ServiceListener listener = listenerCaptor.getValue();
when(mBluetoothPan.isTetheringOn()).thenReturn(enable);
listener.onServiceConnected(BluetoothProfile.PAN, mBluetoothPan);
verify(mBluetoothPan).setBluetoothTethering(enable);
verify(mBluetoothPan).isTetheringOn();
verify(mBluetoothAdapter).closeProfileProxy(eq(BluetoothProfile.PAN), eq(mBluetoothPan));
verifyNoMoreInteractions(mBluetoothAdapter, mBluetoothPan);
reset(mBluetoothAdapter, mBluetoothPan);
}
// TODO: Test that a request for hotspot mode doesn't interfere with an // TODO: Test that a request for hotspot mode doesn't interfere with an
// already operating tethering mode interface. // already operating tethering mode interface.
} }