Home | History | Annotate | Download | only in devicepolicy
      1 /*
      2  * Copyright (C) 2014 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.devicepolicy;
     18 
     19 import android.app.admin.DeviceAdminReceiver;
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.BroadcastReceiver;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.os.Bundle;
     26 import android.os.UserHandle;
     27 import android.provider.Settings;
     28 import android.test.AndroidTestCase;
     29 import android.test.suitebuilder.annotation.SmallTest;
     30 
     31 import java.io.ByteArrayInputStream;
     32 import java.io.ByteArrayOutputStream;
     33 
     34 /**
     35  * Tests for application restrictions persisting via profile owner:
     36  *   make -j FrameworksServicesTests
     37  *   runtest --path frameworks/base/services/tests/servicestests/ \
     38  *       src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java
     39  */
     40 public class ApplicationRestrictionsTest extends AndroidTestCase {
     41 
     42     static DevicePolicyManager sDpm;
     43     static ComponentName sAdminReceiver;
     44     private static final String RESTRICTED_APP = "com.example.restrictedApp";
     45     static boolean sAddBack = false;
     46 
     47     public static class AdminReceiver extends DeviceAdminReceiver {
     48 
     49         @Override
     50         public void onDisabled(Context context, Intent intent) {
     51             if (sAddBack) {
     52                 sDpm.setActiveAdmin(sAdminReceiver, false);
     53                 sAddBack = false;
     54             }
     55 
     56             super.onDisabled(context, intent);
     57         }
     58     }
     59 
     60     @Override
     61     public void setUp() {
     62         final Context context = getContext();
     63         sAdminReceiver = new ComponentName(mContext.getPackageName(),
     64                 AdminReceiver.class.getName());
     65         sDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
     66         Settings.Secure.putInt(context.getContentResolver(),
     67                 Settings.Secure.USER_SETUP_COMPLETE, 0);
     68         sDpm.setProfileOwner(sAdminReceiver, "Test", UserHandle.myUserId());
     69         Settings.Secure.putInt(context.getContentResolver(),
     70                 Settings.Secure.USER_SETUP_COMPLETE, 1);
     71         // Remove the admin if already registered. It's async, so add it back
     72         // when the admin gets a broadcast. Otherwise add it back right away.
     73         if (sDpm.isAdminActive(sAdminReceiver)) {
     74             sAddBack = true;
     75             sDpm.removeActiveAdmin(sAdminReceiver);
     76         } else {
     77             sDpm.setActiveAdmin(sAdminReceiver, false);
     78         }
     79     }
     80 
     81     @Override
     82     public void tearDown() {
     83         Settings.Secure.putInt(getContext().getContentResolver(),
     84                 Settings.Secure.USER_SETUP_COMPLETE, 0);
     85         sDpm.removeActiveAdmin(sAdminReceiver);
     86         Settings.Secure.putInt(getContext().getContentResolver(),
     87                 Settings.Secure.USER_SETUP_COMPLETE, 1);
     88     }
     89 
     90     public void testSettingRestrictions() {
     91         Bundle restrictions = new Bundle();
     92         restrictions.putString("KEY_STRING", "Foo");
     93         assertNotNull(sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP));
     94         sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
     95         Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
     96         assertNotNull(returned);
     97         assertEquals(returned.size(), 1);
     98         assertEquals(returned.get("KEY_STRING"), "Foo");
     99         sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle());
    100         returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
    101         assertEquals(returned.size(), 0);
    102     }
    103 
    104     public void testRestrictionTypes() {
    105         Bundle restrictions = new Bundle();
    106         restrictions.putString("KEY_STRING", "Foo");
    107         restrictions.putInt("KEY_INT", 7);
    108         restrictions.putBoolean("KEY_BOOLEAN", true);
    109         restrictions.putBoolean("KEY_BOOLEAN_2", false);
    110         restrictions.putString("KEY_STRING_2", "Bar");
    111         restrictions.putStringArray("KEY_STR_ARRAY", new String[] { "Foo", "Bar" });
    112         sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
    113         Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
    114         assertTrue(returned.getBoolean("KEY_BOOLEAN"));
    115         assertFalse(returned.getBoolean("KEY_BOOLEAN_2"));
    116         assertFalse(returned.getBoolean("KEY_BOOLEAN_3"));
    117         assertEquals(returned.getInt("KEY_INT"), 7);
    118         assertTrue(returned.get("KEY_BOOLEAN") instanceof Boolean);
    119         assertTrue(returned.get("KEY_INT") instanceof Integer);
    120         assertEquals(returned.get("KEY_STRING"), "Foo");
    121         assertEquals(returned.get("KEY_STRING_2"), "Bar");
    122         assertTrue(returned.getStringArray("KEY_STR_ARRAY") instanceof String[]);
    123         sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle());
    124     }
    125 
    126     public void testTextEscaping() {
    127         String fancyText = "<This contains XML/> <JSON> "
    128                 + "{ \"One\": { \"OneOne\": \"11\", \"OneTwo\": \"12\" }, \"Two\": \"2\" } <JSON/>";
    129         Bundle restrictions = new Bundle();
    130         restrictions.putString("KEY_FANCY_TEXT", fancyText);
    131         sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions);
    132         Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP);
    133         assertEquals(returned.getString("KEY_FANCY_TEXT"), fancyText);
    134     }
    135 }
    136