Merge "Allow mocking permission in TetheringServiceTest"
This commit is contained in:
@@ -25,12 +25,14 @@ import android.content.Intent;
|
|||||||
import android.net.ITetheringConnector;
|
import android.net.ITetheringConnector;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.util.ArrayMap;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
public class MockTetheringService extends TetheringService {
|
public class MockTetheringService extends TetheringService {
|
||||||
private final Tethering mTethering = mock(Tethering.class);
|
private final Tethering mTethering = mock(Tethering.class);
|
||||||
|
private final ArrayMap<String, Integer> mMockedPermissions = new ArrayMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
@@ -51,6 +53,15 @@ public class MockTetheringService extends TetheringService {
|
|||||||
return context.checkCallingOrSelfPermission(WRITE_SETTINGS) == PERMISSION_GRANTED;
|
return context.checkCallingOrSelfPermission(WRITE_SETTINGS) == PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int checkCallingOrSelfPermission(String permission) {
|
||||||
|
final Integer mocked = mMockedPermissions.getOrDefault(permission, null);
|
||||||
|
if (mocked != null) {
|
||||||
|
return mocked;
|
||||||
|
}
|
||||||
|
return super.checkCallingOrSelfPermission(permission);
|
||||||
|
}
|
||||||
|
|
||||||
public Tethering getTethering() {
|
public Tethering getTethering() {
|
||||||
return mTethering;
|
return mTethering;
|
||||||
}
|
}
|
||||||
@@ -68,5 +79,18 @@ public class MockTetheringService extends TetheringService {
|
|||||||
public MockTetheringService getService() {
|
public MockTetheringService getService() {
|
||||||
return MockTetheringService.this;
|
return MockTetheringService.this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock a permission
|
||||||
|
* @param permission Permission to mock
|
||||||
|
* @param granted One of PackageManager.PERMISSION_*, or null to reset to default behavior
|
||||||
|
*/
|
||||||
|
public void setPermission(String permission, Integer granted) {
|
||||||
|
if (granted == null) {
|
||||||
|
mMockedPermissions.remove(permission);
|
||||||
|
} else {
|
||||||
|
mMockedPermissions.put(permission, granted);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ package com.android.networkstack.tethering;
|
|||||||
import static android.Manifest.permission.ACCESS_NETWORK_STATE;
|
import static android.Manifest.permission.ACCESS_NETWORK_STATE;
|
||||||
import static android.Manifest.permission.TETHER_PRIVILEGED;
|
import static android.Manifest.permission.TETHER_PRIVILEGED;
|
||||||
import static android.Manifest.permission.WRITE_SETTINGS;
|
import static android.Manifest.permission.WRITE_SETTINGS;
|
||||||
|
import static android.content.pm.PackageManager.PERMISSION_DENIED;
|
||||||
import static android.net.TetheringManager.TETHERING_WIFI;
|
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;
|
||||||
@@ -50,6 +51,7 @@ import androidx.test.filters.SmallTest;
|
|||||||
import androidx.test.rule.ServiceTestRule;
|
import androidx.test.rule.ServiceTestRule;
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
|
|
||||||
|
import com.android.net.module.util.CollectionUtils;
|
||||||
import com.android.networkstack.tethering.MockTetheringService.MockTetheringConnector;
|
import com.android.networkstack.tethering.MockTetheringService.MockTetheringConnector;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@@ -70,6 +72,7 @@ public final class TetheringServiceTest {
|
|||||||
@Rule public ServiceTestRule mServiceTestRule;
|
@Rule public ServiceTestRule mServiceTestRule;
|
||||||
private Tethering mTethering;
|
private Tethering mTethering;
|
||||||
private Intent mMockServiceIntent;
|
private Intent mMockServiceIntent;
|
||||||
|
private MockTetheringConnector mMockConnector;
|
||||||
private ITetheringConnector mTetheringConnector;
|
private ITetheringConnector mTetheringConnector;
|
||||||
private UiAutomation mUiAutomation;
|
private UiAutomation mUiAutomation;
|
||||||
|
|
||||||
@@ -109,10 +112,9 @@ public final class TetheringServiceTest {
|
|||||||
mMockServiceIntent = new Intent(
|
mMockServiceIntent = new Intent(
|
||||||
InstrumentationRegistry.getTargetContext(),
|
InstrumentationRegistry.getTargetContext(),
|
||||||
MockTetheringService.class);
|
MockTetheringService.class);
|
||||||
final MockTetheringConnector mockConnector =
|
mMockConnector = (MockTetheringConnector) mServiceTestRule.bindService(mMockServiceIntent);
|
||||||
(MockTetheringConnector) mServiceTestRule.bindService(mMockServiceIntent);
|
mTetheringConnector = mMockConnector.getTetheringConnector();
|
||||||
mTetheringConnector = mockConnector.getTetheringConnector();
|
final MockTetheringService service = mMockConnector.getService();
|
||||||
final MockTetheringService service = mockConnector.getService();
|
|
||||||
mTethering = service.getTethering();
|
mTethering = service.getTethering();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,12 +146,18 @@ public final class TetheringServiceTest {
|
|||||||
|
|
||||||
private void runTetheringCall(final TestTetheringCall test, String... permissions)
|
private void runTetheringCall(final TestTetheringCall test, String... permissions)
|
||||||
throws Exception {
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
test.runTetheringCall(new TestTetheringResult());
|
test.runTetheringCall(new TestTetheringResult());
|
||||||
} finally {
|
} finally {
|
||||||
mUiAutomation.dropShellPermissionIdentity();
|
mUiAutomation.dropShellPermissionIdentity();
|
||||||
|
mMockConnector.setPermission(ACCESS_NETWORK_STATE, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user