Skip to content

Commit

Permalink
binder/test: Stop running Robolectric tests on APIs < 19 (grpc#10385)
Browse files Browse the repository at this point in the history
This is a port of internal cl/547560711, but with a change to call
isDeviceOwner that requires sdk 18 unconditioally
  • Loading branch information
zhangkun83 authored Jul 26, 2023
1 parent 41552bf commit c5c37ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
7 changes: 4 additions & 3 deletions binder/src/main/java/io/grpc/binder/SecurityPolicies.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Process;
import androidx.annotation.RequiresApi;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -188,12 +189,12 @@ public Status checkAuthorization(int uid) {
* Creates {@link SecurityPolicy} which checks if the app is a device owner app. See
* {@link DevicePolicyManager}.
*/
public static SecurityPolicy isDeviceOwner(Context applicationContext) {
@androidx.annotation.RequiresApi(18)
public static io.grpc.binder.SecurityPolicy isDeviceOwner(Context applicationContext) {
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) applicationContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
return anyPackageWithUidSatisfies(
applicationContext,
pkg -> VERSION.SDK_INT >= 18 && devicePolicyManager.isDeviceOwnerApp(pkg),
applicationContext, pkg -> devicePolicyManager.isDeviceOwnerApp(pkg),
"Rejected by device owner policy. No packages found for UID.",
"Rejected by device owner policy");
}
Expand Down
21 changes: 3 additions & 18 deletions binder/src/test/java/io/grpc/binder/SecurityPoliciesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Process;
import androidx.test.core.app.ApplicationProvider;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -333,7 +330,7 @@ public void testHasPermissions_failsIfPackageDoesNotHavePermissions() throws Exc
}

@Test
@Config(sdk = 18)
@Config(sdk = 19)
public void testIsDeviceOwner_succeedsForDeviceOwner() throws Exception {
PackageInfo info =
newBuilder().setPackageName(OTHER_UID_PACKAGE_NAME).setSignatures(SIG2).build();
Expand All @@ -348,7 +345,7 @@ public void testIsDeviceOwner_succeedsForDeviceOwner() throws Exception {
}

@Test
@Config(sdk = 18)
@Config(sdk = 19)
public void testIsDeviceOwner_failsForNotDeviceOwner() throws Exception {
PackageInfo info =
newBuilder().setPackageName(OTHER_UID_PACKAGE_NAME).setSignatures(SIG2).build();
Expand All @@ -361,25 +358,13 @@ public void testIsDeviceOwner_failsForNotDeviceOwner() throws Exception {
}

@Test
@Config(sdk = 18)
@Config(sdk = 19)
public void testIsDeviceOwner_failsWhenNoPackagesForUid() throws Exception {
policy = SecurityPolicies.isDeviceOwner(appContext);

assertThat(policy.checkAuthorization(OTHER_UID).getCode()).isEqualTo(Status.UNAUTHENTICATED.getCode());
}

@Test
@Config(sdk = 17)
public void testIsDeviceOwner_failsForSdkLevelTooLow() throws Exception {
PackageInfo info =
newBuilder().setPackageName(OTHER_UID_PACKAGE_NAME).setSignatures(SIG2).build();

installPackages(OTHER_UID, info);

policy = SecurityPolicies.isDeviceOwner(appContext);

assertThat(policy.checkAuthorization(OTHER_UID).getCode()).isEqualTo(Status.PERMISSION_DENIED.getCode());
}

@Test
@Config(sdk = 21)
Expand Down

0 comments on commit c5c37ac

Please sign in to comment.