summaryrefslogtreecommitdiff
path: root/services/tests/uiservicestests/src/com/android/server/slice/SlicePermissionManagerTest.java
blob: b9c979bb32302fd3fc133adf20b2243cb12f8b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.server.slice;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.FileUtils;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.testing.TestableLooper.RunWithLooper;
import android.util.Xml.Encoding;

import androidx.test.filters.SmallTest;

import com.android.server.UiServiceTestCase;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

@SmallTest
@RunWith(AndroidTestingRunner.class)
@RunWithLooper
public class SlicePermissionManagerTest extends UiServiceTestCase {

    @Test
    public void testGrant() {
        File sliceDir = new File(mContext.getDataDir(), "system/slices");
        SlicePermissionManager permissions = new SlicePermissionManager(mContext,
                TestableLooper.get(this).getLooper(), sliceDir);
        Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
                .authority("authority")
                .path("something").build();

        permissions.grantSliceAccess("my.pkg", 0, "provider.pkg", 0, uri);

        assertTrue(permissions.hasPermission("my.pkg", 0, uri));
    }

    @Test
    public void testBackup() throws XmlPullParserException, IOException {
        File sliceDir = new File(mContext.getDataDir(), "system/slices");
        Uri uri = new Builder().scheme(ContentResolver.SCHEME_CONTENT)
                .authority("authority")
                .path("something").build();
        SlicePermissionManager permissions = new SlicePermissionManager(mContext,
                TestableLooper.get(this).getLooper(), sliceDir);

        permissions.grantFullAccess("com.android.mypkg", 10);
        permissions.grantSliceAccess("com.android.otherpkg", 0, "com.android.lastpkg", 1, uri);

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer();
        serializer.setOutput(output, Encoding.UTF_8.name());


        TestableLooper.get(this).processAllMessages();
        permissions.writeBackup(serializer);
        serializer.flush();

        ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
        XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
        parser.setInput(input, Encoding.UTF_8.name());

        permissions = new SlicePermissionManager(mContext,
                TestableLooper.get(this).getLooper());
        permissions.readRestore(parser);

        assertTrue(permissions.hasFullAccess("com.android.mypkg", 10));
        assertTrue(permissions.hasPermission("com.android.otherpkg", 0,
                ContentProvider.maybeAddUserId(uri, 1)));
        permissions.removePkg("com.android.lastpkg", 1);
        assertFalse(permissions.hasPermission("com.android.otherpkg", 0,
                ContentProvider.maybeAddUserId(uri, 1)));

        // Cleanup.
        assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
    }

    @Test
    public void testInvalid() throws Exception {
        File sliceDir = new File(mContext.getCacheDir(), "slices-test");
        if (!sliceDir.exists()) {
            sliceDir.mkdir();
        }
        SlicePermissionManager permissions = new SlicePermissionManager(mContext,
                TestableLooper.get(this).getLooper(), sliceDir);

        DirtyTracker.Persistable junk = new DirtyTracker.Persistable() {
            @Override
            public String getFileName() {
                return "invalidData";
            }

            @Override
            public void writeTo(XmlSerializer out) throws IOException {
                throw new RuntimeException("this doesn't work");
            }
        };

        // let's put something bad in here
        permissions.addDirtyImmediate(junk);
        // force a persist. if this throws, it would take down system_server
        permissions.handlePersist();

        // Cleanup.
        assertTrue(FileUtils.deleteContentsAndDir(sliceDir));
    }

}