1 /*
2  * Copyright (C) 2018 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 android.slice.cts;
16 
17 import static android.content.pm.PackageManager.PERMISSION_DENIED;
18 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assume.assumeFalse;
22 
23 import android.app.slice.SliceManager;
24 import android.app.slice.SliceProvider;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.content.pm.ResolveInfo;
30 import android.net.Uri;
31 import android.os.Process;
32 
33 import androidx.test.InstrumentationRegistry;
34 import androidx.test.runner.AndroidJUnit4;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 import java.util.List;
42 
43 @RunWith(AndroidJUnit4.class)
44 public class SlicePermissionsTest {
45 
46     private static final Uri BASE_URI = Uri.parse("content://android.slice.cts.local/");
47     private final Context mContext = InstrumentationRegistry.getContext();
48     private String mTestPkg;
49     private int mTestUid;
50     private int mTestPid;
51     private SliceManager mSliceManager;
52     private boolean isSliceDisabled = mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_SLICES_DISABLED);
53 
54     @Before
setup()55     public void setup() throws NameNotFoundException {
56         assumeFalse(isSliceDisabled);
57         mSliceManager = mContext.getSystemService(SliceManager.class);
58         mTestPkg = mContext.getPackageName();
59         mTestUid = mContext.getPackageManager().getPackageUid(mTestPkg, 0);
60         mTestPid = Process.myPid();
61     }
62 
63     @After
tearDown()64     public void tearDown() {
65         if (isSliceDisabled) {
66             return;
67         }
68         mSliceManager.revokeSlicePermission(mTestPkg, BASE_URI);
69     }
70 
71     @Test
testGrant()72     public void testGrant() {
73         assumeFalse(isSliceDisabled);
74         assertEquals(PERMISSION_DENIED,
75                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
76 
77         mSliceManager.grantSlicePermission(mTestPkg, BASE_URI);
78 
79         assertEquals(PERMISSION_GRANTED,
80                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
81     }
82 
83     @Test
testGrantParent()84     public void testGrantParent() {
85         assumeFalse(isSliceDisabled);
86         Uri uri = BASE_URI.buildUpon()
87                 .appendPath("something")
88                 .build();
89 
90         assertEquals(PERMISSION_DENIED,
91                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
92 
93         mSliceManager.grantSlicePermission(mTestPkg, BASE_URI);
94 
95         assertEquals(PERMISSION_GRANTED,
96                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
97     }
98 
99     @Test
testGrantParentExpands()100     public void testGrantParentExpands() {
101         assumeFalse(isSliceDisabled);
102         Uri uri = BASE_URI.buildUpon()
103                 .appendPath("something")
104                 .build();
105 
106         assertEquals(PERMISSION_DENIED,
107                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
108 
109         mSliceManager.grantSlicePermission(mTestPkg, uri);
110 
111         // Only sub-path granted.
112         assertEquals(PERMISSION_GRANTED,
113                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
114         assertEquals(PERMISSION_DENIED,
115                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
116 
117         mSliceManager.grantSlicePermission(mTestPkg, BASE_URI);
118 
119         // Now all granted.
120         assertEquals(PERMISSION_GRANTED,
121                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
122         assertEquals(PERMISSION_GRANTED,
123                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
124     }
125 
126     @Test
testGrantChild()127     public void testGrantChild() {
128         assumeFalse(isSliceDisabled);
129         Uri uri = BASE_URI.buildUpon()
130                 .appendPath("something")
131                 .build();
132 
133         assertEquals(PERMISSION_DENIED,
134                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
135 
136         mSliceManager.grantSlicePermission(mTestPkg, uri);
137 
138         // Still no permission because only a child was granted
139         assertEquals(PERMISSION_DENIED,
140                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
141     }
142 
143     @Test
testRevoke()144     public void testRevoke() {
145         assumeFalse(isSliceDisabled);
146         assertEquals(PERMISSION_DENIED,
147                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
148 
149         mSliceManager.grantSlicePermission(mTestPkg, BASE_URI);
150 
151         assertEquals(PERMISSION_GRANTED,
152                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
153 
154         mSliceManager.revokeSlicePermission(mTestPkg, BASE_URI);
155 
156         assertEquals(PERMISSION_DENIED,
157                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
158     }
159 
160     @Test
testRevokeParent()161     public void testRevokeParent() {
162         assumeFalse(isSliceDisabled);
163         Uri uri = BASE_URI.buildUpon()
164                 .appendPath("something")
165                 .build();
166         assertEquals(PERMISSION_DENIED,
167                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
168 
169         mSliceManager.grantSlicePermission(mTestPkg, uri);
170 
171         assertEquals(PERMISSION_GRANTED,
172                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
173 
174         mSliceManager.revokeSlicePermission(mTestPkg, BASE_URI);
175 
176         // Revoked because parent was revoked
177         assertEquals(PERMISSION_DENIED,
178                 mSliceManager.checkSlicePermission(uri, mTestPid, mTestUid));
179     }
180 
181     @Test
testPermissionIntent()182     public void testPermissionIntent() {
183         Intent intent = SliceProvider.createPermissionIntent(mContext, BASE_URI, mTestPkg);
184         PackageManager pm = mContext.getPackageManager();
185         List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
186         assertEquals(1, activities.size());
187     }
188 
189     @Test
testRevokeChild()190     public void testRevokeChild() {
191         assumeFalse(isSliceDisabled);
192         Uri uri = BASE_URI.buildUpon()
193                 .appendPath("something")
194                 .build();
195         assertEquals(PERMISSION_DENIED,
196                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
197 
198         mSliceManager.grantSlicePermission(mTestPkg, BASE_URI);
199 
200         assertEquals(PERMISSION_GRANTED,
201                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
202 
203         mSliceManager.revokeSlicePermission(mTestPkg, uri);
204 
205         // Not revoked because child was revoked.
206         assertEquals(PERMISSION_GRANTED,
207                 mSliceManager.checkSlicePermission(BASE_URI, mTestPid, mTestUid));
208     }
209 
210 }
211