• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.app.cts;
18 
19 import static org.junit.Assert.assertSame;
20 
21 import android.os.Parcel;
22 import android.os.Process;
23 import android.os.UserHandle;
24 import android.test.AndroidTestCase;
25 
26 public class UserHandleTest extends AndroidTestCase {
27     private static final int TEST_APP_ID = 1234;
28 
assertSameUserHandle(int userId)29     private static void assertSameUserHandle(int userId) {
30         assertSame(UserHandle.of(userId), UserHandle.of(userId));
31     }
32 
testOf()33     public void testOf() {
34         // We test them separately since technically it's possible for these constants to have
35         // different values than the AOSP contains and are out of the [-1000, 1000] range.
36         assertSameUserHandle(UserHandle.USER_SYSTEM);
37         assertSameUserHandle(UserHandle.USER_ALL);
38         assertSameUserHandle(UserHandle.USER_NULL);
39 
40         for (int userId = -1000; userId <= 1000; userId++) {
41             assertEquals(userId, UserHandle.of(userId).getIdentifier());
42 
43             // Because of the cache, this should always be true.
44             assertSameUserHandle(userId);
45         }
46     }
47 
assertParcel(int userId)48     private static void assertParcel(int userId) {
49         Parcel p = Parcel.obtain();
50         p.writeParcelable(UserHandle.of(userId), 0);
51         p.setDataPosition(0);
52 
53         UserHandle read = p.readParcelable(UserHandleTest.class.getClassLoader());
54 
55         assertEquals(userId, read.getIdentifier());
56 
57         p.recycle();
58     }
59 
testParcel()60     public void testParcel() {
61         for (int i = -1000; i < 100; i++) {
62             assertParcel(i);
63         }
64     }
65 
testGetUid()66     public void testGetUid() {
67         assertEquals(
68                 UserHandle.getUid(UserHandle.USER_ALL, TEST_APP_ID),
69                 UserHandle.ALL.getUid(TEST_APP_ID));
70         assertEquals(
71                 UserHandle.getUid(UserHandle.USER_SYSTEM, TEST_APP_ID),
72                 UserHandle.SYSTEM.getUid(TEST_APP_ID));
73         assertEquals(
74                 UserHandle.ALL.getUid(TEST_APP_ID),
75                 UserHandle.getUid(UserHandle.ALL.getIdentifier(), TEST_APP_ID));
76         assertEquals(
77                 UserHandle.SYSTEM.getUid(TEST_APP_ID),
78                 UserHandle.getUid(UserHandle.SYSTEM.getIdentifier(), TEST_APP_ID));
79     }
80 
testGetSharedAppGid()81     public void testGetSharedAppGid() {
82         assertEquals(UserHandle.getSharedAppGid(Process.ROOT_UID), Process.ROOT_UID);
83         assertEquals(UserHandle.getSharedAppGid(Process.SYSTEM_UID), Process.SYSTEM_UID);
84         assertEquals(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID), 50000);
85         assertEquals(UserHandle.getSharedAppGid(Process.FIRST_APPLICATION_UID + 1000), 51000);
86         assertEquals(UserHandle.getSharedAppGid(android.os.Process.LAST_APPLICATION_UID + 1), -1);
87     }
88 }
89