blob: 97f2bea32fc3d660fb2fdf6ed3c21c71316bc5f7 [file] [log] [blame]
Jason Monk8da82d82018-04-18 15:47:38 -04001/*
2 * Copyright 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
17package androidx.slice.compat;
18
19import static androidx.core.content.PermissionChecker.PERMISSION_DENIED;
20import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
21
22import static org.junit.Assert.assertEquals;
23import static org.mockito.ArgumentMatchers.eq;
Jason Monk1830d572018-05-22 11:10:39 -040024import static org.mockito.Mockito.mock;
Jason Monk8da82d82018-04-18 15:47:38 -040025import static org.mockito.Mockito.spy;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
Jason Monk1830d572018-05-22 11:10:39 -040028import static org.mockito.Mockito.withSettings;
Jason Monk8da82d82018-04-18 15:47:38 -040029
30import android.content.ContentResolver;
31import android.content.Context;
32import android.content.ContextWrapper;
33import android.content.pm.PackageManager;
34import android.net.Uri;
35import android.os.Process;
Aurimas Liutikas3f9ceb62018-07-10 11:57:16 -070036
Alan Viverettebadf2f82018-12-18 12:14:10 -050037import androidx.test.core.app.ApplicationProvider;
38import androidx.test.ext.junit.runners.AndroidJUnit4;
Louis Pullen-Freilich532eef42019-01-24 18:48:57 +000039import androidx.test.filters.LargeTest;
Aurimas Liutikas3f9ceb62018-07-10 11:57:16 -070040import androidx.test.filters.SdkSuppress;
Jason Monk8da82d82018-04-18 15:47:38 -040041
42import org.junit.Test;
43import org.junit.runner.RunWith;
Jason Monk1830d572018-05-22 11:10:39 -040044import org.mockito.invocation.InvocationOnMock;
45import org.mockito.stubbing.Answer;
Jason Monk8da82d82018-04-18 15:47:38 -040046
47@RunWith(AndroidJUnit4.class)
Louis Pullen-Freilich532eef42019-01-24 18:48:57 +000048@LargeTest
Jason Monkb4537e82018-06-22 12:05:10 -040049@SdkSuppress(minSdkVersion = 19)
Jason Monk8da82d82018-04-18 15:47:38 -040050public class CompatPermissionManagerTest {
51
Alan Viverettebadf2f82018-12-18 12:14:10 -050052 private final Context mContext = ApplicationProvider.getApplicationContext();
Jason Monk8da82d82018-04-18 15:47:38 -040053
54 @Test
55 public void testAutoGrant() {
56 final Uri uri = new Uri.Builder()
57 .scheme(ContentResolver.SCHEME_CONTENT)
58 .authority("my.authority")
59 .path("my_path")
60 .build();
61 final String testPermission = "android.permission.SOME_PERMISSION";
62
63 final int grantedPid = Process.myPid();
64 final int grantedUid = Process.myUid();
65
66 final int nonGrantedPid = grantedPid + 1;
67 final int nonGrantedUid = grantedUid + 1;
68
69 Context permContext = new ContextWrapper(mContext) {
70 @Override
71 public int checkPermission(String permission, int pid, int uid) {
72 if (testPermission.equals(permission)) {
73 if (grantedUid == uid) {
74 return PackageManager.PERMISSION_GRANTED;
75 } else if (nonGrantedUid == uid) {
76 return PackageManager.PERMISSION_DENIED;
77 }
78 }
79 return super.checkPermission(permission, pid, uid);
80 }
81
82 @Override
83 public PackageManager getPackageManager() {
Jason Monk1830d572018-05-22 11:10:39 -040084 final PackageManager realPm = super.getPackageManager();
85 PackageManager pm = mock(PackageManager.class, withSettings()
86 .defaultAnswer(new Answer() {
87 @Override
88 public Object answer(InvocationOnMock invocation) throws Throwable {
89 return invocation.getMethod().invoke(realPm,
90 invocation.getArguments());
91 }
92 }));
Jason Monk8da82d82018-04-18 15:47:38 -040093 when(pm.getPackagesForUid(grantedUid)).thenReturn(new String[] { "grant_pkg"});
94 when(pm.getPackagesForUid(nonGrantedUid)).thenReturn(new String[] { "other_pkg"});
95 return pm;
96 }
97 };
98 CompatPermissionManager manager = spy(new CompatPermissionManager(permContext, "nothing", 0,
99 new String[] {testPermission}));
100
101 assertEquals(PERMISSION_DENIED, manager.checkSlicePermission(uri,
102 nonGrantedPid, nonGrantedUid));
103
104 assertEquals(PERMISSION_GRANTED, manager.checkSlicePermission(uri, grantedPid, grantedUid));
105 verify(manager).grantSlicePermission(eq(uri), eq("grant_pkg"));
106
107 }
108
109}