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 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; 29import 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 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 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 104}