Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.server.slice; |
| 16 | |
| 17 | import static android.content.ContentProvider.maybeAddUserId; |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 18 | import static android.content.pm.PackageManager.PERMISSION_DENIED; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 20 | |
Jason Monk | 4f645cc | 2018-06-22 16:01:55 -0400 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 22 | import static org.mockito.ArgumentMatchers.any; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 23 | import static org.mockito.ArgumentMatchers.anyInt; |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.anyString; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | import static org.mockito.Mockito.doReturn; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.never; |
| 29 | import static org.mockito.Mockito.spy; |
| 30 | import static org.mockito.Mockito.times; |
| 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
| 34 | import android.app.AppOpsManager; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 35 | import android.app.slice.SliceSpec; |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 36 | import android.app.usage.UsageStatsManagerInternal; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 37 | import android.content.pm.PackageManagerInternal; |
| 38 | import android.net.Uri; |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 39 | import android.os.Binder; |
| 40 | import android.os.IBinder; |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 41 | import android.os.Process; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 42 | import android.os.RemoteException; |
| 43 | import android.support.test.filters.SmallTest; |
| 44 | import android.testing.AndroidTestingRunner; |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 45 | import android.testing.TestableContext; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 46 | import android.testing.TestableLooper; |
| 47 | import android.testing.TestableLooper.RunWithLooper; |
| 48 | |
| 49 | import com.android.server.LocalServices; |
| 50 | import com.android.server.UiServiceTestCase; |
| 51 | |
| 52 | import org.junit.After; |
| 53 | import org.junit.Before; |
| 54 | import org.junit.Test; |
| 55 | import org.junit.runner.RunWith; |
| 56 | |
| 57 | @SmallTest |
| 58 | @RunWith(AndroidTestingRunner.class) |
| 59 | @RunWithLooper |
| 60 | public class SliceManagerServiceTest extends UiServiceTestCase { |
| 61 | |
| 62 | private static final String AUTH = "com.android.services.uitests"; |
Jason Monk | 4f645cc | 2018-06-22 16:01:55 -0400 | [diff] [blame] | 63 | private static final Uri TEST_URI = Uri.parse("content://" + AUTH + "/path"); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 64 | |
| 65 | private static final SliceSpec[] EMPTY_SPECS = new SliceSpec[]{ |
| 66 | }; |
| 67 | |
| 68 | private SliceManagerService mService; |
| 69 | private PinnedSliceState mCreatedSliceState; |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 70 | private IBinder mToken = new Binder(); |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 71 | private TestableContext mContextSpy; |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 72 | |
| 73 | @Before |
| 74 | public void setup() { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 75 | LocalServices.addService(UsageStatsManagerInternal.class, |
| 76 | mock(UsageStatsManagerInternal.class)); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 77 | mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); |
| 78 | mContext.getTestablePermissions().setPermission(TEST_URI, PERMISSION_GRANTED); |
| 79 | |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 80 | mContextSpy = spy(mContext); |
| 81 | mService = spy(new SliceManagerService(mContextSpy, TestableLooper.get(this).getLooper())); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 82 | mCreatedSliceState = mock(PinnedSliceState.class); |
Jason Monk | f88d25e | 2018-03-06 20:13:24 -0500 | [diff] [blame] | 83 | doReturn(mCreatedSliceState).when(mService).createPinnedSlice(eq(TEST_URI), anyString()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @After |
| 87 | public void teardown() { |
Jason Monk | 1918ef7 | 2018-03-14 09:20:39 -0400 | [diff] [blame] | 88 | LocalServices.removeServiceForTest(UsageStatsManagerInternal.class); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Test |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 92 | public void testAddPinCreatesPinned() throws RemoteException { |
| 93 | doReturn("pkg").when(mService).getDefaultHome(anyInt()); |
| 94 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 95 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS, mToken); |
| 96 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS, mToken); |
Jason Monk | 4f645cc | 2018-06-22 16:01:55 -0400 | [diff] [blame] | 97 | verify(mService, times(1)).createPinnedSlice(eq(maybeAddUserId(TEST_URI, 0)), anyString()); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testRemovePinDestroysPinned() throws RemoteException { |
| 102 | doReturn("pkg").when(mService).getDefaultHome(anyInt()); |
| 103 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 104 | mService.pinSlice("pkg", TEST_URI, EMPTY_SPECS, mToken); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 105 | |
Jason Monk | 38df280 | 2018-02-22 19:28:12 -0500 | [diff] [blame] | 106 | when(mCreatedSliceState.unpin(eq("pkg"), eq(mToken))).thenReturn(false); |
| 107 | mService.unpinSlice("pkg", TEST_URI, mToken); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 108 | verify(mCreatedSliceState, never()).destroy(); |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 111 | @Test |
| 112 | public void testCheckAutoGrantPermissions() throws RemoteException { |
| 113 | String[] testPerms = new String[] { |
| 114 | "perm1", |
| 115 | "perm2", |
| 116 | }; |
| 117 | when(mContextSpy.checkUriPermission(any(), anyInt(), anyInt(), anyInt())) |
| 118 | .thenReturn(PERMISSION_DENIED); |
| 119 | when(mContextSpy.checkPermission("perm1", Process.myPid(), Process.myUid())) |
| 120 | .thenReturn(PERMISSION_DENIED); |
| 121 | when(mContextSpy.checkPermission("perm2", Process.myPid(), Process.myUid())) |
| 122 | .thenReturn(PERMISSION_GRANTED); |
Jason Monk | fe4885e | 2018-07-23 13:31:30 -0400 | [diff] [blame] | 123 | mService.checkSlicePermission(TEST_URI, mContext.getPackageName(), |
| 124 | mContext.getPackageName(), Process.myPid(), |
Jason Monk | 42e03f8 | 2018-03-30 11:26:56 -0400 | [diff] [blame] | 125 | Process.myUid(), testPerms); |
| 126 | |
| 127 | verify(mContextSpy).checkPermission(eq("perm1"), eq(Process.myPid()), eq(Process.myUid())); |
| 128 | verify(mContextSpy).checkPermission(eq("perm2"), eq(Process.myPid()), eq(Process.myUid())); |
| 129 | } |
| 130 | |
Jason Monk | 4f645cc | 2018-06-22 16:01:55 -0400 | [diff] [blame] | 131 | @Test(expected = IllegalStateException.class) |
| 132 | public void testNoPinThrow() throws Exception { |
| 133 | mService.getPinnedSpecs(TEST_URI, "pkg"); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testGetPinnedSpecs() throws Exception { |
| 138 | SliceSpec[] specs = new SliceSpec[] { |
| 139 | new SliceSpec("Something", 1) }; |
| 140 | mService.pinSlice("pkg", TEST_URI, specs, mToken); |
| 141 | |
| 142 | when(mCreatedSliceState.getSpecs()).thenReturn(specs); |
| 143 | assertEquals(specs, mService.getPinnedSpecs(TEST_URI, "pkg")); |
| 144 | } |
| 145 | |
Jason Monk | 397f6d8 | 2018-04-24 17:01:11 -0400 | [diff] [blame] | 146 | } |