Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 19 | import android.content.pm.UserInfo; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
| 21 | import android.os.FileUtils; |
| 22 | import android.os.Parcelable; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 23 | import android.os.UserHandle; |
| 24 | import android.os.UserManager; |
felipeal | a1c0dab | 2020-05-12 17:32:23 -0700 | [diff] [blame] | 25 | import android.support.test.uiautomator.UiDevice; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 26 | import android.test.AndroidTestCase; |
Sudheer Shanka | c32abe8 | 2016-05-24 13:21:43 -0700 | [diff] [blame] | 27 | import android.test.suitebuilder.annotation.SmallTest; |
felipeal | a1c0dab | 2020-05-12 17:32:23 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 29 | import android.util.AtomicFile; |
| 30 | |
felipeal | a1c0dab | 2020-05-12 17:32:23 -0700 | [diff] [blame] | 31 | import androidx.test.InstrumentationRegistry; |
| 32 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 33 | import java.io.File; |
| 34 | import java.io.IOException; |
| 35 | import java.util.Arrays; |
| 36 | |
Sudheer Shanka | c32abe8 | 2016-05-24 13:21:43 -0700 | [diff] [blame] | 37 | @SmallTest |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 38 | public class UserManagerServiceTest extends AndroidTestCase { |
| 39 | private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["}; |
| 40 | private File restrictionsFile; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 41 | private int tempUserId = UserHandle.USER_NULL; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 42 | |
| 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 Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 53 | if (tempUserId != UserHandle.USER_NULL) { |
| 54 | UserManager.get(mContext).removeUser(tempUserId); |
| 55 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 56 | super.tearDown(); |
| 57 | } |
| 58 | |
| 59 | public void testWriteReadApplicationRestrictions() throws IOException { |
| 60 | AtomicFile atomicFile = new AtomicFile(restrictionsFile); |
| 61 | Bundle bundle = createBundle(); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 62 | UserManagerService.writeApplicationRestrictionsLAr(bundle, atomicFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 63 | assertTrue(atomicFile.getBaseFile().exists()); |
| 64 | String s = FileUtils.readTextFile(restrictionsFile, 10000, ""); |
| 65 | System.out.println("restrictionsFile: " + s); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame] | 66 | bundle = UserManagerService.readApplicationRestrictionsLAr(atomicFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 67 | System.out.println("readApplicationRestrictionsLocked bundle: " + bundle); |
| 68 | assertBundle(bundle); |
| 69 | } |
| 70 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 71 | 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 | |
felipeal | a1c0dab | 2020-05-12 17:32:23 -0700 | [diff] [blame] | 81 | 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 Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 89 | 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 | |
felipeal | a1c0dab | 2020-05-12 17:32:23 -0700 | [diff] [blame] | 133 | private static String runShellCommand(String cmd) throws Exception { |
| 134 | return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) |
| 135 | .executeShellCommand(cmd); |
| 136 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 137 | } |