1/* 2 * Copyright (C) 2016 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 */ 16package com.android.internal.telephony; 17 18import android.app.AppOpsManager; 19import android.content.Context; 20import org.junit.After; 21import org.junit.Assert; 22import org.junit.Before; 23 24import static android.Manifest.permission.READ_PHONE_STATE; 25import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; 26import static android.Manifest.permission.READ_SMS; 27import static org.junit.Assert.assertEquals; 28import static org.junit.Assert.assertNull; 29import static org.junit.Assert.assertTrue; 30import static org.mockito.Matchers.anyInt; 31import static org.mockito.Mockito.anyString; 32import static org.mockito.Mockito.doReturn; 33import static org.mockito.Mockito.eq; 34import org.junit.Test; 35import org.mockito.Mock; 36 37 38import android.test.suitebuilder.annotation.SmallTest; 39 40public class PhoneSubInfoControllerTest extends TelephonyTest { 41 private PhoneSubInfoController mPhoneSubInfoControllerUT; 42 private AppOpsManager mAppOsMgr; 43 44 @Mock 45 GsmCdmaPhone mSecondPhone; 46 47 @Before 48 public void setUp() throws Exception { 49 super.setUp(getClass().getSimpleName()); 50 /* mPhone -> PhoneId: 0 -> SubId:0 51 mSecondPhone -> PhoneId:1 -> SubId: 1*/ 52 doReturn(0).when(mSubscriptionController).getPhoneId(eq(0)); 53 doReturn(1).when(mSubscriptionController).getPhoneId(eq(1)); 54 doReturn(2).when(mTelephonyManager).getPhoneCount(); 55 56 mServiceManagerMockedServices.put("isub", mSubscriptionController); 57 doReturn(mSubscriptionController).when(mSubscriptionController) 58 .queryLocalInterface(anyString()); 59 doReturn(mContext).when(mSecondPhone).getContext(); 60 61 mAppOsMgr = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); 62 63 mPhoneSubInfoControllerUT = new PhoneSubInfoController(mContext, 64 new Phone[]{mPhone, mSecondPhone}); 65 } 66 67 @After 68 public void tearDown() throws Exception { 69 super.tearDown(); 70 } 71 72 @Test 73 @SmallTest 74 public void testGetDeviceId() { 75 doReturn("353626073736741").when(mPhone).getDeviceId(); 76 doReturn("353626073736742").when(mSecondPhone).getDeviceId(); 77 78 assertEquals("353626073736741", mPhoneSubInfoControllerUT.getDeviceIdForPhone(0, TAG)); 79 assertEquals("353626073736742", mPhoneSubInfoControllerUT.getDeviceIdForPhone(1, TAG)); 80 } 81 82 @Test 83 @SmallTest 84 public void testGetDeviceIdWithOutPermission() { 85 doReturn("353626073736741").when(mPhone).getDeviceId(); 86 doReturn("353626073736742").when(mSecondPhone).getDeviceId(); 87 88 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 89 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 90 try { 91 mPhoneSubInfoControllerUT.getDeviceIdForPhone(0, TAG); 92 Assert.fail("expected Security Exception Thrown"); 93 } catch (Exception ex) { 94 assertTrue(ex instanceof SecurityException); 95 assertEquals(READ_PHONE_STATE + " denied: getDeviceId", ex.getMessage()); 96 } 97 98 try { 99 mPhoneSubInfoControllerUT.getDeviceIdForPhone(1, TAG); 100 Assert.fail("expected Security Exception Thrown"); 101 } catch (Exception ex) { 102 assertTrue(ex instanceof SecurityException); 103 assertEquals(READ_PHONE_STATE + " denied: getDeviceId", ex.getMessage()); 104 } 105 106 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 107 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 108 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 109 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 110 111 assertNull(mPhoneSubInfoControllerUT.getDeviceIdForPhone(0, TAG)); 112 assertNull(mPhoneSubInfoControllerUT.getDeviceIdForPhone(1, TAG)); 113 114 //case 3: no READ_PRIVILEGED_PHONE_STATE 115 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 116 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 117 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 118 assertEquals("353626073736741", mPhoneSubInfoControllerUT.getDeviceIdForPhone(0, TAG)); 119 assertEquals("353626073736742", mPhoneSubInfoControllerUT.getDeviceIdForPhone(1, TAG)); 120 } 121 122 @Test 123 @SmallTest 124 public void testGetNai() { 125 doReturn("aaa@example.com").when(mPhone).getNai(); 126 assertEquals("aaa@example.com", mPhoneSubInfoControllerUT.getNaiForSubscriber(0, TAG)); 127 128 doReturn("bbb@example.com").when(mSecondPhone).getNai(); 129 assertEquals("bbb@example.com", mPhoneSubInfoControllerUT.getNaiForSubscriber(1, TAG)); 130 } 131 132 @Test 133 @SmallTest 134 public void testGetNaiWithOutPermission() { 135 doReturn("aaa@example.com").when(mPhone).getNai(); 136 doReturn("bbb@example.com").when(mSecondPhone).getNai(); 137 138 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 139 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 140 try { 141 mPhoneSubInfoControllerUT.getNaiForSubscriber(0, TAG); 142 Assert.fail("expected Security Exception Thrown"); 143 } catch (Exception ex) { 144 assertTrue(ex instanceof SecurityException); 145 assertEquals(READ_PHONE_STATE + " denied: getNai", ex.getMessage()); 146 } 147 148 try { 149 mPhoneSubInfoControllerUT.getNaiForSubscriber(1, TAG); 150 Assert.fail("expected Security Exception Thrown"); 151 } catch (Exception ex) { 152 assertTrue(ex instanceof SecurityException); 153 assertEquals(READ_PHONE_STATE + " denied: getNai", ex.getMessage()); 154 } 155 156 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 157 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 158 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 159 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 160 161 assertNull(mPhoneSubInfoControllerUT.getNaiForSubscriber(0, TAG)); 162 assertNull(mPhoneSubInfoControllerUT.getNaiForSubscriber(1, TAG)); 163 164 //case 3: no READ_PRIVILEGED_PHONE_STATE 165 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 166 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 167 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 168 assertEquals("aaa@example.com", mPhoneSubInfoControllerUT.getNaiForSubscriber(0, TAG)); 169 assertEquals("bbb@example.com", mPhoneSubInfoControllerUT.getNaiForSubscriber(1, TAG)); 170 } 171 172 @Test 173 @SmallTest 174 public void testGetImei() { 175 doReturn("990000862471854").when(mPhone).getImei(); 176 assertEquals("990000862471854", mPhoneSubInfoControllerUT.getImeiForSubscriber(0, TAG)); 177 178 doReturn("990000862471855").when(mSecondPhone).getImei(); 179 assertEquals("990000862471855", mPhoneSubInfoControllerUT.getImeiForSubscriber(1, TAG)); 180 } 181 182 @Test 183 @SmallTest 184 public void testGetImeiWithOutPermission() { 185 doReturn("990000862471854").when(mPhone).getImei(); 186 doReturn("990000862471855").when(mSecondPhone).getImei(); 187 188 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 189 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 190 try { 191 mPhoneSubInfoControllerUT.getImeiForSubscriber(0, TAG); 192 Assert.fail("expected Security Exception Thrown"); 193 } catch (Exception ex) { 194 assertTrue(ex instanceof SecurityException); 195 assertEquals(READ_PHONE_STATE + " denied: getImei", ex.getMessage()); 196 } 197 198 try { 199 mPhoneSubInfoControllerUT.getImeiForSubscriber(1, TAG); 200 Assert.fail("expected Security Exception Thrown"); 201 } catch (Exception ex) { 202 assertTrue(ex instanceof SecurityException); 203 assertEquals(READ_PHONE_STATE + " denied: getImei", ex.getMessage()); 204 } 205 206 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 207 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 208 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 209 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 210 211 assertNull(mPhoneSubInfoControllerUT.getImeiForSubscriber(0, TAG)); 212 assertNull(mPhoneSubInfoControllerUT.getImeiForSubscriber(1, TAG)); 213 214 //case 3: no READ_PRIVILEGED_PHONE_STATE 215 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 216 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 217 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 218 assertEquals("990000862471854", mPhoneSubInfoControllerUT.getImeiForSubscriber(0, TAG)); 219 assertEquals("990000862471855", mPhoneSubInfoControllerUT.getImeiForSubscriber(1, TAG)); 220 } 221 222 @Test 223 @SmallTest 224 public void testGetDeviceSvn() { 225 doReturn("00").when(mPhone).getDeviceSvn(); 226 assertEquals("00", mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(0, TAG)); 227 228 doReturn("01").when(mSecondPhone).getDeviceSvn(); 229 assertEquals("01", mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(1, TAG)); 230 } 231 232 @Test 233 @SmallTest 234 public void testGetDeviceSvnWithOutPermission() { 235 doReturn("00").when(mPhone).getDeviceSvn(); 236 doReturn("01").when(mSecondPhone).getDeviceSvn(); 237 238 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 239 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 240 try { 241 mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(0, TAG); 242 Assert.fail("expected Security Exception Thrown"); 243 } catch (Exception ex) { 244 assertTrue(ex instanceof SecurityException); 245 assertEquals(READ_PHONE_STATE + " denied: getDeviceSvn", ex.getMessage()); 246 } 247 248 try { 249 mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(1, TAG); 250 Assert.fail("expected Security Exception Thrown"); 251 } catch (Exception ex) { 252 assertTrue(ex instanceof SecurityException); 253 assertEquals(READ_PHONE_STATE + " denied: getDeviceSvn", ex.getMessage()); 254 } 255 256 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 257 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 258 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 259 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 260 261 assertNull(mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(0, TAG)); 262 assertNull(mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(1, TAG)); 263 264 //case 3: no READ_PRIVILEGED_PHONE_STATE 265 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 266 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 267 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 268 assertEquals("00", mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(0, TAG)); 269 assertEquals("01", mPhoneSubInfoControllerUT.getDeviceSvnUsingSubId(1, TAG)); 270 } 271 272 @Test 273 @SmallTest 274 public void testGetSubscriberId() { 275 //IMSI 276 doReturn("310260426283121").when(mPhone).getSubscriberId(); 277 assertEquals("310260426283121", mPhoneSubInfoControllerUT 278 .getSubscriberIdForSubscriber(0, TAG)); 279 280 doReturn("310260426283122").when(mSecondPhone).getSubscriberId(); 281 assertEquals("310260426283122", mPhoneSubInfoControllerUT 282 .getSubscriberIdForSubscriber(1, TAG)); 283 } 284 285 @Test 286 @SmallTest 287 public void testGetSubscriberIdWithOutPermission() { 288 doReturn("310260426283121").when(mPhone).getSubscriberId(); 289 doReturn("310260426283122").when(mSecondPhone).getSubscriberId(); 290 291 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 292 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 293 try { 294 mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(0, TAG); 295 Assert.fail("expected Security Exception Thrown"); 296 } catch (Exception ex) { 297 assertTrue(ex instanceof SecurityException); 298 assertEquals(READ_PHONE_STATE + " denied: getSubscriberId", ex.getMessage()); 299 } 300 301 try { 302 mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(1, TAG); 303 Assert.fail("expected Security Exception Thrown"); 304 } catch (Exception ex) { 305 assertTrue(ex instanceof SecurityException); 306 assertEquals(READ_PHONE_STATE + " denied: getSubscriberId", ex.getMessage()); 307 } 308 309 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 310 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 311 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 312 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 313 314 assertNull(mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(0, TAG)); 315 assertNull(mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(1, TAG)); 316 317 //case 3: no READ_PRIVILEGED_PHONE_STATE 318 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 319 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 320 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 321 assertEquals("310260426283121", 322 mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(0, TAG)); 323 assertEquals("310260426283122", 324 mPhoneSubInfoControllerUT.getSubscriberIdForSubscriber(1, TAG)); 325 } 326 327 @Test 328 @SmallTest 329 public void testGetIccSerialNumber() { 330 //IccId 331 doReturn("8991101200003204510").when(mPhone).getIccSerialNumber(); 332 assertEquals("8991101200003204510", mPhoneSubInfoControllerUT 333 .getIccSerialNumberForSubscriber(0, TAG)); 334 335 doReturn("8991101200003204511").when(mSecondPhone).getIccSerialNumber(); 336 assertEquals("8991101200003204511", mPhoneSubInfoControllerUT 337 .getIccSerialNumberForSubscriber(1, TAG)); 338 } 339 340 @Test 341 @SmallTest 342 public void testGetIccSerialNumberWithOutPermission() { 343 doReturn("8991101200003204510").when(mPhone).getIccSerialNumber(); 344 doReturn("8991101200003204511").when(mSecondPhone).getIccSerialNumber(); 345 346 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 347 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 348 try { 349 mPhoneSubInfoControllerUT.getIccSerialNumberForSubscriber(0, TAG); 350 Assert.fail("expected Security Exception Thrown"); 351 } catch (Exception ex) { 352 assertTrue(ex instanceof SecurityException); 353 assertEquals(READ_PHONE_STATE + " denied: getIccSerialNumber", ex.getMessage()); 354 } 355 356 try { 357 mPhoneSubInfoControllerUT.getIccSerialNumberForSubscriber(1, TAG); 358 Assert.fail("expected Security Exception Thrown"); 359 } catch (Exception ex) { 360 assertTrue(ex instanceof SecurityException); 361 assertEquals(READ_PHONE_STATE + " denied: getIccSerialNumber", ex.getMessage()); 362 } 363 364 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 365 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 366 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 367 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 368 369 assertNull(mPhoneSubInfoControllerUT.getIccSerialNumberForSubscriber(0, TAG)); 370 assertNull(mPhoneSubInfoControllerUT.getIccSerialNumberForSubscriber(1, TAG)); 371 372 //case 3: no READ_PRIVILEGED_PHONE_STATE 373 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 374 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 375 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 376 assertEquals("8991101200003204510", mPhoneSubInfoControllerUT 377 .getIccSerialNumberForSubscriber(0, TAG)); 378 assertEquals("8991101200003204511", mPhoneSubInfoControllerUT 379 .getIccSerialNumberForSubscriber(1, TAG)); 380 } 381 382 @Test 383 @SmallTest 384 public void testLine1Number() { 385 doReturn("+18051234567").when(mPhone).getLine1Number(); 386 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 387 388 doReturn("+18052345678").when(mSecondPhone).getLine1Number(); 389 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 390 } 391 392 @Test 393 @SmallTest 394 public void testLine1NumberWithOutPermission() { 395 doReturn("+18051234567").when(mPhone).getLine1Number(); 396 doReturn("+18052345678").when(mSecondPhone).getLine1Number(); 397 398 /* case 1: no READ_PRIVILEGED_PHONE_STATE & READ_PHONE_STATE & 399 READ_SMS and no OP_WRITE_SMS & OP_READ_SMS from appOsMgr */ 400 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 401 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 402 eq(AppOpsManager.OP_READ_SMS), anyInt(), eq(TAG)); 403 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 404 eq(AppOpsManager.OP_WRITE_SMS), anyInt(), eq(TAG)); 405 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 406 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 407 try { 408 mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG); 409 Assert.fail("expected Security Exception Thrown"); 410 } catch (Exception ex) { 411 assertTrue(ex instanceof SecurityException); 412 } 413 414 try { 415 mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG); 416 Assert.fail("expected Security Exception Thrown"); 417 } catch (Exception ex) { 418 assertTrue(ex instanceof SecurityException); 419 } 420 421 /* case 2: only enable WRITE_SMS permission */ 422 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 423 eq(AppOpsManager.OP_WRITE_SMS), anyInt(), eq(TAG)); 424 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 425 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 426 427 /* case 3: only enable READ_PRIVILEGED_PHONE_STATE */ 428 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 429 eq(AppOpsManager.OP_WRITE_SMS), anyInt(), eq(TAG)); 430 mContextFixture.addCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE); 431 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 432 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 433 434 /* case 4: only enable READ_PHONE_STATE permission */ 435 mContextFixture.removeCallingOrSelfPermission(READ_PRIVILEGED_PHONE_STATE); 436 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 437 assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 438 assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 439 440 /* case 5: enable appOsMgr READ_PHONE_PERMISSION & READ_PHONE_STATE */ 441 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 442 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 443 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 444 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 445 446 /* case 6: only enable READ_SMS */ 447 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 448 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 449 mContextFixture.removeCallingOrSelfPermission(READ_PHONE_STATE); 450 mContextFixture.addCallingOrSelfPermission(READ_SMS); 451 assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 452 assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 453 454 /* case 7: enable READ_SMS and OP_READ_SMS */ 455 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 456 eq(AppOpsManager.OP_READ_SMS), anyInt(), eq(TAG)); 457 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG)); 458 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG)); 459 } 460 461 @Test 462 @SmallTest 463 public void testLine1AlphaTag() { 464 doReturn("LINE1_SIM_0").when(mPhone).getLine1AlphaTag(); 465 assertEquals("LINE1_SIM_0", mPhoneSubInfoControllerUT 466 .getLine1AlphaTagForSubscriber(0, TAG)); 467 468 doReturn("LINE1_SIM_1").when(mSecondPhone).getLine1AlphaTag(); 469 assertEquals("LINE1_SIM_1", mPhoneSubInfoControllerUT 470 .getLine1AlphaTagForSubscriber(1, TAG)); 471 } 472 473 @Test 474 @SmallTest 475 public void testLine1AlphaTagWithOutPermission() { 476 doReturn("LINE1_SIM_0").when(mPhone).getLine1AlphaTag(); 477 doReturn("LINE1_SIM_1").when(mSecondPhone).getLine1AlphaTag(); 478 479 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 480 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 481 try { 482 mPhoneSubInfoControllerUT.getLine1AlphaTagForSubscriber(0, TAG); 483 Assert.fail("expected Security Exception Thrown"); 484 } catch (Exception ex) { 485 assertTrue(ex instanceof SecurityException); 486 assertEquals(READ_PHONE_STATE + " denied: getLine1AlphaTag", ex.getMessage()); 487 } 488 489 try { 490 mPhoneSubInfoControllerUT.getLine1AlphaTagForSubscriber(1, TAG); 491 Assert.fail("expected Security Exception Thrown"); 492 } catch (Exception ex) { 493 assertTrue(ex instanceof SecurityException); 494 assertEquals(READ_PHONE_STATE + " denied: getLine1AlphaTag", ex.getMessage()); 495 } 496 497 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 498 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 499 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 500 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 501 502 assertNull(mPhoneSubInfoControllerUT.getLine1AlphaTagForSubscriber(0, TAG)); 503 assertNull(mPhoneSubInfoControllerUT.getLine1AlphaTagForSubscriber(1, TAG)); 504 505 //case 3: no READ_PRIVILEGED_PHONE_STATE 506 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 507 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 508 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 509 assertEquals("LINE1_SIM_0", mPhoneSubInfoControllerUT 510 .getLine1AlphaTagForSubscriber(0, TAG)); 511 assertEquals("LINE1_SIM_1", mPhoneSubInfoControllerUT 512 .getLine1AlphaTagForSubscriber(1, TAG)); 513 } 514 515 @Test 516 @SmallTest 517 public void testMsisdn() { 518 doReturn("+18051234567").when(mPhone).getMsisdn(); 519 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getMsisdnForSubscriber(0, TAG)); 520 521 doReturn("+18052345678").when(mSecondPhone).getMsisdn(); 522 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getMsisdnForSubscriber(1, TAG)); 523 } 524 525 @Test 526 @SmallTest 527 public void testMsisdnWithOutPermission() { 528 doReturn("+18051234567").when(mPhone).getMsisdn(); 529 doReturn("+18052345678").when(mSecondPhone).getMsisdn(); 530 531 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 532 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 533 try { 534 mPhoneSubInfoControllerUT.getMsisdnForSubscriber(0, TAG); 535 Assert.fail("expected Security Exception Thrown"); 536 } catch (Exception ex) { 537 assertTrue(ex instanceof SecurityException); 538 assertEquals(READ_PHONE_STATE + " denied: getMsisdn", ex.getMessage()); 539 } 540 541 try { 542 mPhoneSubInfoControllerUT.getMsisdnForSubscriber(1, TAG); 543 Assert.fail("expected Security Exception Thrown"); 544 } catch (Exception ex) { 545 assertTrue(ex instanceof SecurityException); 546 assertEquals(READ_PHONE_STATE + " denied: getMsisdn", ex.getMessage()); 547 } 548 549 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 550 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 551 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 552 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 553 554 assertNull(mPhoneSubInfoControllerUT.getMsisdnForSubscriber(0, TAG)); 555 assertNull(mPhoneSubInfoControllerUT.getMsisdnForSubscriber(1, TAG)); 556 557 //case 3: no READ_PRIVILEGED_PHONE_STATE 558 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 559 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 560 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 561 assertEquals("+18051234567", mPhoneSubInfoControllerUT.getMsisdnForSubscriber(0, TAG)); 562 assertEquals("+18052345678", mPhoneSubInfoControllerUT.getMsisdnForSubscriber(1, TAG)); 563 } 564 565 @Test 566 @SmallTest 567 public void testGetVoiceMailNumber() { 568 doReturn("+18051234567").when(mPhone).getVoiceMailNumber(); 569 assertEquals("+18051234567", mPhoneSubInfoControllerUT 570 .getVoiceMailNumberForSubscriber(0, TAG)); 571 572 doReturn("+18052345678").when(mSecondPhone).getVoiceMailNumber(); 573 assertEquals("+18052345678", mPhoneSubInfoControllerUT 574 .getVoiceMailNumberForSubscriber(1, TAG)); 575 } 576 577 @Test 578 @SmallTest 579 public void testGetVoiceMailNumberWithOutPermission() { 580 doReturn("+18051234567").when(mPhone).getVoiceMailNumber(); 581 doReturn("+18052345678").when(mSecondPhone).getVoiceMailNumber(); 582 583 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 584 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 585 try { 586 mPhoneSubInfoControllerUT.getVoiceMailNumberForSubscriber(0, TAG); 587 Assert.fail("expected Security Exception Thrown"); 588 } catch (Exception ex) { 589 assertTrue(ex instanceof SecurityException); 590 assertEquals(READ_PHONE_STATE + " denied: getVoiceMailNumber", ex.getMessage()); 591 } 592 593 try { 594 mPhoneSubInfoControllerUT.getVoiceMailNumberForSubscriber(1, TAG); 595 Assert.fail("expected Security Exception Thrown"); 596 } catch (Exception ex) { 597 assertTrue(ex instanceof SecurityException); 598 assertEquals(READ_PHONE_STATE + " denied: getVoiceMailNumber", ex.getMessage()); 599 } 600 601 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 602 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 603 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 604 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 605 606 assertNull(mPhoneSubInfoControllerUT.getVoiceMailNumberForSubscriber(0, TAG)); 607 assertNull(mPhoneSubInfoControllerUT.getVoiceMailNumberForSubscriber(1, TAG)); 608 609 //case 3: no READ_PRIVILEGED_PHONE_STATE 610 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 611 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 612 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 613 assertEquals("+18051234567", mPhoneSubInfoControllerUT 614 .getVoiceMailNumberForSubscriber(0, TAG)); 615 assertEquals("+18052345678", mPhoneSubInfoControllerUT 616 .getVoiceMailNumberForSubscriber(1, TAG)); 617 } 618 619 @Test 620 @SmallTest 621 public void testGetVoiceMailAlphaTag() { 622 doReturn("VM_SIM_0").when(mPhone).getVoiceMailAlphaTag(); 623 assertEquals("VM_SIM_0", mPhoneSubInfoControllerUT 624 .getVoiceMailAlphaTagForSubscriber(0, TAG)); 625 626 doReturn("VM_SIM_1").when(mSecondPhone).getVoiceMailAlphaTag(); 627 assertEquals("VM_SIM_1", mPhoneSubInfoControllerUT 628 .getVoiceMailAlphaTagForSubscriber(1, TAG)); 629 } 630 631 @Test 632 @SmallTest 633 public void testGetVoiceMailAlphaTagWithOutPermission() { 634 doReturn("VM_SIM_0").when(mPhone).getVoiceMailAlphaTag(); 635 doReturn("VM_SIM_1").when(mSecondPhone).getVoiceMailAlphaTag(); 636 637 //case 1: no READ_PRIVILEGED_PHONE_STATE, READ_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 638 mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL); 639 try { 640 mPhoneSubInfoControllerUT.getVoiceMailAlphaTagForSubscriber(0, TAG); 641 Assert.fail("expected Security Exception Thrown"); 642 } catch (Exception ex) { 643 assertTrue(ex instanceof SecurityException); 644 assertEquals(READ_PHONE_STATE + " denied: getVoiceMailAlphaTag", ex.getMessage()); 645 } 646 647 try { 648 mPhoneSubInfoControllerUT.getVoiceMailAlphaTagForSubscriber(1, TAG); 649 Assert.fail("expected Security Exception Thrown"); 650 } catch (Exception ex) { 651 assertTrue(ex instanceof SecurityException); 652 assertEquals(READ_PHONE_STATE + " denied: getVoiceMailAlphaTag", ex.getMessage()); 653 } 654 655 //case 2: no READ_PRIVILEGED_PHONE_STATE & appOsMgr READ_PHONE_PERMISSION 656 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 657 doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp( 658 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 659 660 assertNull(mPhoneSubInfoControllerUT.getVoiceMailAlphaTagForSubscriber(0, TAG)); 661 assertNull(mPhoneSubInfoControllerUT.getVoiceMailAlphaTagForSubscriber(1, TAG)); 662 663 //case 3: no READ_PRIVILEGED_PHONE_STATE 664 mContextFixture.addCallingOrSelfPermission(READ_PHONE_STATE); 665 doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp( 666 eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), eq(TAG)); 667 assertEquals("VM_SIM_0", mPhoneSubInfoControllerUT 668 .getVoiceMailAlphaTagForSubscriber(0, TAG)); 669 assertEquals("VM_SIM_1", mPhoneSubInfoControllerUT 670 .getVoiceMailAlphaTagForSubscriber(1, TAG)); 671 } 672} 673