Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content.pm; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.os.FileUtils; |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 23 | import android.os.ServiceManager; |
| 24 | import android.os.UserManager; |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
KOUSHIK PANUGANTI | 9322512 | 2018-12-17 18:04:30 -0800 | [diff] [blame] | 27 | import androidx.test.InstrumentationRegistry; |
| 28 | import androidx.test.runner.AndroidJUnit4; |
| 29 | |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 30 | import org.junit.After; |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | |
| 34 | import java.io.File; |
| 35 | import 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) |
| 42 | public 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 Shanka | 12783cb | 2018-10-18 08:53:02 -0700 | [diff] [blame] | 84 | public void testSharedInstalledPrimary() throws Exception { |
Sudheer Shanka | d68bd60 | 2018-11-13 17:43:39 -0800 | [diff] [blame] | 85 | assertEquals("1001", getContent(getKernelPackageFile("shared-android.uid.phone", "appid"))); |
Sudheer Shanka | 12783cb | 2018-10-18 08:53:02 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 89 | public void testInstalledAll() throws Exception { |
| 90 | assertEquals("", getContent(getKernelPackageFile("com.android.settings", |
| 91 | "excluded_userids"))); |
| 92 | } |
| 93 | |
| 94 | @Test |
Sudheer Shanka | 12783cb | 2018-10-18 08:53:02 -0700 | [diff] [blame] | 95 | public void testSharedInstalledAll() throws Exception { |
Sudheer Shanka | d68bd60 | 2018-11-13 17:43:39 -0800 | [diff] [blame] | 96 | assertEquals("", getContent(getKernelPackageFile("shared-android.uid.phone", |
Sudheer Shanka | 12783cb | 2018-10-18 08:53:02 -0700 | [diff] [blame] | 97 | "excluded_userids"))); |
| 98 | } |
| 99 | |
| 100 | @Test |
Amith Yamasani | 23ab7f5 | 2017-01-22 17:43:24 -0800 | [diff] [blame] | 101 | 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 | } |