diff --git a/Tethering/src/com/android/networkstack/tethering/Tethering.java b/Tethering/src/com/android/networkstack/tethering/Tethering.java index 47b1bd751d..997acd00ab 100644 --- a/Tethering/src/com/android/networkstack/tethering/Tethering.java +++ b/Tethering/src/com/android/networkstack/tethering/Tethering.java @@ -2525,7 +2525,7 @@ public class Tethering { // if ro.tether.denied = true we default to no tethering // gservices could set the secure setting to 1 though to enable it on a build where it // had previously been turned off. - private boolean isTetheringAllowed() { + boolean isTetheringAllowed() { final int defaultVal = mDeps.isTetheringDenied() ? 0 : 1; final boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.TETHER_SUPPORTED, defaultVal) != 0; diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringService.java b/Tethering/src/com/android/networkstack/tethering/TetheringService.java index f147e1036d..96ddfa01c4 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringService.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringService.java @@ -237,7 +237,7 @@ public class TetheringService extends Service { listener.onResult(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION); return true; } - if (!mTethering.isTetheringSupported()) { + if (!mTethering.isTetheringSupported() || !mTethering.isTetheringAllowed()) { listener.onResult(TETHER_ERROR_UNSUPPORTED); return true; } @@ -255,7 +255,7 @@ public class TetheringService extends Service { receiver.send(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION, null); return true; } - if (!mTethering.isTetheringSupported()) { + if (!mTethering.isTetheringSupported() || !mTethering.isTetheringAllowed()) { receiver.send(TETHER_ERROR_UNSUPPORTED, null); return true; } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java index 9db8f1687e..e114cb5aa7 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringServiceTest.java @@ -24,6 +24,7 @@ import static android.net.TetheringManager.TETHERING_WIFI; import static android.net.TetheringManager.TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION; import static android.net.TetheringManager.TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION; import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR; +import static android.net.TetheringManager.TETHER_ERROR_UNSUPPORTED; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -139,23 +140,27 @@ public final class TetheringServiceTest { } private void runAsNoPermission(final TestTetheringCall test) throws Exception { - runTetheringCall(test, new String[0]); + runTetheringCall(test, true /* isTetheringAllowed */, new String[0]); } private void runAsTetherPrivileged(final TestTetheringCall test) throws Exception { - runTetheringCall(test, TETHER_PRIVILEGED); + runTetheringCall(test, true /* isTetheringAllowed */, TETHER_PRIVILEGED); } private void runAsAccessNetworkState(final TestTetheringCall test) throws Exception { - runTetheringCall(test, ACCESS_NETWORK_STATE); + runTetheringCall(test, true /* isTetheringAllowed */, ACCESS_NETWORK_STATE); } private void runAsWriteSettings(final TestTetheringCall test) throws Exception { - runTetheringCall(test, WRITE_SETTINGS); + runTetheringCall(test, true /* isTetheringAllowed */, WRITE_SETTINGS); } - private void runTetheringCall(final TestTetheringCall test, String... permissions) - throws Exception { + private void runAsTetheringDisallowed(final TestTetheringCall test) throws Exception { + runTetheringCall(test, false /* isTetheringAllowed */, TETHER_PRIVILEGED); + } + + private void runTetheringCall(final TestTetheringCall test, boolean isTetheringAllowed, + String... permissions) throws Exception { // Allow the test to run even if ACCESS_NETWORK_STATE was granted at the APK level if (!CollectionUtils.contains(permissions, ACCESS_NETWORK_STATE)) { mMockConnector.setPermission(ACCESS_NETWORK_STATE, PERMISSION_DENIED); @@ -164,6 +169,7 @@ public final class TetheringServiceTest { if (permissions.length > 0) mUiAutomation.adoptShellPermissionIdentity(permissions); try { when(mTethering.isTetheringSupported()).thenReturn(true); + when(mTethering.isTetheringAllowed()).thenReturn(isTetheringAllowed); test.runTetheringCall(new TestTetheringResult()); } finally { mUiAutomation.dropShellPermissionIdentity(); @@ -180,6 +186,7 @@ public final class TetheringServiceTest { private void runTether(final TestTetheringResult result) throws Exception { mTetheringConnector.tether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).tether(TEST_IFACE_NAME, IpServer.STATE_TETHERED, result); } @@ -203,12 +210,22 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.tether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, + result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runUnTether(final TestTetheringResult result) throws Exception { mTetheringConnector.untether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).untether(eq(TEST_IFACE_NAME), eq(result)); } @@ -232,6 +249,15 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.untether(TEST_IFACE_NAME, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, + result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runSetUsbTethering(final TestTetheringResult result) throws Exception { @@ -243,6 +269,7 @@ public final class TetheringServiceTest { mTetheringConnector.setUsbTethering(true /* enable */, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).setUsbTethering(eq(true) /* enable */, any(IIntResultListener.class)); result.assertResult(TETHER_ERROR_NO_ERROR); } @@ -268,6 +295,14 @@ public final class TetheringServiceTest { verifyNoMoreInteractionsForTethering(); }); + runAsTetheringDisallowed((result) -> { + mTetheringConnector.setUsbTethering(true /* enable */, TEST_CALLER_PKG, + TEST_ATTRIBUTION_TAG, result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runStartTethering(final TestTetheringResult result, @@ -275,6 +310,7 @@ public final class TetheringServiceTest { mTetheringConnector.startTethering(request, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).startTethering(eq(request), eq(TEST_CALLER_PKG), eq(result)); } @@ -301,6 +337,15 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.startTethering(request, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, + result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runStartTetheringAndVerifyNoPermission(final TestTetheringResult result) @@ -337,6 +382,7 @@ public final class TetheringServiceTest { mTetheringConnector.stopTethering(TETHERING_WIFI, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).stopTethering(TETHERING_WIFI); result.assertResult(TETHER_ERROR_NO_ERROR); } @@ -361,6 +407,15 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.stopTethering(TETHERING_WIFI, TEST_CALLER_PKG, + TEST_ATTRIBUTION_TAG, result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runRequestLatestTetheringEntitlementResult() throws Exception { @@ -368,6 +423,7 @@ public final class TetheringServiceTest { mTetheringConnector.requestLatestTetheringEntitlementResult(TETHERING_WIFI, result, true /* showEntitlementUi */, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).requestLatestTetheringEntitlementResult(eq(TETHERING_WIFI), eq(result), eq(true) /* showEntitlementUi */); } @@ -392,6 +448,16 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((none) -> { + final MyResultReceiver receiver = new MyResultReceiver(null); + mTetheringConnector.requestLatestTetheringEntitlementResult(TETHERING_WIFI, receiver, + true /* showEntitlementUi */, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + receiver.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runRegisterTetheringEventCallback() throws Exception { @@ -419,6 +485,12 @@ public final class TetheringServiceTest { runRegisterTetheringEventCallback(); verifyNoMoreInteractionsForTethering(); }); + + // should still be able to register callback even tethering is restricted. + runAsTetheringDisallowed((result) -> { + runRegisterTetheringEventCallback(); + verifyNoMoreInteractionsForTethering(); + }); } private void runUnregisterTetheringEventCallback() throws Exception { @@ -446,11 +518,19 @@ public final class TetheringServiceTest { runUnregisterTetheringEventCallback(); verifyNoMoreInteractionsForTethering(); }); + + // should still be able to unregister callback even tethering is restricted. + runAsTetheringDisallowed((result) -> { + runUnregisterTetheringEventCallback(); + verifyNoMoreInteractionsForTethering(); + }); + } private void runStopAllTethering(final TestTetheringResult result) throws Exception { mTetheringConnector.stopAllTethering(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); verify(mTethering).untetherAll(); result.assertResult(TETHER_ERROR_NO_ERROR); } @@ -474,11 +554,20 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.stopAllTethering(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private void runIsTetheringSupported(final TestTetheringResult result) throws Exception { mTetheringConnector.isTetheringSupported(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); result.assertResult(TETHER_ERROR_NO_ERROR); } @@ -502,6 +591,15 @@ public final class TetheringServiceTest { verify(mTethering).isTetherProvisioningRequired(); verifyNoMoreInteractionsForTethering(); }); + + runAsTetheringDisallowed((result) -> { + mTetheringConnector.isTetheringSupported(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, + result); + verify(mTethering).isTetheringSupported(); + verify(mTethering).isTetheringAllowed(); + result.assertResult(TETHER_ERROR_UNSUPPORTED); + verifyNoMoreInteractionsForTethering(); + }); } private class ConnectorSupplier implements Supplier {