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; |
| 19 | |
| 20 | import android.content.ContentProvider; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.net.Uri; |
| 23 | import android.net.Uri.Builder; |
| 24 | import android.os.FileUtils; |
| 25 | import android.support.test.filters.SmallTest; |
| 26 | import android.testing.AndroidTestingRunner; |
| 27 | import android.testing.TestableLooper; |
| 28 | import android.testing.TestableLooper.RunWithLooper; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 29 | import android.util.Xml.Encoding; |
| 30 | |
| 31 | import com.android.server.UiServiceTestCase; |
| 32 | |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.xmlpull.v1.XmlPullParser; |
| 36 | import org.xmlpull.v1.XmlPullParserException; |
| 37 | import org.xmlpull.v1.XmlPullParserFactory; |
| 38 | import org.xmlpull.v1.XmlSerializer; |
| 39 | |
| 40 | import java.io.ByteArrayInputStream; |
| 41 | import java.io.ByteArrayOutputStream; |
| 42 | import java.io.File; |
| 43 | import java.io.IOException; |
| 44 | |
| 45 | @SmallTest |
| 46 | @RunWith(AndroidTestingRunner.class) |
| 47 | @RunWithLooper |
| 48 | public class SlicePermissionManagerTest extends UiServiceTestCase { |
| 49 | |
| 50 | @Test |
Jason Monk | 9c03ef4 | 2018-05-11 09:26:51 -0700 | [diff] [blame] | 51 | 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 Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 65 | 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 Sandler | 98267b3 | 2018-12-13 15:32:13 -0500 | [diff] [blame] | 104 | @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 | } |