Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.slice.compat; |
| 18 | |
| 19 | import static androidx.core.content.PermissionChecker.PERMISSION_DENIED; |
| 20 | import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED; |
| 21 | |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
Jason Monk | 1830d57 | 2018-05-22 11:10:39 -0400 | [diff] [blame] | 24 | import static org.mockito.Mockito.mock; |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 25 | import static org.mockito.Mockito.spy; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
Jason Monk | 1830d57 | 2018-05-22 11:10:39 -0400 | [diff] [blame] | 28 | import static org.mockito.Mockito.withSettings; |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 29 | |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.ContextWrapper; |
| 33 | import android.content.pm.PackageManager; |
| 34 | import android.net.Uri; |
| 35 | import android.os.Process; |
Aurimas Liutikas | 3f9ceb6 | 2018-07-10 11:57:16 -0700 | [diff] [blame] | 36 | |
Alan Viverette | badf2f8 | 2018-12-18 12:14:10 -0500 | [diff] [blame] | 37 | import androidx.test.core.app.ApplicationProvider; |
| 38 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
Louis Pullen-Freilich | 532eef4 | 2019-01-24 18:48:57 +0000 | [diff] [blame^] | 39 | import androidx.test.filters.LargeTest; |
Aurimas Liutikas | 3f9ceb6 | 2018-07-10 11:57:16 -0700 | [diff] [blame] | 40 | import androidx.test.filters.SdkSuppress; |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 41 | |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
Jason Monk | 1830d57 | 2018-05-22 11:10:39 -0400 | [diff] [blame] | 44 | import org.mockito.invocation.InvocationOnMock; |
| 45 | import org.mockito.stubbing.Answer; |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 46 | |
| 47 | @RunWith(AndroidJUnit4.class) |
Louis Pullen-Freilich | 532eef4 | 2019-01-24 18:48:57 +0000 | [diff] [blame^] | 48 | @LargeTest |
Jason Monk | b4537e8 | 2018-06-22 12:05:10 -0400 | [diff] [blame] | 49 | @SdkSuppress(minSdkVersion = 19) |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 50 | public class CompatPermissionManagerTest { |
| 51 | |
Alan Viverette | badf2f8 | 2018-12-18 12:14:10 -0500 | [diff] [blame] | 52 | private final Context mContext = ApplicationProvider.getApplicationContext(); |
Jason Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 53 | |
| 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 Monk | 1830d57 | 2018-05-22 11:10:39 -0400 | [diff] [blame] | 84 | 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 Monk | 8da82d8 | 2018-04-18 15:47:38 -0400 | [diff] [blame] | 93 | 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 | } |