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; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | import android.testing.TestableLooper.RunWithLooper; |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 28 | import android.util.Xml.Encoding; |
| 29 | |
Brett Chabot | 8091d9e | 2019-02-26 14:52:33 -0800 | [diff] [blame] | 30 | import androidx.test.filters.SmallTest; |
| 31 | |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 32 | import com.android.server.UiServiceTestCase; |
| 33 | |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.xmlpull.v1.XmlPullParser; |
| 37 | import org.xmlpull.v1.XmlPullParserException; |
| 38 | import org.xmlpull.v1.XmlPullParserFactory; |
| 39 | import org.xmlpull.v1.XmlSerializer; |
| 40 | |
| 41 | import java.io.ByteArrayInputStream; |
| 42 | import java.io.ByteArrayOutputStream; |
| 43 | import java.io.File; |
| 44 | import java.io.IOException; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @RunWithLooper |
| 49 | public class SlicePermissionManagerTest extends UiServiceTestCase { |
| 50 | |
| 51 | @Test |
Jason Monk | 9c03ef4 | 2018-05-11 09:26:51 -0700 | [diff] [blame] | 52 | 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 Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 66 | 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 | } |