blob: 6c1c019f504e3227e57f0abae1a7eba11f44f68d [file] [log] [blame]
Fyodor Kupolov262f9952015-03-23 18:55:11 -07001/*
2 * Copyright (C) 2015 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
17package com.android.server.pm;
18
Xiaohui Chenb3b92582015-12-07 11:22:13 -080019import android.content.pm.UserInfo;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070020import android.os.Bundle;
21import android.os.FileUtils;
22import android.os.Parcelable;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080023import android.os.UserHandle;
24import android.os.UserManager;
felipeala1c0dab2020-05-12 17:32:23 -070025import android.support.test.uiautomator.UiDevice;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070026import android.test.AndroidTestCase;
Sudheer Shankac32abe82016-05-24 13:21:43 -070027import android.test.suitebuilder.annotation.SmallTest;
felipeala1c0dab2020-05-12 17:32:23 -070028import android.text.TextUtils;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070029import android.util.AtomicFile;
30
felipeala1c0dab2020-05-12 17:32:23 -070031import androidx.test.InstrumentationRegistry;
32
Fyodor Kupolov262f9952015-03-23 18:55:11 -070033import java.io.File;
34import java.io.IOException;
35import java.util.Arrays;
36
Sudheer Shankac32abe82016-05-24 13:21:43 -070037@SmallTest
Fyodor Kupolov262f9952015-03-23 18:55:11 -070038public class UserManagerServiceTest extends AndroidTestCase {
39 private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
40 private File restrictionsFile;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080041 private int tempUserId = UserHandle.USER_NULL;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070042
43 @Override
44 protected void setUp() throws Exception {
45 super.setUp();
46 restrictionsFile = new File(mContext.getCacheDir(), "restrictions.xml");
47 restrictionsFile.delete();
48 }
49
50 @Override
51 protected void tearDown() throws Exception {
52 restrictionsFile.delete();
Xiaohui Chenb3b92582015-12-07 11:22:13 -080053 if (tempUserId != UserHandle.USER_NULL) {
54 UserManager.get(mContext).removeUser(tempUserId);
55 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -070056 super.tearDown();
57 }
58
59 public void testWriteReadApplicationRestrictions() throws IOException {
60 AtomicFile atomicFile = new AtomicFile(restrictionsFile);
61 Bundle bundle = createBundle();
Fyodor Kupolovd31cee92017-09-05 16:31:08 -070062 UserManagerService.writeApplicationRestrictionsLAr(bundle, atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070063 assertTrue(atomicFile.getBaseFile().exists());
64 String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
65 System.out.println("restrictionsFile: " + s);
Fyodor Kupolovd31cee92017-09-05 16:31:08 -070066 bundle = UserManagerService.readApplicationRestrictionsLAr(atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070067 System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
68 assertBundle(bundle);
69 }
70
Xiaohui Chenb3b92582015-12-07 11:22:13 -080071 public void testAddUserWithAccount() {
72 UserManager um = UserManager.get(mContext);
73 UserInfo user = um.createUser("Test User", 0);
74 assertNotNull(user);
75 tempUserId = user.id;
76 String accountName = "Test Account";
77 um.setUserAccount(tempUserId, accountName);
78 assertEquals(accountName, um.getUserAccount(tempUserId));
79 }
80
felipeala1c0dab2020-05-12 17:32:23 -070081 public void testUserSystemPackageWhitelist() throws Exception {
82 String cmd = "cmd user report-system-user-package-whitelist-problems --critical-only";
83 final String result = runShellCommand(cmd);
84 if (!TextUtils.isEmpty(result)) {
85 fail("Command '" + cmd + " reported errors:\n" + result);
86 }
87 }
88
Fyodor Kupolov262f9952015-03-23 18:55:11 -070089 private Bundle createBundle() {
90 Bundle result = new Bundle();
91 // Tests for 6 allowed types: Integer, Boolean, String, String[], Bundle and Parcelable[]
92 result.putBoolean("boolean_0", false);
93 result.putBoolean("boolean_1", true);
94 result.putInt("integer", 100);
95 result.putString("empty", "");
96 result.putString("string", "text");
97 result.putStringArray("string[]", STRING_ARRAY);
98
99 Bundle bundle = new Bundle();
100 bundle.putString("bundle_string", "bundle_string");
101 bundle.putInt("bundle_int", 1);
102 result.putBundle("bundle", bundle);
103
104 Bundle[] bundleArray = new Bundle[2];
105 bundleArray[0] = new Bundle();
106 bundleArray[0].putString("bundle_array_string", "bundle_array_string");
107 bundleArray[0].putBundle("bundle_array_bundle", bundle);
108 bundleArray[1] = new Bundle();
109 bundleArray[1].putString("bundle_array_string2", "bundle_array_string2");
110 result.putParcelableArray("bundle_array", bundleArray);
111 return result;
112 }
113
114 private void assertBundle(Bundle bundle) {
115 assertFalse(bundle.getBoolean("boolean_0"));
116 assertTrue(bundle.getBoolean("boolean_1"));
117 assertEquals(100, bundle.getInt("integer"));
118 assertEquals("", bundle.getString("empty"));
119 assertEquals("text", bundle.getString("string"));
120 assertEquals(Arrays.asList(STRING_ARRAY), Arrays.asList(bundle.getStringArray("string[]")));
121 Parcelable[] bundle_array = bundle.getParcelableArray("bundle_array");
122 assertEquals(2, bundle_array.length);
123 Bundle bundle1 = (Bundle) bundle_array[0];
124 assertEquals("bundle_array_string", bundle1.getString("bundle_array_string"));
125 assertNotNull(bundle1.getBundle("bundle_array_bundle"));
126 Bundle bundle2 = (Bundle) bundle_array[1];
127 assertEquals("bundle_array_string2", bundle2.getString("bundle_array_string2"));
128 Bundle childBundle = bundle.getBundle("bundle");
129 assertEquals("bundle_string", childBundle.getString("bundle_string"));
130 assertEquals(1, childBundle.getInt("bundle_int"));
131 }
132
felipeala1c0dab2020-05-12 17:32:23 -0700133 private static String runShellCommand(String cmd) throws Exception {
134 return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
135 .executeShellCommand(cmd);
136 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -0700137}