blob: d1366144d33bf66fc5b97e16cc1475549506b8c4 [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;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070025import android.test.AndroidTestCase;
Sudheer Shankac32abe82016-05-24 13:21:43 -070026import android.test.suitebuilder.annotation.SmallTest;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070027import android.util.AtomicFile;
28
29import java.io.File;
30import java.io.IOException;
31import java.util.Arrays;
32
Sudheer Shankac32abe82016-05-24 13:21:43 -070033@SmallTest
Fyodor Kupolov262f9952015-03-23 18:55:11 -070034public class UserManagerServiceTest extends AndroidTestCase {
35 private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
36 private File restrictionsFile;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080037 private int tempUserId = UserHandle.USER_NULL;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070038
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 Chenb3b92582015-12-07 11:22:13 -080049 if (tempUserId != UserHandle.USER_NULL) {
50 UserManager.get(mContext).removeUser(tempUserId);
51 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -070052 super.tearDown();
53 }
54
55 public void testWriteReadApplicationRestrictions() throws IOException {
56 AtomicFile atomicFile = new AtomicFile(restrictionsFile);
57 Bundle bundle = createBundle();
Fyodor Kupolovd31cee92017-09-05 16:31:08 -070058 UserManagerService.writeApplicationRestrictionsLAr(bundle, atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070059 assertTrue(atomicFile.getBaseFile().exists());
60 String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
61 System.out.println("restrictionsFile: " + s);
Fyodor Kupolovd31cee92017-09-05 16:31:08 -070062 bundle = UserManagerService.readApplicationRestrictionsLAr(atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070063 System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
64 assertBundle(bundle);
65 }
66
Xiaohui Chenb3b92582015-12-07 11:22:13 -080067 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 Kupolov262f9952015-03-23 18:55:11 -070077 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}