Home | History | Annotate | Download | only in pm
      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;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertSame;
     22 import static org.junit.Assert.assertTrue;
     23 import static org.junit.Assert.fail;
     24 
     25 import android.content.Context;
     26 import android.os.FileUtils;
     27 import android.os.Process;
     28 import android.os.ServiceManager;
     29 import android.os.UserManager;
     30 import android.support.test.InstrumentationRegistry;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.util.Log;
     33 
     34 import org.junit.After;
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 import java.io.File;
     40 import java.io.IOException;
     41 
     42 /**
     43  * This test needs to be run without any secondary users on the device,
     44  * and selinux needs to be disabled with "adb shell setenforce 0".
     45  */
     46 @RunWith(AndroidJUnit4.class)
     47 public class KernelPackageMappingTests {
     48 
     49     private static final String TAG = "KernelPackageMapping";
     50     private static final String SDCARDFS_PATH = "/config/sdcardfs";
     51 
     52     private UserInfo mSecondaryUser;
     53 
     54     private static File getKernelPackageDir(String packageName) {
     55         return new File(new File(SDCARDFS_PATH), packageName);
     56     }
     57 
     58     private static File getKernelPackageFile(String packageName, String filename) {
     59         return new File(getKernelPackageDir(packageName), filename);
     60     }
     61 
     62     private UserManager getUserManager() {
     63         UserManager um = (UserManager) InstrumentationRegistry.getContext().getSystemService(
     64                 Context.USER_SERVICE);
     65         return um;
     66     }
     67 
     68     private IPackageManager getIPackageManager() {
     69         IPackageManager ipm = IPackageManager.Stub.asInterface(
     70                 ServiceManager.getService("package"));
     71         return ipm;
     72     }
     73 
     74     private static String getContent(File file) {
     75         try {
     76             return FileUtils.readTextFile(file, 0, null).trim();
     77         } catch (IOException ioe) {
     78             Log.w(TAG, "Couldn't read file " + file.getAbsolutePath() + "\n" + ioe);
     79             return "<error>";
     80         }
     81     }
     82 
     83     @Test
     84     public void testInstalledPrimary() throws Exception {
     85         assertEquals("1000", getContent(getKernelPackageFile("com.android.settings", "appid")));
     86     }
     87 
     88     @Test
     89     public void testInstalledAll() throws Exception {
     90         assertEquals("", getContent(getKernelPackageFile("com.android.settings",
     91                 "excluded_userids")));
     92     }
     93 
     94     @Test
     95     public void testNotInstalledSecondary() throws Exception {
     96         mSecondaryUser = getUserManager().createUser("Secondary", 0);
     97         assertEquals(Integer.toString(mSecondaryUser.id),
     98                 getContent(
     99                         getKernelPackageFile("com.android.frameworks.coretests.packagemanager",
    100                                 "excluded_userids")));
    101     }
    102 
    103     @After
    104     public void shutDown() throws Exception {
    105         if (mSecondaryUser != null) {
    106             getUserManager().removeUser(mSecondaryUser.id);
    107         }
    108     }
    109 }
    110