123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani/* 223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * Copyright (C) 2017 The Android Open Source Project 323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * 423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * Licensed under the Apache License, Version 2.0 (the "License"); 523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * you may not use this file except in compliance with the License. 623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * You may obtain a copy of the License at 723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * 823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * http://www.apache.org/licenses/LICENSE-2.0 923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * 1023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * Unless required by applicable law or agreed to in writing, software 1123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * distributed under the License is distributed on an "AS IS" BASIS, 1223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * See the License for the specific language governing permissions and 1423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * limitations under the License. 1523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani */ 1623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 1723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasanipackage android.content.pm; 1823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 1923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport static org.junit.Assert.assertEquals; 2023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport static org.junit.Assert.assertNotNull; 2123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport static org.junit.Assert.assertSame; 2223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport static org.junit.Assert.assertTrue; 2323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport static org.junit.Assert.fail; 2423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 2523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.content.Context; 2623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.os.FileUtils; 2723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.os.Process; 2823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.os.ServiceManager; 2923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.os.UserManager; 3023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.support.test.InstrumentationRegistry; 3123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.support.test.runner.AndroidJUnit4; 3223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport android.util.Log; 3323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 3423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport org.junit.After; 3523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport org.junit.Before; 3623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport org.junit.Test; 3723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport org.junit.runner.RunWith; 3823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 3923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport java.io.File; 4023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasaniimport java.io.IOException; 4123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 4223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani/** 4323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * This test needs to be run without any secondary users on the device, 4423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani * and selinux needs to be disabled with "adb shell setenforce 0". 4523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani */ 4623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani@RunWith(AndroidJUnit4.class) 4723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasanipublic class KernelPackageMappingTests { 4823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 4923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private static final String TAG = "KernelPackageMapping"; 5023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private static final String SDCARDFS_PATH = "/config/sdcardfs"; 5123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 5223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private UserInfo mSecondaryUser; 5323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 5423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private static File getKernelPackageDir(String packageName) { 5523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return new File(new File(SDCARDFS_PATH), packageName); 5623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 5723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 5823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private static File getKernelPackageFile(String packageName, String filename) { 5923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return new File(getKernelPackageDir(packageName), filename); 6023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 6123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 6223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private UserManager getUserManager() { 6323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani UserManager um = (UserManager) InstrumentationRegistry.getContext().getSystemService( 6423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani Context.USER_SERVICE); 6523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return um; 6623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 6723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 6823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private IPackageManager getIPackageManager() { 6923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani IPackageManager ipm = IPackageManager.Stub.asInterface( 7023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani ServiceManager.getService("package")); 7123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return ipm; 7223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 7323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 7423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani private static String getContent(File file) { 7523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani try { 7623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return FileUtils.readTextFile(file, 0, null).trim(); 7723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } catch (IOException ioe) { 7823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani Log.w(TAG, "Couldn't read file " + file.getAbsolutePath() + "\n" + ioe); 7923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani return "<error>"; 8023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 8123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 8223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 8323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani @Test 8423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani public void testInstalledPrimary() throws Exception { 8523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani assertEquals("1000", getContent(getKernelPackageFile("com.android.settings", "appid"))); 8623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 8723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 8823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani @Test 8923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani public void testInstalledAll() throws Exception { 9023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani assertEquals("", getContent(getKernelPackageFile("com.android.settings", 9123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani "excluded_userids"))); 9223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 9323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 9423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani @Test 9523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani public void testNotInstalledSecondary() throws Exception { 9623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani mSecondaryUser = getUserManager().createUser("Secondary", 0); 9723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani assertEquals(Integer.toString(mSecondaryUser.id), 9823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani getContent( 9923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani getKernelPackageFile("com.android.frameworks.coretests.packagemanager", 10023ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani "excluded_userids"))); 10123ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 10223ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani 10323ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani @After 10423ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani public void shutDown() throws Exception { 10523ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani if (mSecondaryUser != null) { 10623ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani getUserManager().removeUser(mSecondaryUser.id); 10723ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 10823ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani } 10923ab7f54d96386db2b45dc0b352b3451f665e331Amith Yamasani} 110