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; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 25 | import android.test.AndroidTestCase; |
Sudheer Shanka | c32abe8 | 2016-05-24 13:21:43 -0700 | [diff] [blame] | 26 | import android.test.suitebuilder.annotation.SmallTest; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 27 | import android.util.AtomicFile; |
| 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.IOException; |
| 31 | import java.util.Arrays; |
| 32 | |
Sudheer Shanka | c32abe8 | 2016-05-24 13:21:43 -0700 | [diff] [blame] | 33 | @SmallTest |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 34 | public class UserManagerServiceTest extends AndroidTestCase { |
| 35 | private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["}; |
| 36 | private File restrictionsFile; |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 37 | private int tempUserId = UserHandle.USER_NULL; |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 38 | |
| 39 | @Override |
| 40 | protected void setUp() throws Exception { |
| 41 | super.setUp(); |
| 42 | restrictionsFile = new File(mContext.getCacheDir(), "restrictions.xml"); |
| 43 | restrictionsFile.delete(); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | protected void tearDown() throws Exception { |
| 48 | restrictionsFile.delete(); |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 49 | if (tempUserId != UserHandle.USER_NULL) { |
| 50 | UserManager.get(mContext).removeUser(tempUserId); |
| 51 | } |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 52 | super.tearDown(); |
| 53 | } |
| 54 | |
| 55 | public void testWriteReadApplicationRestrictions() throws IOException { |
| 56 | AtomicFile atomicFile = new AtomicFile(restrictionsFile); |
| 57 | Bundle bundle = createBundle(); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame^] | 58 | UserManagerService.writeApplicationRestrictionsLAr(bundle, atomicFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 59 | assertTrue(atomicFile.getBaseFile().exists()); |
| 60 | String s = FileUtils.readTextFile(restrictionsFile, 10000, ""); |
| 61 | System.out.println("restrictionsFile: " + s); |
Fyodor Kupolov | d31cee9 | 2017-09-05 16:31:08 -0700 | [diff] [blame^] | 62 | bundle = UserManagerService.readApplicationRestrictionsLAr(atomicFile); |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 63 | System.out.println("readApplicationRestrictionsLocked bundle: " + bundle); |
| 64 | assertBundle(bundle); |
| 65 | } |
| 66 | |
Xiaohui Chen | b3b9258 | 2015-12-07 11:22:13 -0800 | [diff] [blame] | 67 | public void testAddUserWithAccount() { |
| 68 | UserManager um = UserManager.get(mContext); |
| 69 | UserInfo user = um.createUser("Test User", 0); |
| 70 | assertNotNull(user); |
| 71 | tempUserId = user.id; |
| 72 | String accountName = "Test Account"; |
| 73 | um.setUserAccount(tempUserId, accountName); |
| 74 | assertEquals(accountName, um.getUserAccount(tempUserId)); |
| 75 | } |
| 76 | |
Fyodor Kupolov | 262f995 | 2015-03-23 18:55:11 -0700 | [diff] [blame] | 77 | private Bundle createBundle() { |
| 78 | Bundle result = new Bundle(); |
| 79 | // Tests for 6 allowed types: Integer, Boolean, String, String[], Bundle and Parcelable[] |
| 80 | result.putBoolean("boolean_0", false); |
| 81 | result.putBoolean("boolean_1", true); |
| 82 | result.putInt("integer", 100); |
| 83 | result.putString("empty", ""); |
| 84 | result.putString("string", "text"); |
| 85 | result.putStringArray("string[]", STRING_ARRAY); |
| 86 | |
| 87 | Bundle bundle = new Bundle(); |
| 88 | bundle.putString("bundle_string", "bundle_string"); |
| 89 | bundle.putInt("bundle_int", 1); |
| 90 | result.putBundle("bundle", bundle); |
| 91 | |
| 92 | Bundle[] bundleArray = new Bundle[2]; |
| 93 | bundleArray[0] = new Bundle(); |
| 94 | bundleArray[0].putString("bundle_array_string", "bundle_array_string"); |
| 95 | bundleArray[0].putBundle("bundle_array_bundle", bundle); |
| 96 | bundleArray[1] = new Bundle(); |
| 97 | bundleArray[1].putString("bundle_array_string2", "bundle_array_string2"); |
| 98 | result.putParcelableArray("bundle_array", bundleArray); |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | private void assertBundle(Bundle bundle) { |
| 103 | assertFalse(bundle.getBoolean("boolean_0")); |
| 104 | assertTrue(bundle.getBoolean("boolean_1")); |
| 105 | assertEquals(100, bundle.getInt("integer")); |
| 106 | assertEquals("", bundle.getString("empty")); |
| 107 | assertEquals("text", bundle.getString("string")); |
| 108 | assertEquals(Arrays.asList(STRING_ARRAY), Arrays.asList(bundle.getStringArray("string[]"))); |
| 109 | Parcelable[] bundle_array = bundle.getParcelableArray("bundle_array"); |
| 110 | assertEquals(2, bundle_array.length); |
| 111 | Bundle bundle1 = (Bundle) bundle_array[0]; |
| 112 | assertEquals("bundle_array_string", bundle1.getString("bundle_array_string")); |
| 113 | assertNotNull(bundle1.getBundle("bundle_array_bundle")); |
| 114 | Bundle bundle2 = (Bundle) bundle_array[1]; |
| 115 | assertEquals("bundle_array_string2", bundle2.getString("bundle_array_string2")); |
| 116 | Bundle childBundle = bundle.getBundle("bundle"); |
| 117 | assertEquals("bundle_string", childBundle.getString("bundle_string")); |
| 118 | assertEquals(1, childBundle.getInt("bundle_int")); |
| 119 | } |
| 120 | |
| 121 | } |