blob: 48a11c5cb7f15adf53da484ecab37498f2fa3386 [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;
26import android.util.AtomicFile;
27
28import java.io.File;
29import java.io.IOException;
30import java.util.Arrays;
31
32public class UserManagerServiceTest extends AndroidTestCase {
33 private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
34 private File restrictionsFile;
Xiaohui Chenb3b92582015-12-07 11:22:13 -080035 private int tempUserId = UserHandle.USER_NULL;
Fyodor Kupolov262f9952015-03-23 18:55:11 -070036
37 @Override
38 protected void setUp() throws Exception {
39 super.setUp();
40 restrictionsFile = new File(mContext.getCacheDir(), "restrictions.xml");
41 restrictionsFile.delete();
42 }
43
44 @Override
45 protected void tearDown() throws Exception {
46 restrictionsFile.delete();
Xiaohui Chenb3b92582015-12-07 11:22:13 -080047 if (tempUserId != UserHandle.USER_NULL) {
48 UserManager.get(mContext).removeUser(tempUserId);
49 }
Fyodor Kupolov262f9952015-03-23 18:55:11 -070050 super.tearDown();
51 }
52
53 public void testWriteReadApplicationRestrictions() throws IOException {
54 AtomicFile atomicFile = new AtomicFile(restrictionsFile);
55 Bundle bundle = createBundle();
Fyodor Kupolov82402752015-10-28 14:54:51 -070056 UserManagerService.writeApplicationRestrictionsLP(bundle, atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070057 assertTrue(atomicFile.getBaseFile().exists());
58 String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
59 System.out.println("restrictionsFile: " + s);
Fyodor Kupolov82402752015-10-28 14:54:51 -070060 bundle = UserManagerService.readApplicationRestrictionsLP(atomicFile);
Fyodor Kupolov262f9952015-03-23 18:55:11 -070061 System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
62 assertBundle(bundle);
63 }
64
Xiaohui Chenb3b92582015-12-07 11:22:13 -080065 public void testAddUserWithAccount() {
66 UserManager um = UserManager.get(mContext);
67 UserInfo user = um.createUser("Test User", 0);
68 assertNotNull(user);
69 tempUserId = user.id;
70 String accountName = "Test Account";
71 um.setUserAccount(tempUserId, accountName);
72 assertEquals(accountName, um.getUserAccount(tempUserId));
73 }
74
Fyodor Kupolov262f9952015-03-23 18:55:11 -070075 private Bundle createBundle() {
76 Bundle result = new Bundle();
77 // Tests for 6 allowed types: Integer, Boolean, String, String[], Bundle and Parcelable[]
78 result.putBoolean("boolean_0", false);
79 result.putBoolean("boolean_1", true);
80 result.putInt("integer", 100);
81 result.putString("empty", "");
82 result.putString("string", "text");
83 result.putStringArray("string[]", STRING_ARRAY);
84
85 Bundle bundle = new Bundle();
86 bundle.putString("bundle_string", "bundle_string");
87 bundle.putInt("bundle_int", 1);
88 result.putBundle("bundle", bundle);
89
90 Bundle[] bundleArray = new Bundle[2];
91 bundleArray[0] = new Bundle();
92 bundleArray[0].putString("bundle_array_string", "bundle_array_string");
93 bundleArray[0].putBundle("bundle_array_bundle", bundle);
94 bundleArray[1] = new Bundle();
95 bundleArray[1].putString("bundle_array_string2", "bundle_array_string2");
96 result.putParcelableArray("bundle_array", bundleArray);
97 return result;
98 }
99
100 private void assertBundle(Bundle bundle) {
101 assertFalse(bundle.getBoolean("boolean_0"));
102 assertTrue(bundle.getBoolean("boolean_1"));
103 assertEquals(100, bundle.getInt("integer"));
104 assertEquals("", bundle.getString("empty"));
105 assertEquals("text", bundle.getString("string"));
106 assertEquals(Arrays.asList(STRING_ARRAY), Arrays.asList(bundle.getStringArray("string[]")));
107 Parcelable[] bundle_array = bundle.getParcelableArray("bundle_array");
108 assertEquals(2, bundle_array.length);
109 Bundle bundle1 = (Bundle) bundle_array[0];
110 assertEquals("bundle_array_string", bundle1.getString("bundle_array_string"));
111 assertNotNull(bundle1.getBundle("bundle_array_bundle"));
112 Bundle bundle2 = (Bundle) bundle_array[1];
113 assertEquals("bundle_array_string2", bundle2.getString("bundle_array_string2"));
114 Bundle childBundle = bundle.getBundle("bundle");
115 assertEquals("bundle_string", childBundle.getString("bundle_string"));
116 assertEquals(1, childBundle.getInt("bundle_int"));
117 }
118
119}