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