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 android.telephony.cts; 18 19 import static androidx.test.InstrumentationRegistry.getInstrumentation; 20 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.os.PersistableBundle; 24 import android.service.carrier.CarrierIdentifier; 25 import android.service.carrier.CarrierService; 26 import android.telephony.SubscriptionManager; 27 import android.telephony.TelephonyManager; 28 import android.test.ServiceTestCase; 29 import android.util.Log; 30 31 import com.android.compatibility.common.util.CarrierPrivilegeUtils; 32 33 public class CarrierServiceTest extends ServiceTestCase<CarrierServiceTest.TestCarrierService> { 34 private static final String TAG = CarrierServiceTest.class.getSimpleName(); 35 CarrierServiceTest()36 public CarrierServiceTest() { super(TestCarrierService.class); } 37 38 @Override runTest()39 protected void runTest() throws Throwable { 40 if (!hasCellular()) { 41 Log.e(TAG, "No cellular support, all tests will be skipped."); 42 return; 43 } 44 super.runTest(); 45 } 46 hasCellular()47 private static boolean hasCellular() { 48 PackageManager packageManager = getInstrumentation().getContext().getPackageManager(); 49 TelephonyManager telephonyManager = 50 getInstrumentation().getContext().getSystemService(TelephonyManager.class); 51 return packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_SUBSCRIPTION) 52 && telephonyManager.getPhoneCount() > 0; 53 } 54 testNotifyCarrierNetworkChange_true()55 public void testNotifyCarrierNetworkChange_true() { 56 notifyCarrierNetworkChangeWithoutCarrierPrivileges(/*active=*/ true); 57 notifyCarrierNetworkChangeWithCarrierPrivileges(/*active=*/ true); 58 } 59 testNotifyCarrierNetworkChange_false()60 public void testNotifyCarrierNetworkChange_false() { 61 notifyCarrierNetworkChangeWithoutCarrierPrivileges(/*active=*/ false); 62 notifyCarrierNetworkChangeWithCarrierPrivileges(/*active=*/ false); 63 } 64 notifyCarrierNetworkChangeWithoutCarrierPrivileges(boolean active)65 private void notifyCarrierNetworkChangeWithoutCarrierPrivileges(boolean active) { 66 Intent intent = new Intent(getContext(), TestCarrierService.class); 67 startService(intent); 68 69 try { 70 getService().notifyCarrierNetworkChange(active); 71 fail("Expected SecurityException for notifyCarrierNetworkChange(" + active + ")"); 72 } catch (SecurityException expected) { 73 } 74 } 75 notifyCarrierNetworkChangeWithCarrierPrivileges(boolean active)76 private void notifyCarrierNetworkChangeWithCarrierPrivileges(boolean active) { 77 Intent intent = new Intent(getContext(), TestCarrierService.class); 78 startService(intent); 79 80 try { 81 CarrierPrivilegeUtils.withCarrierPrivileges( 82 getContext(), 83 SubscriptionManager.getDefaultSubscriptionId(), 84 () -> getService().notifyCarrierNetworkChange(active)); 85 } catch (SecurityException se) { 86 fail("notifyCarrierNetworkChange should not throw SecurityException when has carrier " 87 + "privileges"); 88 } catch (Exception e) { 89 fail("Exception thrown when try to get carrier privileges."); 90 } 91 } 92 testNotifyCarrierNetworkChangeWithSubId_true()93 public void testNotifyCarrierNetworkChangeWithSubId_true() { 94 notifyCarrierNetworkChangeForSubIdWithoutCarrierPrivileges(/*active=*/ true); 95 notifyCarrierNetworkChangeForSubIdWithCarrierPrivileges(/*active=*/true); 96 } 97 testNotifyCarrierNetworkChangeWithSubId_false()98 public void testNotifyCarrierNetworkChangeWithSubId_false() { 99 notifyCarrierNetworkChangeForSubIdWithoutCarrierPrivileges(/*active=*/ false); 100 notifyCarrierNetworkChangeForSubIdWithCarrierPrivileges(/*active=*/false); 101 } 102 notifyCarrierNetworkChangeForSubIdWithoutCarrierPrivileges(boolean active)103 private void notifyCarrierNetworkChangeForSubIdWithoutCarrierPrivileges(boolean active) { 104 Intent intent = new Intent(getContext(), TestCarrierService.class); 105 startService(intent); 106 107 try { 108 int subId = SubscriptionManager.getDefaultSubscriptionId(); 109 getService().notifyCarrierNetworkChange(subId, active); 110 fail("Expected SecurityException for notifyCarrierNetworkChangeWithSubId(" + subId 111 + ", " + active + ")"); 112 } catch (SecurityException expected) { 113 } 114 } 115 notifyCarrierNetworkChangeForSubIdWithCarrierPrivileges(boolean active)116 private void notifyCarrierNetworkChangeForSubIdWithCarrierPrivileges(boolean active) { 117 Intent intent = new Intent(getContext(), TestCarrierService.class); 118 startService(intent); 119 120 try { 121 int subId = SubscriptionManager.getDefaultSubscriptionId(); 122 CarrierPrivilegeUtils.withCarrierPrivileges( 123 getContext(), 124 subId, 125 () -> getService().notifyCarrierNetworkChange(subId, active)); 126 } catch (SecurityException securityException) { 127 fail("notifyCarrierNetworkChange with subId should not throw SecurityException when " 128 + "has carrier privileges"); 129 } catch (Exception e) { 130 fail("Exception thrown when try to get carrier privileges."); 131 } 132 } 133 134 public static class TestCarrierService extends CarrierService { 135 @Override onLoadConfig(CarrierIdentifier id)136 public PersistableBundle onLoadConfig(CarrierIdentifier id) { 137 return null; 138 } onLoadConfig(int subId, CarrierIdentifier id)139 public PersistableBundle onLoadConfig(int subId, CarrierIdentifier id) { 140 return null; 141 } 142 } 143 } 144