1 /*
2  * Copyright (C) 2018 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 com.android.cts.duplicatepermissiondeclareapp;
18 
19 import static android.content.pm.PackageManager.GET_PERMISSIONS;
20 
21 import static org.junit.Assert.assertEquals;
22 
23 import android.content.pm.PackageManager;
24 import android.content.pm.PermissionInfo;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(AndroidJUnit4.class)
33 public class VerifyDuplicatePermissionTest {
34     private final static String APP_THAT_WAS_INSTALLED_FIRST =
35             "com.android.cts.permissiondeclareapp";
36     private final static String APP_THAT_WAS_INSTALLED_AFTER =
37             "com.android.cts.duplicatepermissiondeclareapp";
38     private final static String PERM = "com.android.cts.permissionNormal";
39 
40     private final static PackageManager sPm =
41             InstrumentationRegistry.getTargetContext().getPackageManager();
42 
getDeclaredPermission(String pkg, String permission)43     private PermissionInfo getDeclaredPermission(String pkg, String permission) throws Exception {
44         for (PermissionInfo permInfo : sPm.getPackageInfo(pkg, GET_PERMISSIONS).permissions) {
45             if (permInfo.name.equals(permission)) {
46                 return permInfo;
47             }
48         }
49 
50         return null;
51     }
52 
53     @Test
verifyDuplicatePermission()54     public void verifyDuplicatePermission() throws Exception {
55         // The other app was first. The second app could not steal the permission. Hence the
56         // permission belongs to the first app.
57         assertEquals(APP_THAT_WAS_INSTALLED_FIRST, sPm.getPermissionInfo(PERM, 0).packageName);
58 
59         // The first app declared the permission
60         assertEquals(APP_THAT_WAS_INSTALLED_FIRST,
61                 getDeclaredPermission(APP_THAT_WAS_INSTALLED_FIRST, PERM).packageName);
62 
63         // The second app declared the permission too, but the permission is owned by first app.
64         // Hence we end up with a permission info that is different from the one read from the
65         // system.
66         assertEquals(APP_THAT_WAS_INSTALLED_AFTER,
67                 getDeclaredPermission(APP_THAT_WAS_INSTALLED_AFTER, PERM).packageName);
68     }
69 }
70