1 /* 2 * Copyright (C) 2009 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 android.permissionpolicy.cts; 18 19 import static com.android.compatibility.common.util.SystemUtil.runShellCommand; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.content.BroadcastReceiver; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.content.pm.PackageManager; 29 import android.net.Uri; 30 import android.os.Bundle; 31 import android.platform.test.annotations.AppModeFull; 32 import android.telecom.TelecomManager; 33 import android.util.Log; 34 35 import androidx.test.platform.app.InstrumentationRegistry; 36 37 import com.android.compatibility.common.util.SystemUtil; 38 39 import org.junit.After; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.Test; 43 44 import java.util.concurrent.CountDownLatch; 45 import java.util.concurrent.TimeUnit; 46 47 /** 48 * Verify that processing outgoing calls requires Permission. 49 */ 50 @AppModeFull(reason = "Instant apps cannot hold PROCESS_OUTGOING_CALL") 51 public class NoProcessOutgoingCallPermissionTest { 52 private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext(); 53 54 // Time to wait for call to be placed. 55 private static final int CALL_START_WAIT_TIME_SEC = 30; 56 // Time to wait for a broadcast to be received after we verify that the test app which has 57 // the proper permission got the broadcast 58 private static final int POST_CALL_START_WAIT_TIME_SEC = 5; 59 60 private static final String APK_INSTALL_LOCATION = 61 "/data/local/tmp/cts/permissions2/CtsProcessOutgoingCalls.apk"; 62 private static final String LOG_TAG = "NoProcessOutgoingCallPermissionTest"; 63 64 private static final String ACTION_TEST_APP_RECEIVED_CALL = 65 "android.permissionpolicy.cts.TEST_APP_RECEIVED_CALL"; 66 private static final String TEST_PKG_NAME = "android.permissionpolicy.cts.receivecallbroadcast"; 67 68 private final CountDownLatch mTestAppBroadcastLatch = new CountDownLatch(1); 69 private final CountDownLatch mSystemBroadcastLatch = new CountDownLatch(1); 70 callPhone()71 private void callPhone() { 72 Uri uri = Uri.parse("tel:123456"); 73 Intent intent = new Intent(Intent.ACTION_CALL, uri); 74 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 75 mContext.startActivity(intent); 76 Log.i(LOG_TAG, "Called phone: " + uri.toString()); 77 } 78 79 /** 80 * Verify that to process an outgoing call requires Permission. 81 * <p>Tests Permission: 82 * {@link android.Manifest.permission#PROCESS_OUTGOING_CALLS} 83 */ 84 // TODO: add back to LargeTest when test can cancel initiated call 85 @Test testProcessOutgoingCall()86 public void testProcessOutgoingCall() throws InterruptedException { 87 final PackageManager pm = mContext.getPackageManager(); 88 if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) || 89 !pm.hasSystemFeature(PackageManager.FEATURE_SIP_VOIP)) { 90 return; 91 } 92 93 OutgoingCallBroadcastReceiver rcvr = new OutgoingCallBroadcastReceiver(); 94 IntentFilter filter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL); 95 filter.addAction(ACTION_TEST_APP_RECEIVED_CALL); 96 mContext.registerReceiver(rcvr, filter, Context.RECEIVER_EXPORTED); 97 // start the test app, so that it can receive the broadcast 98 mContext.startActivity(new Intent().setComponent(new ComponentName(TEST_PKG_NAME, 99 TEST_PKG_NAME + ".ProcessOutgoingCallReceiver$BaseActivity")) 100 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); 101 102 callPhone(); 103 104 boolean testAppGotBroadcast = 105 mTestAppBroadcastLatch.await(CALL_START_WAIT_TIME_SEC, TimeUnit.SECONDS); 106 Assert.assertTrue("Expected test app to receive broadcast within " 107 + CALL_START_WAIT_TIME_SEC + " seconds", testAppGotBroadcast); 108 boolean testClassGotBroadcast = 109 mSystemBroadcastLatch.await(POST_CALL_START_WAIT_TIME_SEC, TimeUnit.SECONDS); 110 Assert.assertFalse("Outgoing call processed without proper permissions", 111 testClassGotBroadcast); 112 } 113 114 @Before installApp()115 public void installApp() { 116 String installResult = runShellCommand("pm install -g " + APK_INSTALL_LOCATION); 117 assertThat(installResult.trim()).isEqualTo("Success"); 118 } 119 120 @After endCall()121 public void endCall() { 122 SystemUtil.runWithShellPermissionIdentity(() -> { 123 mContext.getSystemService(TelecomManager.class).endCall(); 124 }); 125 runShellCommand("pm uninstall " + TEST_PKG_NAME); 126 } 127 128 public class OutgoingCallBroadcastReceiver extends BroadcastReceiver { onReceive(Context context, Intent intent)129 public void onReceive(Context context, Intent intent) { 130 if (ACTION_TEST_APP_RECEIVED_CALL.equals(intent.getAction())) { 131 mTestAppBroadcastLatch.countDown(); 132 return; 133 } 134 Bundle xtrs = intent.getExtras(); 135 Log.e(LOG_TAG, xtrs.toString()); 136 mSystemBroadcastLatch.countDown(); 137 } 138 } 139 140 } 141 142