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.permission.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.fail; 22 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.media.AudioManager; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.telephony.TelephonyManager; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import java.util.Collections; 35 36 /** 37 * Test the non-location-related functionality of TelephonyManager. 38 */ 39 @RunWith(AndroidJUnit4.class) 40 public class TelephonyManagerPermissionTest { 41 42 private boolean mHasTelephony; 43 TelephonyManager mTelephonyManager = null; 44 private AudioManager mAudioManager; 45 46 @Before 47 public void setUp() throws Exception { 48 mHasTelephony = getContext().getPackageManager().hasSystemFeature( 49 PackageManager.FEATURE_TELEPHONY); 50 mTelephonyManager = 51 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE); 52 assertNotNull(mTelephonyManager); 53 mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); 54 assertNotNull(mAudioManager); 55 } 56 57 /** 58 * Verify that TelephonyManager.getDeviceId requires Permission. 59 * <p> 60 * Requires Permission: 61 * {@link android.Manifest.permission#READ_PHONE_STATE}. 62 */ 63 @Test 64 public void testGetDeviceId() { 65 if (!mHasTelephony) { 66 return; 67 } 68 69 try { 70 String id = mTelephonyManager.getDeviceId(); 71 fail("Got device ID: " + id); 72 } catch (SecurityException e) { 73 // expected 74 } 75 try { 76 String id = mTelephonyManager.getDeviceId(0); 77 fail("Got device ID: " + id); 78 } catch (SecurityException e) { 79 // expected 80 } 81 } 82 83 /** 84 * Verify that TelephonyManager.getLine1Number requires Permission. 85 * <p> 86 * Requires Permission: 87 * {@link android.Manifest.permission#READ_PHONE_STATE}. 88 */ 89 @Test 90 public void testGetLine1Number() { 91 if (!mHasTelephony) { 92 return; 93 } 94 95 try { 96 String nmbr = mTelephonyManager.getLine1Number(); 97 fail("Got line 1 number: " + nmbr); 98 } catch (SecurityException e) { 99 // expected 100 } 101 } 102 103 /** 104 * Verify that TelephonyManager.getSimSerialNumber requires Permission. 105 * <p> 106 * Requires Permission: 107 * {@link android.Manifest.permission#READ_PHONE_STATE}. 108 */ 109 @Test 110 public void testGetSimSerialNumber() { 111 if (!mHasTelephony) { 112 return; 113 } 114 115 try { 116 String nmbr = mTelephonyManager.getSimSerialNumber(); 117 fail("Got SIM serial number: " + nmbr); 118 } catch (SecurityException e) { 119 // expected 120 } 121 } 122 123 /** 124 * Verify that TelephonyManager.getSubscriberId requires Permission. 125 * <p> 126 * Requires Permission: 127 * {@link android.Manifest.permission#READ_PHONE_STATE}. 128 */ 129 @Test 130 public void testGetSubscriberId() { 131 if (!mHasTelephony) { 132 return; 133 } 134 135 try { 136 String sid = mTelephonyManager.getSubscriberId(); 137 fail("Got subscriber id: " + sid); 138 } catch (SecurityException e) { 139 // expected 140 } 141 } 142 143 /** 144 * Verify that TelephonyManager.getVoiceMailNumber requires Permission. 145 * <p> 146 * Requires Permission: 147 * {@link android.Manifest.permission#READ_PHONE_STATE}. 148 */ 149 @Test 150 public void testVoiceMailNumber() { 151 if (!mHasTelephony) { 152 return; 153 } 154 155 try { 156 String vmnum = mTelephonyManager.getVoiceMailNumber(); 157 fail("Got voicemail number: " + vmnum); 158 } catch (SecurityException e) { 159 // expected 160 } 161 } 162 /** 163 * Verify that AudioManager.setMode requires Permission. 164 * <p> 165 * Requires Permissions: 166 * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS} and 167 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} for 168 * {@link AudioManager#MODE_IN_CALL}. 169 */ 170 @Test 171 public void testSetMode() { 172 if (!mHasTelephony) { 173 return; 174 } 175 int audioMode = mAudioManager.getMode(); 176 mAudioManager.setMode(AudioManager.MODE_IN_CALL); 177 assertEquals(audioMode, mAudioManager.getMode()); 178 } 179 180 /** 181 * Verify that TelephonyManager.setDataEnabled requires Permission. 182 * <p> 183 * Requires Permission: 184 * {@link android.Manifest.permission#MODIFY_PHONE_STATE}. 185 */ 186 @Test 187 public void testSetDataEnabled() { 188 if (!mHasTelephony) { 189 return; 190 } 191 try { 192 mTelephonyManager.setDataEnabled(false); 193 fail("Able to set data enabled"); 194 } catch (SecurityException e) { 195 // expected 196 } 197 } 198 199 /** 200 * Verify that Telephony related broadcasts are protected. 201 */ 202 @Test 203 public void testProtectedBroadcasts() { 204 if (!mHasTelephony) { 205 return; 206 } 207 try { 208 Intent intent = new Intent("android.intent.action.SIM_STATE_CHANGED"); 209 getContext().sendBroadcast(intent); 210 fail("SecurityException expected!"); 211 } catch (SecurityException e) {} 212 try { 213 Intent intent = new Intent("android.intent.action.SERVICE_STATE"); 214 getContext().sendBroadcast(intent); 215 fail("SecurityException expected!"); 216 } catch (SecurityException e) {} 217 try { 218 Intent intent = new Intent("android.telephony.action.DEFAULT_SUBSCRIPTION_CHANGED"); 219 getContext().sendBroadcast(intent); 220 fail("SecurityException expected!"); 221 } catch (SecurityException e) {} 222 try { 223 Intent intent = new Intent( 224 "android.intent.action.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED"); 225 getContext().sendBroadcast(intent); 226 fail("SecurityException expected!"); 227 } catch (SecurityException e) {} 228 try { 229 Intent intent = new Intent( 230 "android.telephony.action.DEFAULT_SMS_SUBSCRIPTION_CHANGED"); 231 getContext().sendBroadcast(intent); 232 fail("SecurityException expected!"); 233 } catch (SecurityException e) {} 234 try { 235 Intent intent = new Intent( 236 "android.intent.action.ACTION_DEFAULT_VOICE_SUBSCRIPTION_CHANGED"); 237 getContext().sendBroadcast(intent); 238 fail("SecurityException expected!"); 239 } catch (SecurityException e) {} 240 try { 241 Intent intent = new Intent("android.intent.action.SIG_STR"); 242 getContext().sendBroadcast(intent); 243 fail("SecurityException expected!"); 244 } catch (SecurityException e) {} 245 try { 246 Intent intent = new Intent("android.provider.Telephony.SECRET_CODE"); 247 getContext().sendBroadcast(intent); 248 fail("SecurityException expected!"); 249 } catch (SecurityException e) {} 250 } 251 252 /** 253 * Verify that TelephonyManager.getImei requires Permission. 254 * <p> 255 * Requires Permission: 256 * {@link android.Manifest.permission#READ_PHONE_STATE}. 257 */ 258 @Test 259 public void testGetImei() { 260 if (!mHasTelephony) { 261 return; 262 } 263 264 try { 265 String imei = mTelephonyManager.getImei(); 266 fail("Got IMEI: " + imei); 267 } catch (SecurityException e) { 268 // expected 269 } 270 try { 271 String imei = mTelephonyManager.getImei(0); 272 fail("Got IMEI: " + imei); 273 } catch (SecurityException e) { 274 // expected 275 } 276 } 277 278 /** 279 * Verify that TelephonyManager.setAllowedCarriers requires Permission. 280 * <p> 281 * Requires Permission: 282 * {@link android.Manifest.permission#MODIFY_PHONE_STATE}. 283 */ 284 @Test 285 public void testSetAllowedCarriers() { 286 if (!mHasTelephony 287 || !getContext().getPackageManager().hasSystemFeature( 288 PackageManager.FEATURE_TELEPHONY_CARRIERLOCK)) { 289 return; 290 } 291 try { 292 mTelephonyManager.setAllowedCarriers(0, Collections.emptyList()); 293 fail("Able to set allowed carriers"); 294 } catch (SecurityException e) { 295 // expected 296 } 297 } 298 299 /** 300 * Verify that TelephonyManager.getAllowedCarriers requires Permission. 301 * <p> 302 * Requires Permission: 303 * {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE}. 304 */ 305 @Test 306 public void testGetAllowedCarriers() { 307 if (!mHasTelephony 308 || !getContext().getPackageManager().hasSystemFeature( 309 PackageManager.FEATURE_TELEPHONY_CARRIERLOCK)) { 310 return; 311 } 312 try { 313 mTelephonyManager.getAllowedCarriers(0); 314 fail("Able to get allowed carriers"); 315 } catch (SecurityException e) { 316 // expected 317 } 318 } 319 320 private static Context getContext() { 321 return InstrumentationRegistry.getContext(); 322 } 323 } 324