blob: b315e514d6a6d7da2a0fadd21f3253083e83b672 [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;
25import android.support.test.filters.SmallTest;
26import android.testing.AndroidTestingRunner;
27import android.testing.TestableLooper;
28import android.testing.TestableLooper.RunWithLooper;
Jason Monkbf3eedc2018-04-05 20:56:42 -040029import android.util.Xml.Encoding;
30
31import com.android.server.UiServiceTestCase;
32
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.xmlpull.v1.XmlPullParser;
36import org.xmlpull.v1.XmlPullParserException;
37import org.xmlpull.v1.XmlPullParserFactory;
38import org.xmlpull.v1.XmlSerializer;
39
40import java.io.ByteArrayInputStream;
41import java.io.ByteArrayOutputStream;
42import java.io.File;
43import java.io.IOException;
44
45@SmallTest
46@RunWith(AndroidTestingRunner.class)
47@RunWithLooper
48public class SlicePermissionManagerTest extends UiServiceTestCase {
49
50 @Test
Jason Monk9c03ef42018-05-11 09:26:51 -070051 public void testGrant() {
52 File sliceDir = new File(mContext.getDataDir(), "system/slices");
53 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
54 TestableLooper.get(this).getLooper(), sliceDir);
55 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
56 .authority("authority")
57 .path("something").build();
58
59 permissions.grantSliceAccess("my.pkg", 0, "provider.pkg", 0, uri);
60
61 assertTrue(permissions.hasPermission("my.pkg", 0, uri));
62 }
63
64 @Test
Jason Monkbf3eedc2018-04-05 20:56:42 -040065 public void testBackup() throws XmlPullParserException, IOException {
66 File sliceDir = new File(mContext.getDataDir(), "system/slices");
67 Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
68 .authority("authority")
69 .path("something").build();
70 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
71 TestableLooper.get(this).getLooper(), sliceDir);
72
73 permissions.grantFullAccess("com.android.mypkg", 10);
74 permissions.grantSliceAccess("com.android.otherpkg", 0, "com.android.lastpkg", 1, uri);
75
76 ByteArrayOutputStream output = new ByteArrayOutputStream();
77 XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer();
78 serializer.setOutput(output, Encoding.UTF_8.name());
79
80
81 TestableLooper.get(this).processAllMessages();
82 permissions.writeBackup(serializer);
83 serializer.flush();
84
85 ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
86 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
87 parser.setInput(input, Encoding.UTF_8.name());
88
89 permissions = new SlicePermissionManager(mContext,
90 TestableLooper.get(this).getLooper());
91 permissions.readRestore(parser);
92
93 assertTrue(permissions.hasFullAccess("com.android.mypkg", 10));
94 assertTrue(permissions.hasPermission("com.android.otherpkg", 0,
95 ContentProvider.maybeAddUserId(uri, 1)));
96 permissions.removePkg("com.android.lastpkg", 1);
97 assertFalse(permissions.hasPermission("com.android.otherpkg", 0,
98 ContentProvider.maybeAddUserId(uri, 1)));
99
100 // Cleanup.
101 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
102 }
103
Dan Sandler98267b32018-12-13 15:32:13 -0500104 @Test
105 public void testInvalid() throws Exception {
106 File sliceDir = new File(mContext.getCacheDir(), "slices-test");
107 if (!sliceDir.exists()) {
108 sliceDir.mkdir();
109 }
110 SlicePermissionManager permissions = new SlicePermissionManager(mContext,
111 TestableLooper.get(this).getLooper(), sliceDir);
112
113 DirtyTracker.Persistable junk = new DirtyTracker.Persistable() {
114 @Override
115 public String getFileName() {
116 return "invalidData";
117 }
118
119 @Override
120 public void writeTo(XmlSerializer out) throws IOException {
121 throw new RuntimeException("this doesn't work");
122 }
123 };
124
125 // let's put something bad in here
126 permissions.addDirtyImmediate(junk);
127 // force a persist. if this throws, it would take down system_server
128 permissions.handlePersist();
129
130 // Cleanup.
131 assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
132 }
133
134}