Home | History | Annotate | Download | only in pm
      1 /*
      2  * Copyright (C) 2016 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 com.android.server.pm;
     18 
     19 import android.content.pm.UserInfo;
     20 import android.os.Looper;
     21 import android.os.Parcel;
     22 import android.os.UserManagerInternal;
     23 import android.os.UserHandle;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.support.test.runner.AndroidJUnit4;
     26 import android.support.test.filters.MediumTest;
     27 import android.text.TextUtils;
     28 
     29 import com.android.server.LocalServices;
     30 import com.android.server.pm.UserManagerService.UserData;
     31 
     32 import org.junit.Before;
     33 import org.junit.Test;
     34 import org.junit.runner.RunWith;
     35 
     36 import java.io.ByteArrayInputStream;
     37 import java.io.ByteArrayOutputStream;
     38 import java.io.DataOutputStream;
     39 import java.util.List;
     40 
     41 import static org.junit.Assert.assertEquals;
     42 import static org.junit.Assert.assertFalse;
     43 import static org.junit.Assert.assertTrue;
     44 
     45 /**
     46  * <p>Run with:<pre>
     47  * runtest -c com.android.server.pm.UserManagerServiceUserInfoTest frameworks-services
     48  * </pre>
     49  */
     50 @RunWith(AndroidJUnit4.class)
     51 @MediumTest
     52 public class UserManagerServiceUserInfoTest {
     53     private UserManagerService mUserManagerService;
     54 
     55     @Before
     56     public void setup() {
     57         // Currently UserManagerService cannot be instantiated twice inside a VM without a cleanup
     58         // TODO: Remove once UMS supports proper dependency injection
     59         if (Looper.myLooper() == null) {
     60             Looper.prepare();
     61         }
     62         LocalServices.removeServiceForTest(UserManagerInternal.class);
     63         mUserManagerService = new UserManagerService(InstrumentationRegistry.getContext());
     64 
     65         // The tests assume that the device has one user and its the system user.
     66         List<UserInfo> users = mUserManagerService.getUsers(/* excludeDying */ false);
     67         assertEquals("Multiple users so this test can't run.", 1, users.size());
     68         assertEquals("Only user present isn't the system user.",
     69                 UserHandle.USER_SYSTEM, users.get(0).id);
     70     }
     71 
     72     @Test
     73     public void testWriteReadUserInfo() throws Exception {
     74         UserData data = new UserData();
     75         data.info = createUser();
     76 
     77         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     78         DataOutputStream out = new DataOutputStream(baos);
     79         mUserManagerService.writeUserLP(data, out);
     80         byte[] bytes = baos.toByteArray();
     81 
     82         UserData read = mUserManagerService.readUserLP(
     83                 data.info.id, new ByteArrayInputStream(bytes));
     84 
     85         assertUserInfoEquals(data.info, read.info);
     86     }
     87 
     88     @Test
     89     public void testParcelUnparcelUserInfo() throws Exception {
     90         UserInfo info = createUser();
     91 
     92         Parcel out = Parcel.obtain();
     93         info.writeToParcel(out, 0);
     94         byte[] data = out.marshall();
     95         out.recycle();
     96 
     97         Parcel in = Parcel.obtain();
     98         in.unmarshall(data, 0, data.length);
     99         in.setDataPosition(0);
    100         UserInfo read = UserInfo.CREATOR.createFromParcel(in);
    101         in.recycle();
    102 
    103         assertUserInfoEquals(info, read);
    104     }
    105 
    106     @Test
    107     public void testGetUserName() throws Exception {
    108         assertFalse("System user name shouldn't be set",
    109                 mUserManagerService.isUserNameSet(UserHandle.USER_SYSTEM));
    110         UserInfo userInfo = mUserManagerService.getUserInfo(UserHandle.USER_SYSTEM);
    111         assertFalse("A system provided name should be returned for primary user",
    112                 TextUtils.isEmpty(userInfo.name));
    113 
    114         userInfo = createUser();
    115         userInfo.partial = false;
    116         final int TEST_ID = 100;
    117         userInfo.id = TEST_ID;
    118         mUserManagerService.putUserInfo(userInfo);
    119         assertTrue("Test user name must be set", mUserManagerService.isUserNameSet(TEST_ID));
    120         assertEquals("A Name", mUserManagerService.getUserInfo(TEST_ID).name);
    121     }
    122 
    123     private UserInfo createUser() {
    124         UserInfo user = new UserInfo(/*id*/ 21, "A Name", "A path", /*flags*/ 0x0ff0ff);
    125         user.serialNumber = 5;
    126         user.creationTime = 4L << 32;
    127         user.lastLoggedInTime = 5L << 32;
    128         user.lastLoggedInFingerprint = "afingerprint";
    129         user.profileGroupId = 45;
    130         user.restrictedProfileParentId = 4;
    131         user.profileBadge = 2;
    132         user.partial = true;
    133         user.guestToRemove = true;
    134         return user;
    135     }
    136 
    137     private void assertUserInfoEquals(UserInfo one, UserInfo two) {
    138         assertEquals("Id not preserved", one.id, two.id);
    139         assertEquals("Name not preserved", one.name, two.name);
    140         assertEquals("Icon path not preserved", one.iconPath, two.iconPath);
    141         assertEquals("Flags not preserved", one.flags, two.flags);
    142         assertEquals("profile group not preserved", one.profileGroupId,
    143                 two.profileGroupId);
    144         assertEquals("restricted profile parent not preseved", one.restrictedProfileParentId,
    145                 two.restrictedProfileParentId);
    146         assertEquals("profile badge not preseved", one.profileBadge, two.profileBadge);
    147         assertEquals("partial not preseved", one.partial, two.partial);
    148         assertEquals("guestToRemove not preseved", one.guestToRemove, two.guestToRemove);
    149     }
    150 }
    151