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 package com.android.car.test; 17 18 import android.car.Car; 19 import android.car.CarNotConnectedException; 20 import android.car.content.pm.AppBlockingPackageInfo; 21 import android.car.content.pm.CarAppBlockingPolicy; 22 import android.car.content.pm.CarPackageManager; 23 import android.test.suitebuilder.annotation.SmallTest; 24 import android.util.Log; 25 26 @SmallTest 27 public class CarPackageManagerTest extends MockedCarTestBase { 28 private static final String TAG = CarPackageManagerTest.class.getSimpleName(); 29 30 private static final int POLLING_MAX_RETRY = 10; 31 private static final long POLLING_SLEEP = 100; 32 33 private CarPackageManager mCarPm; 34 35 @Override 36 protected void setUp() throws Exception { 37 super.setUp(); 38 } 39 40 private void init(boolean policyFromService) throws Exception { 41 TestAppBlockingPolicyService.controlPolicySettingFromService(policyFromService); 42 mCarPm = (CarPackageManager) getCar().getCarManager(Car.PACKAGE_SERVICE); 43 assertNotNull(mCarPm); 44 } 45 46 public void testServiceLaunched() throws Exception { 47 init(true); 48 assertTrue(pollingCheck(new PollingChecker() { 49 @Override 50 public boolean check() { 51 return TestAppBlockingPolicyService.getInstance() != null; 52 } 53 }, POLLING_MAX_RETRY, POLLING_SLEEP)); 54 final String thisPackage = getContext().getPackageName(); 55 final String serviceClassName = "DOES_NOT_MATTER"; 56 assertTrue(pollingCheck(new PollingChecker() { 57 @Override 58 public boolean check() { 59 try { 60 return mCarPm.isServiceAllowedWhileDriving(thisPackage, serviceClassName); 61 } catch (CarNotConnectedException e) { 62 return false; 63 } 64 } 65 }, POLLING_MAX_RETRY, POLLING_SLEEP)); 66 assertTrue(mCarPm.isServiceAllowedWhileDriving(thisPackage, null)); 67 assertFalse(mCarPm.isServiceAllowedWhileDriving(serviceClassName, serviceClassName)); 68 assertFalse(mCarPm.isServiceAllowedWhileDriving(serviceClassName, null)); 69 } 70 71 public void testSettingWhitelist() throws Exception { 72 init(false); 73 final String carServicePackageName = "com.android.car"; 74 final String activityAllowed = "NO_SUCH_ACTIVITY_BUT_ALLOWED"; 75 final String activityNotAllowed = "NO_SUCH_ACTIVITY_AND_NOT_ALLOWED"; 76 final String acticityAllowed2 = "NO_SUCH_ACTIVITY_BUT_ALLOWED2"; 77 final String thisPackage = getContext().getPackageName(); 78 79 AppBlockingPackageInfo info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 80 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 81 CarAppBlockingPolicy policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 82 , null); 83 Log.i(TAG, "setting policy"); 84 mCarPm.setAppBlockingPolicy(thisPackage, policy, 85 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE); 86 Log.i(TAG, "setting policy done"); 87 assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed)); 88 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, 89 activityNotAllowed)); 90 91 // replace policy 92 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 93 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { acticityAllowed2 }); 94 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 95 , null); 96 mCarPm.setAppBlockingPolicy(thisPackage, policy, 97 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE); 98 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed)); 99 assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2)); 100 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, 101 activityNotAllowed)); 102 103 //add, it replace the whole package policy. So activities are not added. 104 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 105 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 106 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 107 , null); 108 mCarPm.setAppBlockingPolicy(thisPackage, policy, 109 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE | 110 CarPackageManager.FLAG_SET_POLICY_ADD); 111 assertTrue(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed)); 112 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2)); 113 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, 114 activityNotAllowed)); 115 116 //remove 117 info = new AppBlockingPackageInfo(carServicePackageName, 0, 0, 118 AppBlockingPackageInfo.FLAG_SYSTEM_APP, null, new String[] { activityAllowed }); 119 policy = new CarAppBlockingPolicy(new AppBlockingPackageInfo[] { info } 120 , null); 121 mCarPm.setAppBlockingPolicy(thisPackage, policy, 122 CarPackageManager.FLAG_SET_POLICY_WAIT_FOR_CHANGE | 123 CarPackageManager.FLAG_SET_POLICY_REMOVE); 124 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, activityAllowed)); 125 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, acticityAllowed2)); 126 assertFalse(mCarPm.isActivityAllowedWhileDriving(carServicePackageName, 127 activityNotAllowed)); 128 } 129 130 interface PollingChecker { 131 boolean check(); 132 } 133 134 static boolean pollingCheck(PollingChecker checker, int maxRetry, long sleepMs) 135 throws Exception { 136 int retry = 0; 137 boolean checked = checker.check(); 138 while (!checked && (retry < maxRetry)) { 139 Thread.sleep(sleepMs); 140 retry++; 141 checked = checker.check(); 142 } 143 return checked; 144 } 145 } 146