blob: c5c9700e231689e497d44c3d7b947b479b519f18 [file] [log] [blame]
Amith Yamasani23ab7f52017-01-22 17:43:24 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.content.pm;
18
19import static org.junit.Assert.assertEquals;
Amith Yamasani23ab7f52017-01-22 17:43:24 -080020
21import android.content.Context;
22import android.os.FileUtils;
Amith Yamasani23ab7f52017-01-22 17:43:24 -080023import android.os.ServiceManager;
24import android.os.UserManager;
Amith Yamasani23ab7f52017-01-22 17:43:24 -080025import android.util.Log;
26
KOUSHIK PANUGANTI93225122018-12-17 18:04:30 -080027import androidx.test.InstrumentationRegistry;
28import androidx.test.runner.AndroidJUnit4;
29
Amith Yamasani23ab7f52017-01-22 17:43:24 -080030import org.junit.After;
Amith Yamasani23ab7f52017-01-22 17:43:24 -080031import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.io.File;
35import java.io.IOException;
36
37/**
38 * This test needs to be run without any secondary users on the device,
39 * and selinux needs to be disabled with "adb shell setenforce 0".
40 */
41@RunWith(AndroidJUnit4.class)
42public class KernelPackageMappingTests {
43
44 private static final String TAG = "KernelPackageMapping";
45 private static final String SDCARDFS_PATH = "/config/sdcardfs";
46
47 private UserInfo mSecondaryUser;
48
49 private static File getKernelPackageDir(String packageName) {
50 return new File(new File(SDCARDFS_PATH), packageName);
51 }
52
53 private static File getKernelPackageFile(String packageName, String filename) {
54 return new File(getKernelPackageDir(packageName), filename);
55 }
56
57 private UserManager getUserManager() {
58 UserManager um = (UserManager) InstrumentationRegistry.getContext().getSystemService(
59 Context.USER_SERVICE);
60 return um;
61 }
62
63 private IPackageManager getIPackageManager() {
64 IPackageManager ipm = IPackageManager.Stub.asInterface(
65 ServiceManager.getService("package"));
66 return ipm;
67 }
68
69 private static String getContent(File file) {
70 try {
71 return FileUtils.readTextFile(file, 0, null).trim();
72 } catch (IOException ioe) {
73 Log.w(TAG, "Couldn't read file " + file.getAbsolutePath() + "\n" + ioe);
74 return "<error>";
75 }
76 }
77
78 @Test
79 public void testInstalledPrimary() throws Exception {
80 assertEquals("1000", getContent(getKernelPackageFile("com.android.settings", "appid")));
81 }
82
83 @Test
Sudheer Shanka12783cb2018-10-18 08:53:02 -070084 public void testSharedInstalledPrimary() throws Exception {
Sudheer Shankad68bd602018-11-13 17:43:39 -080085 assertEquals("1001", getContent(getKernelPackageFile("shared-android.uid.phone", "appid")));
Sudheer Shanka12783cb2018-10-18 08:53:02 -070086 }
87
88 @Test
Amith Yamasani23ab7f52017-01-22 17:43:24 -080089 public void testInstalledAll() throws Exception {
90 assertEquals("", getContent(getKernelPackageFile("com.android.settings",
91 "excluded_userids")));
92 }
93
94 @Test
Sudheer Shanka12783cb2018-10-18 08:53:02 -070095 public void testSharedInstalledAll() throws Exception {
Sudheer Shankad68bd602018-11-13 17:43:39 -080096 assertEquals("", getContent(getKernelPackageFile("shared-android.uid.phone",
Sudheer Shanka12783cb2018-10-18 08:53:02 -070097 "excluded_userids")));
98 }
99
100 @Test
Amith Yamasani23ab7f52017-01-22 17:43:24 -0800101 public void testNotInstalledSecondary() throws Exception {
102 mSecondaryUser = getUserManager().createUser("Secondary", 0);
103 assertEquals(Integer.toString(mSecondaryUser.id),
104 getContent(
105 getKernelPackageFile("com.android.frameworks.coretests.packagemanager",
106 "excluded_userids")));
107 }
108
109 @After
110 public void shutDown() throws Exception {
111 if (mSecondaryUser != null) {
112 getUserManager().removeUser(mSecondaryUser.id);
113 }
114 }
115}