• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.permission.cts;
18 
19 import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
20 
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.fail;
23 
24 import android.content.Context;
25 import android.content.pm.PackageInfo;
26 import android.content.pm.PackageManager;
27 import android.content.pm.PermissionInfo;
28 import android.platform.test.annotations.AppModeFull;
29 
30 import androidx.test.InstrumentationRegistry;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class MainlineNetworkStackPermissionTest{
38     private final Context mContext = InstrumentationRegistry.getContext();
39 
40     /**
41      * Test that a package defining android.permission.MAINLINE_NETWORK_STACK is installed,
42      * and is a system package
43      */
44     @Test
45     @AppModeFull(reason = "Instant apps cannot access PackageManager#getPermissionInfo")
testPackageWithMainlineNetworkStackPermission()46     public void testPackageWithMainlineNetworkStackPermission() throws Exception {
47         final PackageManager packageManager = mContext.getPackageManager();
48         assertNotNull("Unable to find PackageManager.", packageManager);
49 
50         final PermissionInfo permissioninfo =
51                 packageManager.getPermissionInfo(PERMISSION_MAINLINE_NETWORK_STACK, 0);
52         assertNotNull("Network stack permission is not defined.", permissioninfo);
53 
54         PackageInfo packageInfo = packageManager.getPackageInfo(permissioninfo.packageName,
55                 PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_PERMISSIONS);
56         assertNotNull("Package defining the network stack permission is not a system package.",
57                 packageInfo.permissions);
58 
59         for (PermissionInfo permission : packageInfo.permissions) {
60             if (PERMISSION_MAINLINE_NETWORK_STACK.equals(permission.name)) {
61                 return;
62             }
63         }
64 
65         fail("Expect a system package defining " + PERMISSION_MAINLINE_NETWORK_STACK
66                 + " is installed.");
67     }
68 }
69