summaryrefslogtreecommitdiffstats
path: root/core/tests/coretests/src/android/content/pm/SELinuxMMACTests.java
blob: f086113ea5e8f01e115cf071eb9acc1383e9ade6 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2012 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 android.content.pm;

import android.content.pm.PackageManagerTests;
import android.content.pm.SELinuxMMAC;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.os.FileUtils;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.DisplayMetrics;
import android.util.Log;

import com.android.frameworks.coretests.R;

import java.io.File;
import java.io.FileReader;
import java.io.InputStream;

public class SELinuxMMACTests extends AndroidTestCase {

    private static final String TAG = "SELinuxMMACTests";

    private static File MAC_INSTALL_TMP;
    private static File APK_INSTALL_TMP;
    private static final String MAC_INSTALL_TMP_NAME = "mac_install_policy";
    private static final String APK_INSTALL_TMP_NAME = "install.apk";

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Need a tmp file to hold the various mmac install files.
        File filesDir = mContext.getFilesDir();
        MAC_INSTALL_TMP = new File(filesDir, MAC_INSTALL_TMP_NAME);
        assertNotNull(MAC_INSTALL_TMP);

        // Need a tmp file to hold the apk
        APK_INSTALL_TMP = new File(filesDir, APK_INSTALL_TMP_NAME);
        assertNotNull(APK_INSTALL_TMP);
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();

        // Just in case still around
        MAC_INSTALL_TMP.delete();
        APK_INSTALL_TMP.delete();
    }

    void failStr(String errMsg) {
        Log.w(TAG, "errMsg="+errMsg);
        fail(errMsg);
    }

    void failStr(Exception e) {
        failStr(e.getMessage());
    }

    private PackageParser.Package parsePackage(Uri packageURI) {
        final String archiveFilePath = packageURI.getPath();
        PackageParser packageParser = new PackageParser(archiveFilePath);
        File sourceFile = new File(archiveFilePath);
        DisplayMetrics metrics = new DisplayMetrics();
        metrics.setToDefaults();
        PackageParser.Package pkg = packageParser.parsePackage(sourceFile,
                                                               archiveFilePath,
                                                               metrics, 0);
        assertNotNull(pkg);
        assertTrue(packageParser.collectCertificates(pkg,0));
        packageParser = null;
        return pkg;
    }

    Uri getResourceURI(int fileResId, File outFile) {
        Resources res = mContext.getResources();
        InputStream is = null;
        try {
            is = res.openRawResource(fileResId);
        } catch (NotFoundException e) {
            failStr("Failed to load resource with id: " + fileResId);
        }
        assertNotNull(is);
        FileUtils.setPermissions(outFile.getPath(),
                                 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
                                 -1, -1);
        assertTrue(FileUtils.copyToFile(is, outFile));
        FileUtils.setPermissions(outFile.getPath(),
                                 FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IRWXO,
                                 -1, -1);
        return Uri.fromFile(outFile);
    }

    /**
     * Takes the policy xml file as a resource, the apk as a resource,
     * and the expected seinfo string.
     * We mock a package install here by calling parsePackage.
     */
    void checkSeinfoWithPolicy(int policyRes, int apkRes,
                               String expectedSeinfo) {
        // grab policy file
        Uri policyURI = getResourceURI(policyRes, MAC_INSTALL_TMP);
        assertNotNull(policyURI);
        // parse the policy file
        boolean ret = SELinuxMMAC.readInstallPolicy(new File(policyURI.getPath()));
        assertTrue(ret);
        // grab the apk
        Uri apkURI = getResourceURI(apkRes, APK_INSTALL_TMP);
        assertNotNull(apkURI);
        // "install" the apk
        PackageParser.Package pkg = parsePackage(apkURI);
        assertNotNull(pkg);
        assertNotNull(pkg.packageName);
        // check for correct seinfo label
        SELinuxMMAC.assignSeinfoValue(pkg);
        String seinfo = pkg.applicationInfo.seinfo;
        if (seinfo == null)
            seinfo = "null";
        assertEquals(expectedSeinfo, seinfo);

        // delete policy and apk
        MAC_INSTALL_TMP.delete();
        APK_INSTALL_TMP.delete();
    }

    /*
     * Requested policy file doesn't exist.
     */
    @LargeTest
    public void testPOLICY_BADPATH() {
        boolean ret = SELinuxMMAC.readInstallPolicy(new File("/d/o/e/s/n/t/e/x/i/s/t"));
        assertFalse(ret);
    }

    /*
     * Requested policy file is null object.
     */
    @LargeTest
    public void testPOLICY_NULL() {
        boolean ret = SELinuxMMAC.readInstallPolicy(null);
        assertFalse(ret);
    }

    /*
     * Parse an apk that should be labeled with signature stanza.
     */
    @LargeTest
    public void testSIGNATURE_LABEL() {
        checkSeinfoWithPolicy(R.raw.mac_permissions_signature, R.raw.signed_platform,
                              "platform");
    }

    /*
     * Parse an apk that should be labeled with default stanza.
     */
    @LargeTest
    public void testDEFAULT_LABEL() {
        checkSeinfoWithPolicy(R.raw.mac_permissions_default, R.raw.signed_platform,
                              "default");
    }

    /*
     * Parse an apk that should be labeled with package stanza.
     */
    @LargeTest
    public void testPACKAGENAME_LABEL() {
        checkSeinfoWithPolicy(R.raw.mac_permissions_package_name, R.raw.signed_platform,
                              "per-package");
    }

    /*
     * Parse an apk that should not be labeled. No matching entry in policy.
     */
    @LargeTest
    public void testNO_MATCHING_POLICY() {
        checkSeinfoWithPolicy(R.raw.mac_permissions_no_match, R.raw.signed_platform,
                              "null");
    }
}