blob: 2531adaa6966f4754a8a843f28d960be17c27f01 [file] [log] [blame]
Jason Monkbf3eedc2018-04-05 20:56:42 -04001/*
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
15package com.android.server.slice;
16
17import static org.junit.Assert.assertFalse;
18import static org.junit.Assert.assertTrue;
19
20import android.content.ContentProvider;
21import android.content.ContentResolver;
22import android.net.Uri;
23import android.net.Uri.Builder;
24import android.os.FileUtils;
Jason Monkbf3eedc2018-04-05 20:56:42 -040025import android.testing.AndroidTestingRunner;
26import android.testing.TestableLooper;
27import android.testing.TestableLooper.RunWithLooper;
Jason Monkbf3eedc2018-04-05 20:56:42 -040028import android.util.Xml.Encoding;
29
Brett Chabot8091d9e2019-02-26 14:52:33 -080030import androidx.test.filters.SmallTest;
31
Jason Monkbf3eedc2018-04-05 20:56:42 -040032import com.android.server.UiServiceTestCase;
33
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.xmlpull.v1.XmlPullParser;
37import org.xmlpull.v1.XmlPullParserException;
38import org.xmlpull.v1.XmlPullParserFactory;
39import org.xmlpull.v1.XmlSerializer;
40
41import java.io.ByteArrayInputStream;
42import java.io.ByteArrayOutputStream;
43import java.io.File;
44import java.io.IOException;
45
46@SmallTest
47@RunWith(AndroidTestingRunner.class)
48@RunWithLooper
49public class SlicePermissionManagerTest extends UiServiceTestCase {
50
51 @Test
Jason Monk9c03ef42018-05-11 09:26:51 -070052 public void testGrant() {
53 File sliceDir = new File(mContext.getDataDir(), "system/slices");
54 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
55 TestableLooper.get(this).getLooper(), sliceDir);
56 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
57 .authority("authority")
58 .path("something").build();
59
60 permissions.grantSliceAccess("my.pkg", 0, "provider.pkg", 0, uri);
61
62 assertTrue(permissions.hasPermission("my.pkg", 0, uri));
63 }
64
65 @Test
Jason Monkbf3eedc2018-04-05 20:56:42 -040066 public void testBackup() throws XmlPullParserException, IOException {
67 File sliceDir = new File(mContext.getDataDir(), "system/slices");
68 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
69 .authority("authority")
70 .path("something").build();
71 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
72 TestableLooper.get(this).getLooper(), sliceDir);
73
74 permissions.grantFullAccess("com.android.mypkg", 10);
75 permissions.grantSliceAccess("com.android.otherpkg", 0, "com.android.lastpkg", 1, uri);
76
77 ByteArrayOutputStream output = new ByteArrayOutputStream();
78 XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer();
79 serializer.setOutput(output, Encoding.UTF_8.name());
80
81
82 TestableLooper.get(this).processAllMessages();
83 permissions.writeBackup(serializer);
84 serializer.flush();
85
86 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
87 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
88 parser.setInput(input, Encoding.UTF_8.name());
89
90 permissions = new SlicePermissionManager(mContext,
91 TestableLooper.get(this).getLooper());
92 permissions.readRestore(parser);
93
94 assertTrue(permissions.hasFullAccess("com.android.mypkg", 10));
95 assertTrue(permissions.hasPermission("com.android.otherpkg", 0,
96 ContentProvider.maybeAddUserId(uri, 1)));
97 permissions.removePkg("com.android.lastpkg", 1);
98 assertFalse(permissions.hasPermission("com.android.otherpkg", 0,
99 ContentProvider.maybeAddUserId(uri, 1)));
100
101 // Cleanup.
102 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
103 }
104
105}