summaryrefslogtreecommitdiff
path: root/common/framework/com
diff options
context:
space:
mode:
authorpaulhu <paulhu@google.com>2021-01-25 17:49:07 +0800
committerpaulhu <paulhu@google.com>2021-02-18 23:18:21 +0800
commit715c5327e41f5be8851c5c82f01a32e6b92e81a0 (patch)
treea1f89dfbb337c72b8739aab78b801a35f43caf81 /common/framework/com
parent46baaac36bdcf2408509c1918ad1fffb7b9c36d7 (diff)
downloadnet-715c5327e41f5be8851c5c82f01a32e6b92e81a0.tar.gz
Add checking NetworkStack permission methods and tests
These methods migrate from android.net.NetworkStack. checkNetworkStackPermission() checkNetworkStackPermissionOr() Bug: 178352309 Test: atest NetworkStaticLibTests Change-Id: Iba4daaac3c662b87fec038f7c557e4fd6544c069
Diffstat (limited to 'common/framework/com')
-rw-r--r--common/framework/com/android/net/module/util/PermissionUtils.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/common/framework/com/android/net/module/util/PermissionUtils.java b/common/framework/com/android/net/module/util/PermissionUtils.java
index ce8a7454..10eda576 100644
--- a/common/framework/com/android/net/module/util/PermissionUtils.java
+++ b/common/framework/com/android/net/module/util/PermissionUtils.java
@@ -16,11 +16,16 @@
package com.android.net.module.util;
+import static android.Manifest.permission.NETWORK_STACK;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
+import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
import android.annotation.NonNull;
import android.content.Context;
+import java.util.ArrayList;
+import java.util.Arrays;
+
/**
* Collection of permission utilities.
* @hide
@@ -49,4 +54,30 @@ public final class PermissionUtils {
+ String.join(", ", permissions) + ".");
}
}
+
+ /**
+ * If the NetworkStack, MAINLINE_NETWORK_STACK are not allowed for a particular process, throw a
+ * {@link SecurityException}.
+ *
+ * @param context {@link android.content.Context} for the process.
+ */
+ public static void enforceNetworkStackPermission(final @NonNull Context context) {
+ enforceNetworkStackPermissionOr(context);
+ }
+
+ /**
+ * If the NetworkStack, MAINLINE_NETWORK_STACK or other specified permissions are not allowed
+ * for a particular process, throw a {@link SecurityException}.
+ *
+ * @param context {@link android.content.Context} for the process.
+ * @param otherPermissions The set of permissions that could be the candidate permissions , or
+ * empty string if none of other permissions needed.
+ */
+ public static void enforceNetworkStackPermissionOr(final @NonNull Context context,
+ final @NonNull String... otherPermissions) {
+ ArrayList<String> permissions = new ArrayList<String>(Arrays.asList(otherPermissions));
+ permissions.add(NETWORK_STACK);
+ permissions.add(PERMISSION_MAINLINE_NETWORK_STACK);
+ enforceAnyPermissionOf(context, permissions.toArray(new String[0]));
+ }
}