[RFPM02] Add Dependencies class for injection in tests.
Add Dependencies class in PermissionMonitor for injection in tests easily. Bug: 132784544 Test: atests FrameworksNetTests Change-Id: Ibb84d548908f7a955fa0ff206794486733259978
This commit is contained in:
@@ -82,6 +82,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
|
|||||||
private final PackageManager mPackageManager;
|
private final PackageManager mPackageManager;
|
||||||
private final UserManager mUserManager;
|
private final UserManager mUserManager;
|
||||||
private final INetd mNetd;
|
private final INetd mNetd;
|
||||||
|
private final Dependencies mDeps;
|
||||||
|
|
||||||
// Values are User IDs.
|
// Values are User IDs.
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
@@ -102,10 +103,30 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
|
|||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
private final Set<Integer> mAllApps = new HashSet<>();
|
private final Set<Integer> mAllApps = new HashSet<>();
|
||||||
|
|
||||||
public PermissionMonitor(Context context, INetd netd) {
|
/**
|
||||||
|
* Dependencies of PermissionMonitor, for injection in tests.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
public static class Dependencies {
|
||||||
|
/**
|
||||||
|
* Get device first sdk version.
|
||||||
|
*/
|
||||||
|
public int getDeviceFirstSdkInt() {
|
||||||
|
return Build.VERSION.FIRST_SDK_INT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd) {
|
||||||
|
this(context, netd, new Dependencies());
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd,
|
||||||
|
@NonNull final Dependencies deps) {
|
||||||
mPackageManager = context.getPackageManager();
|
mPackageManager = context.getPackageManager();
|
||||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||||
mNetd = netd;
|
mNetd = netd;
|
||||||
|
mDeps = deps;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intended to be called only once at startup, after the system is ready. Installs a broadcast
|
// Intended to be called only once at startup, after the system is ready. Installs a broadcast
|
||||||
@@ -185,11 +206,6 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
|
|||||||
return appInfo.isVendor() || appInfo.isOem() || appInfo.isProduct();
|
return appInfo.isVendor() || appInfo.isOem() || appInfo.isProduct();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
protected int getDeviceFirstSdkInt() {
|
|
||||||
return Build.VERSION.FIRST_SDK_INT;
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
boolean hasPermission(@NonNull final PackageInfo app, @NonNull final String permission) {
|
boolean hasPermission(@NonNull final PackageInfo app, @NonNull final String permission) {
|
||||||
if (app.requestedPermissions == null || app.requestedPermissionsFlags == null) {
|
if (app.requestedPermissions == null || app.requestedPermissionsFlags == null) {
|
||||||
@@ -212,7 +228,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
|
|||||||
if (app.applicationInfo != null) {
|
if (app.applicationInfo != null) {
|
||||||
// Backward compatibility for b/114245686, on devices that launched before Q daemons
|
// Backward compatibility for b/114245686, on devices that launched before Q daemons
|
||||||
// and apps running as the system UID are exempted from this check.
|
// and apps running as the system UID are exempted from this check.
|
||||||
if (app.applicationInfo.uid == SYSTEM_UID && getDeviceFirstSdkInt() < VERSION_Q) {
|
if (app.applicationInfo.uid == SYSTEM_UID && mDeps.getDeviceFirstSdkInt() < VERSION_Q) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ public class PermissionMonitorTest {
|
|||||||
@Mock private INetd mNetdService;
|
@Mock private INetd mNetdService;
|
||||||
@Mock private PackageManagerInternal mMockPmi;
|
@Mock private PackageManagerInternal mMockPmi;
|
||||||
@Mock private UserManager mUserManager;
|
@Mock private UserManager mUserManager;
|
||||||
|
@Mock private PermissionMonitor.Dependencies mDeps;
|
||||||
|
|
||||||
private PermissionMonitor mPermissionMonitor;
|
private PermissionMonitor mPermissionMonitor;
|
||||||
|
|
||||||
@@ -128,7 +129,7 @@ public class PermissionMonitorTest {
|
|||||||
new UserInfo(MOCK_USER2, "", 0),
|
new UserInfo(MOCK_USER2, "", 0),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService));
|
mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService, mDeps));
|
||||||
|
|
||||||
LocalServices.removeServiceForTest(PackageManagerInternal.class);
|
LocalServices.removeServiceForTest(PackageManagerInternal.class);
|
||||||
LocalServices.addService(PackageManagerInternal.class, mMockPmi);
|
LocalServices.addService(PackageManagerInternal.class, mMockPmi);
|
||||||
@@ -283,14 +284,14 @@ public class PermissionMonitorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testHasRestrictedNetworkPermissionSystemUid() {
|
public void testHasRestrictedNetworkPermissionSystemUid() {
|
||||||
doReturn(VERSION_P).when(mPermissionMonitor).getDeviceFirstSdkInt();
|
doReturn(VERSION_P).when(mDeps).getDeviceFirstSdkInt();
|
||||||
assertTrue(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_P, SYSTEM_UID));
|
assertTrue(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_P, SYSTEM_UID));
|
||||||
assertTrue(hasRestrictedNetworkPermission(
|
assertTrue(hasRestrictedNetworkPermission(
|
||||||
PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_INTERNAL));
|
PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_INTERNAL));
|
||||||
assertTrue(hasRestrictedNetworkPermission(
|
assertTrue(hasRestrictedNetworkPermission(
|
||||||
PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_USE_RESTRICTED_NETWORKS));
|
PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_USE_RESTRICTED_NETWORKS));
|
||||||
|
|
||||||
doReturn(VERSION_Q).when(mPermissionMonitor).getDeviceFirstSdkInt();
|
doReturn(VERSION_Q).when(mDeps).getDeviceFirstSdkInt();
|
||||||
assertFalse(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID));
|
assertFalse(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID));
|
||||||
assertFalse(hasRestrictedNetworkPermission(
|
assertFalse(hasRestrictedNetworkPermission(
|
||||||
PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID, CONNECTIVITY_INTERNAL));
|
PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID, CONNECTIVITY_INTERNAL));
|
||||||
|
|||||||
Reference in New Issue
Block a user