blob: efefee119e5f26ac83c1ae57247dfc4775969ddd [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;
Dan Sandlere7316812019-01-10 11:24:19 -050019import static org.junit.Assert.fail;
Jason Monkbf3eedc2018-04-05 20:56:42 -040020
21import android.content.ContentProvider;
22import android.content.ContentResolver;
23import android.net.Uri;
24import android.net.Uri.Builder;
25import android.os.FileUtils;
26import android.support.test.filters.SmallTest;
27import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper;
29import android.testing.TestableLooper.RunWithLooper;
Dan Sandlere7316812019-01-10 11:24:19 -050030import android.util.Log;
Jason Monkbf3eedc2018-04-05 20:56:42 -040031import android.util.Xml.Encoding;
32
33import com.android.server.UiServiceTestCase;
34
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.xmlpull.v1.XmlPullParser;
38import org.xmlpull.v1.XmlPullParserException;
39import org.xmlpull.v1.XmlPullParserFactory;
40import org.xmlpull.v1.XmlSerializer;
41
42import java.io.ByteArrayInputStream;
43import java.io.ByteArrayOutputStream;
44import java.io.File;
45import java.io.IOException;
46
47@SmallTest
48@RunWith(AndroidTestingRunner.class)
49@RunWithLooper
50public class SlicePermissionManagerTest extends UiServiceTestCase {
Dan Sandlere7316812019-01-10 11:24:19 -050051 private static final String TAG = "SlicePerManTest";
Jason Monkbf3eedc2018-04-05 20:56:42 -040052
53 @Test
Jason Monk9c03ef42018-05-11 09:26:51 -070054 public void testGrant() {
Dan Sandlere7316812019-01-10 11:24:19 -050055 File sliceDir = new File(mContext.getCacheDir(), "testGrantSlices");
56 Log.v(TAG, "testGrant: slice permissions stored in " + sliceDir.getAbsolutePath());
Jason Monk9c03ef42018-05-11 09:26:51 -070057 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
58 TestableLooper.get(this).getLooper(), sliceDir);
59 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
60 .authority("authority")
61 .path("something").build();
62
63 permissions.grantSliceAccess("my.pkg", 0, "provider.pkg", 0, uri);
64
65 assertTrue(permissions.hasPermission("my.pkg", 0, uri));
Dan Sandlere7316812019-01-10 11:24:19 -050066
67 // Cleanup.
68 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
Jason Monk9c03ef42018-05-11 09:26:51 -070069 }
70
71 @Test
Jason Monkbf3eedc2018-04-05 20:56:42 -040072 public void testBackup() throws XmlPullParserException, IOException {
Dan Sandlere7316812019-01-10 11:24:19 -050073 File sliceDir = new File(mContext.getCacheDir(), "testBackupSlices");
74 Log.v(TAG, "testBackup: slice permissions stored in " + sliceDir.getAbsolutePath());
Jason Monkbf3eedc2018-04-05 20:56:42 -040075 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
76 .authority("authority")
77 .path("something").build();
78 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
79 TestableLooper.get(this).getLooper(), sliceDir);
80
81 permissions.grantFullAccess("com.android.mypkg", 10);
82 permissions.grantSliceAccess("com.android.otherpkg", 0, "com.android.lastpkg", 1, uri);
83
84 ByteArrayOutputStream output = new ByteArrayOutputStream();
85 XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer();
86 serializer.setOutput(output, Encoding.UTF_8.name());
87
88
89 TestableLooper.get(this).processAllMessages();
90 permissions.writeBackup(serializer);
91 serializer.flush();
92
93 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
94 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
95 parser.setInput(input, Encoding.UTF_8.name());
96
97 permissions = new SlicePermissionManager(mContext,
98 TestableLooper.get(this).getLooper());
99 permissions.readRestore(parser);
100
Dan Sandlere7316812019-01-10 11:24:19 -0500101 if (!permissions.hasFullAccess("com.android.mypkg", 10)) {
102 fail("com.android.mypkg@10 did not have full access. backup file: "
103 + output.toString());
104 }
Jason Monkbf3eedc2018-04-05 20:56:42 -0400105 assertTrue(permissions.hasPermission("com.android.otherpkg", 0,
106 ContentProvider.maybeAddUserId(uri, 1)));
107 permissions.removePkg("com.android.lastpkg", 1);
108 assertFalse(permissions.hasPermission("com.android.otherpkg", 0,
109 ContentProvider.maybeAddUserId(uri, 1)));
110
111 // Cleanup.
112 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
113 }
114
Dan Sandler2d8e3d12018-12-13 15:32:13 -0500115 @Test
Dan Sandlere7316812019-01-10 11:24:19 -0500116 public void testInvalid() {
117 File sliceDir = new File(mContext.getCacheDir(), "testInvalidSlices");
118 Log.v(TAG, "testInvalid: slice permissions stored in " + sliceDir.getAbsolutePath());
Dan Sandler2d8e3d12018-12-13 15:32:13 -0500119 if (!sliceDir.exists()) {
120 sliceDir.mkdir();
121 }
122 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
123 TestableLooper.get(this).getLooper(), sliceDir);
124
125 DirtyTracker.Persistable junk = new DirtyTracker.Persistable() {
126 @Override
127 public String getFileName() {
128 return "invalidData";
129 }
130
131 @Override
132 public void writeTo(XmlSerializer out) throws IOException {
Dan Sandlere7316812019-01-10 11:24:19 -0500133 throw new RuntimeException("this RuntimeException inside junk.writeTo() "
134 + "should be caught and suppressed by surrounding code");
Dan Sandler2d8e3d12018-12-13 15:32:13 -0500135 }
136 };
137
138 // let's put something bad in here
139 permissions.addDirtyImmediate(junk);
140 // force a persist. if this throws, it would take down system_server
141 permissions.handlePersist();
142
143 // Cleanup.
144 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
145 }
146
147}