Home | History | Annotate | Download | only in pm
      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 
     19 import android.os.Bundle;
     20 import android.os.FileUtils;
     21 import android.os.Parcelable;
     22 import android.test.AndroidTestCase;
     23 import android.util.AtomicFile;
     24 
     25 import java.io.File;
     26 import java.io.IOException;
     27 import java.util.Arrays;
     28 
     29 public class UserManagerServiceTest extends AndroidTestCase {
     30     private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
     31     private File restrictionsFile;
     32 
     33     @Override
     34     protected void setUp() throws Exception {
     35         super.setUp();
     36         restrictionsFile = new File(mContext.getCacheDir(), "restrictions.xml");
     37         restrictionsFile.delete();
     38     }
     39 
     40     @Override
     41     protected void tearDown() throws Exception {
     42         restrictionsFile.delete();
     43         super.tearDown();
     44     }
     45 
     46     public void testWriteReadApplicationRestrictions() throws IOException {
     47         AtomicFile atomicFile = new AtomicFile(restrictionsFile);
     48         Bundle bundle = createBundle();
     49         UserManagerService.writeApplicationRestrictionsLocked(bundle, atomicFile);
     50         assertTrue(atomicFile.getBaseFile().exists());
     51         String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
     52         System.out.println("restrictionsFile: " + s);
     53         bundle = UserManagerService.readApplicationRestrictionsLocked(atomicFile);
     54         System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
     55         assertBundle(bundle);
     56     }
     57 
     58     private Bundle createBundle() {
     59         Bundle result = new Bundle();
     60         // Tests for 6 allowed types: Integer, Boolean, String, String[], Bundle and Parcelable[]
     61         result.putBoolean("boolean_0", false);
     62         result.putBoolean("boolean_1", true);
     63         result.putInt("integer", 100);
     64         result.putString("empty", "");
     65         result.putString("string", "text");
     66         result.putStringArray("string[]", STRING_ARRAY);
     67 
     68         Bundle bundle = new Bundle();
     69         bundle.putString("bundle_string", "bundle_string");
     70         bundle.putInt("bundle_int", 1);
     71         result.putBundle("bundle", bundle);
     72 
     73         Bundle[] bundleArray = new Bundle[2];
     74         bundleArray[0] = new Bundle();
     75         bundleArray[0].putString("bundle_array_string", "bundle_array_string");
     76         bundleArray[0].putBundle("bundle_array_bundle", bundle);
     77         bundleArray[1] = new Bundle();
     78         bundleArray[1].putString("bundle_array_string2", "bundle_array_string2");
     79         result.putParcelableArray("bundle_array", bundleArray);
     80         return result;
     81     }
     82 
     83     private void assertBundle(Bundle bundle) {
     84         assertFalse(bundle.getBoolean("boolean_0"));
     85         assertTrue(bundle.getBoolean("boolean_1"));
     86         assertEquals(100, bundle.getInt("integer"));
     87         assertEquals("", bundle.getString("empty"));
     88         assertEquals("text", bundle.getString("string"));
     89         assertEquals(Arrays.asList(STRING_ARRAY), Arrays.asList(bundle.getStringArray("string[]")));
     90         Parcelable[] bundle_array = bundle.getParcelableArray("bundle_array");
     91         assertEquals(2, bundle_array.length);
     92         Bundle bundle1 = (Bundle) bundle_array[0];
     93         assertEquals("bundle_array_string", bundle1.getString("bundle_array_string"));
     94         assertNotNull(bundle1.getBundle("bundle_array_bundle"));
     95         Bundle bundle2 = (Bundle) bundle_array[1];
     96         assertEquals("bundle_array_string2", bundle2.getString("bundle_array_string2"));
     97         Bundle childBundle = bundle.getBundle("bundle");
     98         assertEquals("bundle_string", childBundle.getString("bundle_string"));
     99         assertEquals(1, childBundle.getInt("bundle_int"));
    100     }
    101 
    102 }
    103