Make tethering APIs unsupported synchronously when disallowed

To backward compatible existing behavior, make tethering APIs
unsupported synchronously when tethering is disallowed. There
are two APIs register/unregisterTetheringEventCallback still
supported even tethering is disallowed.
This could avoid some existing tests flaky. The test assume
startTethering would fail right away after tethering restricted
apply.

Bug: 184996041
Bug: 239500515
Test: atest TetheringTests
Change-Id: I41ae1d61647c9baf69bcb246965e8d9b4a89b497
This commit is contained in:
Mark
2022-07-27 08:27:06 +00:00
parent 874a3e273e
commit cf230301e3
3 changed files with 107 additions and 9 deletions

View File

@@ -2525,7 +2525,7 @@ public class Tethering {
// if ro.tether.denied = true we default to no 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 // gservices could set the secure setting to 1 though to enable it on a build where it
// had previously been turned off. // had previously been turned off.
private boolean isTetheringAllowed() { boolean isTetheringAllowed() {
final int defaultVal = mDeps.isTetheringDenied() ? 0 : 1; final int defaultVal = mDeps.isTetheringDenied() ? 0 : 1;
final boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(), final boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.TETHER_SUPPORTED, defaultVal) != 0; Settings.Global.TETHER_SUPPORTED, defaultVal) != 0;

View File

@@ -237,7 +237,7 @@ public class TetheringService extends Service {
listener.onResult(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION); listener.onResult(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION);
return true; return true;
} }
if (!mTethering.isTetheringSupported()) { if (!mTethering.isTetheringSupported() || !mTethering.isTetheringAllowed()) {
listener.onResult(TETHER_ERROR_UNSUPPORTED); listener.onResult(TETHER_ERROR_UNSUPPORTED);
return true; return true;
} }
@@ -255,7 +255,7 @@ public class TetheringService extends Service {
receiver.send(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION, null); receiver.send(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION, null);
return true; return true;
} }
if (!mTethering.isTetheringSupported()) { if (!mTethering.isTetheringSupported() || !mTethering.isTetheringAllowed()) {
receiver.send(TETHER_ERROR_UNSUPPORTED, null); receiver.send(TETHER_ERROR_UNSUPPORTED, null);
return true; return true;
} }

View File

@@ -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_ACCESS_TETHERING_PERMISSION;
import static android.net.TetheringManager.TETHER_ERROR_NO_CHANGE_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_NO_ERROR;
import static android.net.TetheringManager.TETHER_ERROR_UNSUPPORTED;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@@ -139,23 +140,27 @@ public final class TetheringServiceTest {
} }
private void runAsNoPermission(final TestTetheringCall test) throws Exception { 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 { 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 { 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 { 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) private void runAsTetheringDisallowed(final TestTetheringCall test) throws Exception {
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 // Allow the test to run even if ACCESS_NETWORK_STATE was granted at the APK level
if (!CollectionUtils.contains(permissions, ACCESS_NETWORK_STATE)) { if (!CollectionUtils.contains(permissions, ACCESS_NETWORK_STATE)) {
mMockConnector.setPermission(ACCESS_NETWORK_STATE, PERMISSION_DENIED); mMockConnector.setPermission(ACCESS_NETWORK_STATE, PERMISSION_DENIED);
@@ -164,6 +169,7 @@ public final class TetheringServiceTest {
if (permissions.length > 0) mUiAutomation.adoptShellPermissionIdentity(permissions); if (permissions.length > 0) mUiAutomation.adoptShellPermissionIdentity(permissions);
try { try {
when(mTethering.isTetheringSupported()).thenReturn(true); when(mTethering.isTetheringSupported()).thenReturn(true);
when(mTethering.isTetheringAllowed()).thenReturn(isTetheringAllowed);
test.runTetheringCall(new TestTetheringResult()); test.runTetheringCall(new TestTetheringResult());
} finally { } finally {
mUiAutomation.dropShellPermissionIdentity(); mUiAutomation.dropShellPermissionIdentity();
@@ -180,6 +186,7 @@ public final class TetheringServiceTest {
private void runTether(final TestTetheringResult result) throws Exception { private void runTether(final TestTetheringResult result) throws Exception {
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).isTetheringAllowed();
verify(mTethering).tether(TEST_IFACE_NAME, IpServer.STATE_TETHERED, result); verify(mTethering).tether(TEST_IFACE_NAME, IpServer.STATE_TETHERED, result);
} }
@@ -203,12 +210,22 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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 { private void runUnTether(final TestTetheringResult result) throws Exception {
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).isTetheringAllowed();
verify(mTethering).untether(eq(TEST_IFACE_NAME), eq(result)); verify(mTethering).untether(eq(TEST_IFACE_NAME), eq(result));
} }
@@ -232,6 +249,15 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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 { private void runSetUsbTethering(final TestTetheringResult result) throws Exception {
@@ -243,6 +269,7 @@ public final class TetheringServiceTest {
mTetheringConnector.setUsbTethering(true /* enable */, TEST_CALLER_PKG, mTetheringConnector.setUsbTethering(true /* enable */, TEST_CALLER_PKG,
TEST_ATTRIBUTION_TAG, result); TEST_ATTRIBUTION_TAG, result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
verify(mTethering).setUsbTethering(eq(true) /* enable */, any(IIntResultListener.class)); verify(mTethering).setUsbTethering(eq(true) /* enable */, any(IIntResultListener.class));
result.assertResult(TETHER_ERROR_NO_ERROR); result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@@ -268,6 +295,14 @@ public final class TetheringServiceTest {
verifyNoMoreInteractionsForTethering(); 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, private void runStartTethering(final TestTetheringResult result,
@@ -275,6 +310,7 @@ public final class TetheringServiceTest {
mTetheringConnector.startTethering(request, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, mTetheringConnector.startTethering(request, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG,
result); result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
verify(mTethering).startTethering(eq(request), eq(TEST_CALLER_PKG), eq(result)); verify(mTethering).startTethering(eq(request), eq(TEST_CALLER_PKG), eq(result));
} }
@@ -301,6 +337,15 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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) private void runStartTetheringAndVerifyNoPermission(final TestTetheringResult result)
@@ -337,6 +382,7 @@ public final class TetheringServiceTest {
mTetheringConnector.stopTethering(TETHERING_WIFI, TEST_CALLER_PKG, mTetheringConnector.stopTethering(TETHERING_WIFI, TEST_CALLER_PKG,
TEST_ATTRIBUTION_TAG, result); TEST_ATTRIBUTION_TAG, result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
verify(mTethering).stopTethering(TETHERING_WIFI); verify(mTethering).stopTethering(TETHERING_WIFI);
result.assertResult(TETHER_ERROR_NO_ERROR); result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@@ -361,6 +407,15 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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 { private void runRequestLatestTetheringEntitlementResult() throws Exception {
@@ -368,6 +423,7 @@ public final class TetheringServiceTest {
mTetheringConnector.requestLatestTetheringEntitlementResult(TETHERING_WIFI, result, mTetheringConnector.requestLatestTetheringEntitlementResult(TETHERING_WIFI, result,
true /* showEntitlementUi */, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG); true /* showEntitlementUi */, TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
verify(mTethering).requestLatestTetheringEntitlementResult(eq(TETHERING_WIFI), verify(mTethering).requestLatestTetheringEntitlementResult(eq(TETHERING_WIFI),
eq(result), eq(true) /* showEntitlementUi */); eq(result), eq(true) /* showEntitlementUi */);
} }
@@ -392,6 +448,16 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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 { private void runRegisterTetheringEventCallback() throws Exception {
@@ -419,6 +485,12 @@ public final class TetheringServiceTest {
runRegisterTetheringEventCallback(); runRegisterTetheringEventCallback();
verifyNoMoreInteractionsForTethering(); verifyNoMoreInteractionsForTethering();
}); });
// should still be able to register callback even tethering is restricted.
runAsTetheringDisallowed((result) -> {
runRegisterTetheringEventCallback();
verifyNoMoreInteractionsForTethering();
});
} }
private void runUnregisterTetheringEventCallback() throws Exception { private void runUnregisterTetheringEventCallback() throws Exception {
@@ -446,11 +518,19 @@ public final class TetheringServiceTest {
runUnregisterTetheringEventCallback(); runUnregisterTetheringEventCallback();
verifyNoMoreInteractionsForTethering(); verifyNoMoreInteractionsForTethering();
}); });
// should still be able to unregister callback even tethering is restricted.
runAsTetheringDisallowed((result) -> {
runUnregisterTetheringEventCallback();
verifyNoMoreInteractionsForTethering();
});
} }
private void runStopAllTethering(final TestTetheringResult result) throws Exception { private void runStopAllTethering(final TestTetheringResult result) throws Exception {
mTetheringConnector.stopAllTethering(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); mTetheringConnector.stopAllTethering(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
verify(mTethering).untetherAll(); verify(mTethering).untetherAll();
result.assertResult(TETHER_ERROR_NO_ERROR); result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@@ -474,11 +554,20 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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 { private void runIsTetheringSupported(final TestTetheringResult result) throws Exception {
mTetheringConnector.isTetheringSupported(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result); mTetheringConnector.isTetheringSupported(TEST_CALLER_PKG, TEST_ATTRIBUTION_TAG, result);
verify(mTethering).isTetheringSupported(); verify(mTethering).isTetheringSupported();
verify(mTethering).isTetheringAllowed();
result.assertResult(TETHER_ERROR_NO_ERROR); result.assertResult(TETHER_ERROR_NO_ERROR);
} }
@@ -502,6 +591,15 @@ public final class TetheringServiceTest {
verify(mTethering).isTetherProvisioningRequired(); verify(mTethering).isTetherProvisioningRequired();
verifyNoMoreInteractionsForTethering(); 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<T> implements Supplier<T> { private class ConnectorSupplier<T> implements Supplier<T> {