Home | History | Annotate | Download | only in comp
      1 /*
      2  * Copyright (C) 2017 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 package com.android.cts.comp;
     17 
     18 import static junit.framework.Assert.fail;
     19 
     20 import android.app.admin.DevicePolicyManager;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.ServiceConnection;
     25 import android.os.IBinder;
     26 import android.os.UserHandle;
     27 import android.test.AndroidTestCase;
     28 import android.test.MoreAsserts;
     29 import java.util.List;
     30 
     31 /**
     32  * Test class called when binding to a service across users should not work for some reason.
     33  */
     34 public class BindDeviceAdminServiceFailsTest extends AndroidTestCase {
     35 
     36     private DevicePolicyManager mDpm;
     37 
     38     private static final ServiceConnection EMPTY_SERVICE_CONNECTION = new ServiceConnection() {
     39         @Override
     40         public void onServiceConnected(ComponentName name, IBinder service) {}
     41 
     42         @Override
     43         public void onServiceDisconnected(ComponentName name) {}
     44     };
     45 
     46     @Override
     47     public void setUp() {
     48         mDpm = (DevicePolicyManager)
     49                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
     50     }
     51 
     52     public void testNoBindDeviceAdminTargetUsers() {
     53         List<UserHandle> allowedTargetUsers = mDpm.getBindDeviceAdminTargetUsers(
     54                 AdminReceiver.getComponentName(mContext));
     55         assertEquals(0, allowedTargetUsers.size());
     56     }
     57 
     58     public void testCannotBind() throws Exception {
     59         UserHandle otherProfile = Utils.getOtherProfile(mContext);
     60         if (otherProfile != null) {
     61             checkCannotBind(AdminReceiver.COMP_DPC_PACKAGE_NAME, otherProfile);
     62             checkCannotBind(AdminReceiver.COMP_DPC_2_PACKAGE_NAME, otherProfile);
     63         }
     64     }
     65 
     66     private void checkCannotBind(String targetPackageName, UserHandle otherProfile) {
     67         try {
     68             final Intent serviceIntent = new Intent();
     69             serviceIntent.setClassName(targetPackageName, ProtectedCrossUserService.class.getName());
     70             bind(serviceIntent, EMPTY_SERVICE_CONNECTION, otherProfile);
     71             fail("SecurityException should be thrown");
     72         } catch (SecurityException ex) {
     73             MoreAsserts.assertContainsRegex(
     74                     "Not allowed to bind to target user id", ex.getMessage());
     75         }
     76     }
     77 
     78     private boolean bind(Intent serviceIntent, ServiceConnection serviceConnection,
     79             UserHandle userHandle) {
     80         return mDpm.bindDeviceAdminServiceAsUser(AdminReceiver.getComponentName(mContext),
     81                 serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE, userHandle);
     82     }
     83 }
     84