1 /* 2 * Copyright (C) 2020 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.server.uri; 18 19 import static com.android.server.uri.UriGrantsMockContext.FLAG_PERSISTABLE; 20 import static com.android.server.uri.UriGrantsMockContext.FLAG_READ; 21 import static com.android.server.uri.UriGrantsMockContext.FLAG_WRITE; 22 import static com.android.server.uri.UriGrantsMockContext.PKG_CAMERA; 23 import static com.android.server.uri.UriGrantsMockContext.PKG_SOCIAL; 24 import static com.android.server.uri.UriGrantsMockContext.UID_PRIMARY_SOCIAL; 25 import static com.android.server.uri.UriGrantsMockContext.URI_PHOTO_1; 26 import static com.android.server.uri.UriGrantsMockContext.URI_PHOTO_2; 27 import static com.android.server.uri.UriGrantsMockContext.USER_PRIMARY; 28 import static com.android.server.uri.UriPermission.INVALID_TIME; 29 import static com.android.server.uri.UriPermission.STRENGTH_GLOBAL; 30 import static com.android.server.uri.UriPermission.STRENGTH_NONE; 31 import static com.android.server.uri.UriPermission.STRENGTH_OWNED; 32 import static com.android.server.uri.UriPermission.STRENGTH_PERSISTABLE; 33 34 import static org.junit.Assert.assertEquals; 35 import static org.junit.Assert.assertFalse; 36 import static org.junit.Assert.assertTrue; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 public class UriPermissionTest { 44 @Mock 45 private UriGrantsManagerInternal mService; 46 47 @Before setUp()48 public void setUp() throws Exception { 49 MockitoAnnotations.initMocks(this); 50 } 51 52 @Test testNone()53 public void testNone() { 54 final GrantUri grant = new GrantUri(USER_PRIMARY, URI_PHOTO_1, FLAG_READ); 55 final UriPermission perm = new UriPermission(PKG_CAMERA, 56 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant); 57 assertEquals(STRENGTH_NONE, perm.getStrength(FLAG_READ)); 58 } 59 60 @Test testGlobal()61 public void testGlobal() { 62 final GrantUri grant = new GrantUri(USER_PRIMARY, URI_PHOTO_1, FLAG_READ); 63 final UriPermission perm = new UriPermission(PKG_CAMERA, 64 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant); 65 66 assertFalse(perm.grantModes(FLAG_READ, null)); 67 assertEquals(STRENGTH_GLOBAL, perm.getStrength(FLAG_READ)); 68 69 assertFalse(perm.revokeModes(FLAG_READ, true)); 70 assertEquals(STRENGTH_NONE, perm.getStrength(FLAG_READ)); 71 } 72 73 @Test testOwned()74 public void testOwned() { 75 final GrantUri grant = new GrantUri(USER_PRIMARY, URI_PHOTO_1, FLAG_READ); 76 final UriPermission perm = new UriPermission(PKG_CAMERA, 77 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant); 78 final UriPermissionOwner owner = new UriPermissionOwner(mService, "test"); 79 80 assertFalse(perm.grantModes(FLAG_READ, owner)); 81 assertEquals(STRENGTH_OWNED, perm.getStrength(FLAG_READ)); 82 83 assertFalse(perm.revokeModes(FLAG_READ, false)); 84 assertEquals(STRENGTH_OWNED, perm.getStrength(FLAG_READ)); 85 86 assertFalse(perm.revokeModes(FLAG_READ, true)); 87 assertEquals(STRENGTH_NONE, perm.getStrength(FLAG_READ)); 88 } 89 90 @Test testOverlap()91 public void testOverlap() { 92 final GrantUri grant1 = new GrantUri(USER_PRIMARY, URI_PHOTO_1, FLAG_READ); 93 final GrantUri grant2 = new GrantUri(USER_PRIMARY, URI_PHOTO_2, FLAG_READ); 94 95 final UriPermission photo1 = new UriPermission(PKG_CAMERA, 96 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant1); 97 final UriPermission photo2 = new UriPermission(PKG_CAMERA, 98 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant2); 99 100 // Verify behavior when we have multiple owners within the same app 101 final UriPermissionOwner activity = new UriPermissionOwner(mService, "activity"); 102 final UriPermissionOwner service = new UriPermissionOwner(mService, "service"); 103 104 photo1.grantModes(FLAG_READ | FLAG_WRITE, activity); 105 photo1.grantModes(FLAG_READ, service); 106 photo2.grantModes(FLAG_READ, activity); 107 photo2.grantModes(FLAG_WRITE, null); 108 109 assertEquals(FLAG_READ | FLAG_WRITE, photo1.modeFlags); 110 assertEquals(FLAG_READ | FLAG_WRITE, photo2.modeFlags); 111 112 // Shutting down activity should only trim away write access 113 activity.removeUriPermissions(); 114 assertEquals(FLAG_READ, photo1.modeFlags); 115 assertEquals(FLAG_WRITE, photo2.modeFlags); 116 117 // Shutting down service should bring everything else down 118 service.removeUriPermissions(); 119 assertEquals(0, photo1.modeFlags); 120 assertEquals(FLAG_WRITE, photo2.modeFlags); 121 } 122 123 @Test testPersist()124 public void testPersist() { 125 final GrantUri grant = new GrantUri(USER_PRIMARY, URI_PHOTO_1, FLAG_READ); 126 final UriPermission perm = new UriPermission(PKG_CAMERA, 127 PKG_SOCIAL, UID_PRIMARY_SOCIAL, grant); 128 129 assertFalse(perm.grantModes(FLAG_READ, null)); 130 assertFalse(perm.grantModes(FLAG_WRITE | FLAG_PERSISTABLE, null)); 131 assertEquals(STRENGTH_GLOBAL, perm.getStrength(FLAG_READ)); 132 assertEquals(STRENGTH_PERSISTABLE, perm.getStrength(FLAG_WRITE)); 133 134 // Verify behavior of non-persistable mode; nothing happens 135 { 136 assertFalse(perm.takePersistableModes(FLAG_READ)); 137 assertEquals(0, perm.persistedModeFlags); 138 139 assertFalse(perm.releasePersistableModes(FLAG_READ)); 140 assertEquals(0, perm.persistedModeFlags); 141 } 142 143 // Verify behavior of persistable mode 144 { 145 assertEquals(FLAG_WRITE, perm.persistableModeFlags); 146 assertEquals(0, perm.persistedModeFlags); 147 assertTrue(perm.takePersistableModes(FLAG_WRITE)); 148 assertEquals(FLAG_WRITE, perm.persistableModeFlags); 149 assertEquals(FLAG_WRITE, perm.persistedModeFlags); 150 151 // Attempting to take a second time should be a no-op 152 final long createTime = perm.persistedCreateTime; 153 assertFalse(perm.takePersistableModes(FLAG_WRITE)); 154 assertEquals(createTime, perm.persistedCreateTime); 155 156 assertTrue(perm.releasePersistableModes(FLAG_WRITE)); 157 assertEquals(FLAG_WRITE, perm.persistableModeFlags); 158 assertEquals(0, perm.persistedModeFlags); 159 assertEquals(INVALID_TIME, perm.persistedCreateTime); 160 161 // Attempting to release a second time should be a no-op 162 assertFalse(perm.releasePersistableModes(FLAG_WRITE)); 163 164 // We should still be able to take again 165 assertTrue(perm.takePersistableModes(FLAG_WRITE)); 166 assertEquals(FLAG_WRITE, perm.persistableModeFlags); 167 assertEquals(FLAG_WRITE, perm.persistedModeFlags); 168 } 169 } 170 } 171