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
17package com.android.server.pm;
18
19import android.content.pm.UserInfo;
20import android.os.Bundle;
21import android.os.FileUtils;
22import android.os.Parcelable;
23import android.os.UserHandle;
24import android.os.UserManager;
25import android.test.AndroidTestCase;
26import android.test.suitebuilder.annotation.SmallTest;
27import android.util.AtomicFile;
28
29import java.io.File;
30import java.io.IOException;
31import java.util.Arrays;
32
33@SmallTest
34public class UserManagerServiceTest extends AndroidTestCase {
35    private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
36    private File restrictionsFile;
37    private int tempUserId = UserHandle.USER_NULL;
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();
49        if (tempUserId != UserHandle.USER_NULL) {
50            UserManager.get(mContext).removeUser(tempUserId);
51        }
52        super.tearDown();
53    }
54
55    public void testWriteReadApplicationRestrictions() throws IOException {
56        AtomicFile atomicFile = new AtomicFile(restrictionsFile);
57        Bundle bundle = createBundle();
58        UserManagerService.writeApplicationRestrictionsLP(bundle, atomicFile);
59        assertTrue(atomicFile.getBaseFile().exists());
60        String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
61        System.out.println("restrictionsFile: " + s);
62        bundle = UserManagerService.readApplicationRestrictionsLP(atomicFile);
63        System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
64        assertBundle(bundle);
65    }
66
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
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}
122