Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 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 com.android.server.slice; |
| 16 | |
| 17 | import static org.junit.Assert.assertFalse; |
| 18 | import static org.junit.Assert.assertTrue; |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 19 | import static org.junit.Assert.fail; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 20 | |
| 21 | import android.content.ContentProvider; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.net.Uri; |
| 24 | import android.net.Uri.Builder; |
| 25 | import android.os.FileUtils; |
| 26 | import android.support.test.filters.SmallTest; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper; |
| 29 | import android.testing.TestableLooper.RunWithLooper; |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 30 | import android.util.Log; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 31 | import android.util.Xml.Encoding; |
| 32 | |
| 33 | import com.android.server.UiServiceTestCase; |
| 34 | |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.xmlpull.v1.XmlPullParser; |
| 38 | import org.xmlpull.v1.XmlPullParserException; |
| 39 | import org.xmlpull.v1.XmlPullParserFactory; |
| 40 | import org.xmlpull.v1.XmlSerializer; |
| 41 | |
| 42 | import java.io.ByteArrayInputStream; |
| 43 | import java.io.ByteArrayOutputStream; |
| 44 | import java.io.File; |
| 45 | import java.io.IOException; |
| 46 | |
| 47 | @SmallTest |
| 48 | @RunWith(AndroidTestingRunner.class) |
| 49 | @RunWithLooper |
| 50 | public class SlicePermissionManagerTest extends UiServiceTestCase { |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 51 | private static final String TAG = "SlicePerManTest"; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 52 | |
| 53 | @Test |
Jason Monk | 9c03ef4 | 2018-05-11 09:26:51 -0700 | [diff] [blame] | 54 | public void testGrant() { |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 55 | File sliceDir = new File(mContext.getCacheDir(), "testGrantSlices"); |
| 56 | Log.v(TAG, "testGrant: slice permissions stored in " + sliceDir.getAbsolutePath()); |
Jason Monk | 9c03ef4 | 2018-05-11 09:26:51 -0700 | [diff] [blame] | 57 | 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 Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 66 | |
| 67 | // Cleanup. |
| 68 | assertTrue(FileUtils.deleteContentsAndDir(sliceDir)); |
Jason Monk | 9c03ef4 | 2018-05-11 09:26:51 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Test |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 72 | public void testBackup() throws XmlPullParserException, IOException { |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 73 | File sliceDir = new File(mContext.getCacheDir(), "testBackupSlices"); |
| 74 | Log.v(TAG, "testBackup: slice permissions stored in " + sliceDir.getAbsolutePath()); |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 75 | 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 Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 101 | 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 Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 105 | 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 Sandler | 2d8e3d1 | 2018-12-13 15:32:13 -0500 | [diff] [blame] | 115 | @Test |
Dan Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 116 | public void testInvalid() { |
| 117 | File sliceDir = new File(mContext.getCacheDir(), "testInvalidSlices"); |
| 118 | Log.v(TAG, "testInvalid: slice permissions stored in " + sliceDir.getAbsolutePath()); |
Dan Sandler | 2d8e3d1 | 2018-12-13 15:32:13 -0500 | [diff] [blame] | 119 | 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 Sandler | e731681 | 2019-01-10 11:24:19 -0500 | [diff] [blame] | 133 | throw new RuntimeException("this RuntimeException inside junk.writeTo() " |
| 134 | + "should be caught and suppressed by surrounding code"); |
Dan Sandler | 2d8e3d1 | 2018-12-13 15:32:13 -0500 | [diff] [blame] | 135 | } |
| 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 | } |