1 /* 2 * Copyright (C) 2017 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 package com.android.internal.telephony.euicc; 17 18 import static android.telephony.euicc.EuiccCardManager.RESET_OPTION_DELETE_OPERATIONAL_PROFILES; 19 import static android.telephony.euicc.EuiccManager.EUICC_OTA_STATUS_UNAVAILABLE; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertFalse; 23 import static org.junit.Assert.assertNotNull; 24 import static org.junit.Assert.assertNull; 25 import static org.junit.Assert.assertTrue; 26 import static org.mockito.ArgumentMatchers.any; 27 import static org.mockito.ArgumentMatchers.anyBoolean; 28 import static org.mockito.ArgumentMatchers.anyInt; 29 import static org.mockito.ArgumentMatchers.anyString; 30 import static org.mockito.ArgumentMatchers.eq; 31 import static org.mockito.Mockito.doAnswer; 32 import static org.mockito.Mockito.doNothing; 33 import static org.mockito.Mockito.doReturn; 34 import static org.mockito.Mockito.doThrow; 35 import static org.mockito.Mockito.never; 36 import static org.mockito.Mockito.verify; 37 import static org.mockito.Mockito.when; 38 39 import android.Manifest; 40 import android.annotation.Nullable; 41 import android.app.PendingIntent; 42 import android.content.Context; 43 import android.content.Intent; 44 import android.content.pm.PackageInfo; 45 import android.content.pm.PackageManager; 46 import android.content.pm.Signature; 47 import android.os.Parcelable; 48 import android.os.RemoteException; 49 import android.provider.Settings; 50 import android.service.euicc.DownloadSubscriptionResult; 51 import android.service.euicc.EuiccService; 52 import android.service.euicc.GetDefaultDownloadableSubscriptionListResult; 53 import android.service.euicc.GetDownloadableSubscriptionMetadataResult; 54 import android.telephony.SubscriptionInfo; 55 import android.telephony.SubscriptionManager; 56 import android.telephony.TelephonyManager; 57 import android.telephony.UiccAccessRule; 58 import android.telephony.UiccCardInfo; 59 import android.telephony.euicc.DownloadableSubscription; 60 import android.telephony.euicc.EuiccInfo; 61 import android.telephony.euicc.EuiccManager; 62 63 import androidx.test.runner.AndroidJUnit4; 64 65 import com.android.internal.telephony.TelephonyTest; 66 import com.android.internal.telephony.euicc.EuiccConnector.GetOtaStatusCommandCallback; 67 import com.android.internal.telephony.euicc.EuiccConnector.OtaStatusChangedCallback; 68 69 import org.junit.After; 70 import org.junit.Before; 71 import org.junit.Test; 72 import org.junit.runner.RunWith; 73 import org.mockito.Mock; 74 import org.mockito.Mockito; 75 import org.mockito.invocation.InvocationOnMock; 76 import org.mockito.stubbing.Answer; 77 import org.mockito.stubbing.Stubber; 78 79 import java.security.MessageDigest; 80 import java.security.NoSuchAlgorithmException; 81 import java.util.ArrayList; 82 import java.util.Arrays; 83 import java.util.Collections; 84 85 @RunWith(AndroidJUnit4.class) 86 public class EuiccControllerTest extends TelephonyTest { 87 private static final DownloadableSubscription SUBSCRIPTION = 88 DownloadableSubscription.forActivationCode("abcde"); 89 90 private static final String PACKAGE_NAME = "test.package"; 91 private static final String CARRIER_NAME = "test name"; 92 private static final byte[] SIGNATURE_BYTES = new byte[] {1, 2, 3, 4, 5}; 93 94 private static final UiccAccessRule ACCESS_RULE; 95 static { 96 try { 97 ACCESS_RULE = new UiccAccessRule( 98 MessageDigest.getInstance("SHA-256").digest(SIGNATURE_BYTES), 99 PACKAGE_NAME, 100 0); 101 } catch (NoSuchAlgorithmException e) { 102 throw new RuntimeException("SHA-256 must exist"); 103 } 104 } 105 106 private static final DownloadableSubscription SUBSCRIPTION_WITH_METADATA = 107 DownloadableSubscription.forActivationCode("abcde"); 108 static { 109 SUBSCRIPTION_WITH_METADATA.setCarrierName("test name"); 110 SUBSCRIPTION_WITH_METADATA.setAccessRules( Arrays.asList(new UiccAccessRule[] { ACCESS_RULE })111 Arrays.asList(new UiccAccessRule[] { ACCESS_RULE })); 112 } 113 114 private static final String OS_VERSION = "1.0"; 115 private static final EuiccInfo EUICC_INFO = new EuiccInfo(OS_VERSION); 116 117 private static final int SUBSCRIPTION_ID = 12345; 118 private static final String ICC_ID = "54321"; 119 private static final int CARD_ID = 25; 120 121 @Mock private EuiccConnector mMockConnector; 122 private TestEuiccController mController; 123 private int mSavedEuiccProvisionedValue; 124 125 private static class TestEuiccController extends EuiccController { 126 // Captured arguments to addResolutionIntent 127 private String mResolutionAction; 128 private EuiccOperation mOp; 129 130 // Captured arguments to sendResult. 131 private PendingIntent mCallbackIntent; 132 private int mResultCode; 133 private Intent mExtrasIntent; 134 135 // Whether refreshSubscriptionsAndSendResult was called. 136 private boolean mCalledRefreshSubscriptionsAndSendResult; 137 138 // Number of OTA status changed. 139 private int mNumOtaStatusChanged; 140 TestEuiccController(Context context, EuiccConnector connector)141 TestEuiccController(Context context, EuiccConnector connector) { 142 super(context, connector); 143 mNumOtaStatusChanged = 0; 144 } 145 146 @Override addResolutionIntent( Intent extrasIntent, String resolutionAction, String callingPackage, int resolvableErrors, boolean confirmationCodeRetried, EuiccOperation op, int cardId)147 public void addResolutionIntent( 148 Intent extrasIntent, String resolutionAction, String callingPackage, 149 int resolvableErrors, boolean confirmationCodeRetried, EuiccOperation op, 150 int cardId) { 151 mResolutionAction = resolutionAction; 152 mOp = op; 153 } 154 155 @Override sendResult(PendingIntent callbackIntent, int resultCode, Intent extrasIntent)156 public void sendResult(PendingIntent callbackIntent, int resultCode, Intent extrasIntent) { 157 assertNull("sendResult called twice unexpectedly", mCallbackIntent); 158 mCallbackIntent = callbackIntent; 159 mResultCode = resultCode; 160 mExtrasIntent = extrasIntent; 161 } 162 163 @Override refreshSubscriptionsAndSendResult( PendingIntent callbackIntent, int resultCode, Intent extrasIntent)164 public void refreshSubscriptionsAndSendResult( 165 PendingIntent callbackIntent, int resultCode, Intent extrasIntent) { 166 mCalledRefreshSubscriptionsAndSendResult = true; 167 sendResult(callbackIntent, resultCode, extrasIntent); 168 } 169 170 @Override sendOtaStatusChangedBroadcast()171 public void sendOtaStatusChangedBroadcast() { 172 ++mNumOtaStatusChanged; 173 } 174 } 175 176 @Before setUp()177 public void setUp() throws Exception { 178 super.setUp("EuiccControllerTest"); 179 mController = new TestEuiccController(mContext, mMockConnector); 180 181 PackageInfo pi = new PackageInfo(); 182 pi.packageName = PACKAGE_NAME; 183 pi.signatures = new Signature[] { new Signature(SIGNATURE_BYTES) }; 184 when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi); 185 186 mSavedEuiccProvisionedValue = Settings.Global.getInt(mContext.getContentResolver(), 187 Settings.Global.EUICC_PROVISIONED, 0); 188 Settings.Global.putInt(mContext.getContentResolver(), 189 Settings.Global.EUICC_PROVISIONED, 0); 190 } 191 192 @After tearDown()193 public void tearDown() throws Exception { 194 super.tearDown(); 195 Settings.Global.putInt(mContext.getContentResolver(), 196 Settings.Global.EUICC_PROVISIONED, mSavedEuiccProvisionedValue); 197 } 198 199 @Test(expected = SecurityException.class) testGetEid_noPrivileges()200 public void testGetEid_noPrivileges() throws Exception { 201 setGetEidPermissions(false /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */); 202 callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID); 203 } 204 205 @Test testGetEid_withPhoneStatePrivileged()206 public void testGetEid_withPhoneStatePrivileged() throws Exception { 207 setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */); 208 assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID)); 209 } 210 211 @Test testGetEid_withCarrierPrivileges()212 public void testGetEid_withCarrierPrivileges() throws Exception { 213 setGetEidPermissions(false /* hasPhoneStatePrivileged */, true /* hasCarrierPrivileges */); 214 assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID)); 215 } 216 217 @Test testGetEid_failure()218 public void testGetEid_failure() throws Exception { 219 setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */); 220 assertNull(callGetEid(false /* success */, null /* eid */, CARD_ID)); 221 } 222 223 @Test testGetEid_nullReturnValue()224 public void testGetEid_nullReturnValue() throws Exception { 225 setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */); 226 assertNull(callGetEid(true /* success */, null /* eid */, CARD_ID)); 227 } 228 229 @Test testGetEid_unsupportedCardId()230 public void testGetEid_unsupportedCardId() throws Exception { 231 setGetEidPermissions(false /* hasPhoneStatePrivileged */, true /* hasCarrierPrivileges */); 232 assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */, 233 TelephonyManager.UNSUPPORTED_CARD_ID)); 234 } 235 236 @Test(expected = SecurityException.class) testGetOtaStatus_noPrivileges()237 public void testGetOtaStatus_noPrivileges() { 238 setHasWriteEmbeddedPermission(false /* hasPermission */); 239 callGetOtaStatus(true /* success */, 1 /* status */); 240 } 241 242 @Test testGetOtaStatus_withWriteEmbeddedPermission()243 public void testGetOtaStatus_withWriteEmbeddedPermission() { 244 setHasWriteEmbeddedPermission(true /* hasPermission */); 245 assertEquals(1, callGetOtaStatus(true /* success */, 1 /* status */)); 246 } 247 248 @Test testGetOtaStatus_failure()249 public void testGetOtaStatus_failure() { 250 setHasWriteEmbeddedPermission(true /* hasPermission */); 251 assertEquals( 252 EUICC_OTA_STATUS_UNAVAILABLE, 253 callGetOtaStatus(false /* success */, 1 /* status */)); 254 } 255 256 @Test testStartOtaUpdatingIfNecessary_serviceNotAvailable()257 public void testStartOtaUpdatingIfNecessary_serviceNotAvailable() { 258 setHasWriteEmbeddedPermission(true /* hasPermission */); 259 callStartOtaUpdatingIfNecessary( 260 false /* serviceAvailable */, EuiccManager.EUICC_OTA_IN_PROGRESS); 261 assertEquals(mController.mNumOtaStatusChanged, 0); 262 } 263 264 @Test testStartOtaUpdatingIfNecessary_otaStatusChanged()265 public void testStartOtaUpdatingIfNecessary_otaStatusChanged() { 266 setHasWriteEmbeddedPermission(true /* hasPermission */); 267 callStartOtaUpdatingIfNecessary( 268 true /* serviceAvailable */, EuiccManager.EUICC_OTA_IN_PROGRESS); 269 callStartOtaUpdatingIfNecessary( 270 true /* serviceAvailable */, EuiccManager.EUICC_OTA_FAILED); 271 callStartOtaUpdatingIfNecessary( 272 true /* serviceAvailable */, EuiccManager.EUICC_OTA_SUCCEEDED); 273 callStartOtaUpdatingIfNecessary( 274 true /* serviceAvailable */, EuiccManager.EUICC_OTA_NOT_NEEDED); 275 callStartOtaUpdatingIfNecessary( 276 true /* serviceAvailable */, EuiccManager.EUICC_OTA_STATUS_UNAVAILABLE); 277 278 assertEquals(mController.mNumOtaStatusChanged, 5); 279 } 280 281 282 @Test testGetEuiccInfo_success()283 public void testGetEuiccInfo_success() { 284 assertEquals(OS_VERSION, callGetEuiccInfo(true /* success */, EUICC_INFO).getOsVersion()); 285 } 286 287 @Test testGetEuiccInfo_failure()288 public void testGetEuiccInfo_failure() { 289 assertNull(callGetEuiccInfo(false /* success */, null /* euiccInfo */)); 290 } 291 292 @Test testGetEuiccInfo_nullReturnValue()293 public void testGetEuiccInfo_nullReturnValue() { 294 assertNull(callGetEuiccInfo(true /* success */, null /* euiccInfo */)); 295 } 296 297 @Test testGetDownloadableSubscriptionMetadata_serviceUnavailable()298 public void testGetDownloadableSubscriptionMetadata_serviceUnavailable() throws Exception { 299 setHasWriteEmbeddedPermission(true); 300 callGetDownloadableSubscriptionMetadata( 301 SUBSCRIPTION, false /* complete */, null /* result */); 302 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 303 0 /* detailedCode */); 304 verify(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(), 305 any()); 306 } 307 308 @Test testGetDownloadableSubscriptionMetadata_error()309 public void testGetDownloadableSubscriptionMetadata_error() throws Exception { 310 setHasWriteEmbeddedPermission(true); 311 GetDownloadableSubscriptionMetadataResult result = 312 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */); 313 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 314 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 315 42 /* detailedCode */); 316 } 317 318 @Test testGetDownloadableSubscriptionMetadata_mustDeactivateSim()319 public void testGetDownloadableSubscriptionMetadata_mustDeactivateSim() 320 throws Exception { 321 setHasWriteEmbeddedPermission(true); 322 GetDownloadableSubscriptionMetadataResult result = 323 new GetDownloadableSubscriptionMetadataResult( 324 EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */); 325 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 326 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 327 0 /* detailedCode */); 328 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM, 329 EuiccOperation.ACTION_GET_METADATA_DEACTIVATE_SIM); 330 } 331 332 @Test testGetDownloadableSubscriptionMetadata_success()333 public void testGetDownloadableSubscriptionMetadata_success() throws Exception { 334 setHasWriteEmbeddedPermission(true); 335 GetDownloadableSubscriptionMetadataResult result = 336 new GetDownloadableSubscriptionMetadataResult( 337 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 338 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 339 Intent intent = verifyIntentSent( 340 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 341 DownloadableSubscription receivedSubscription = intent.getParcelableExtra( 342 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DOWNLOADABLE_SUBSCRIPTION); 343 assertNotNull(receivedSubscription); 344 assertEquals(CARRIER_NAME, receivedSubscription.getCarrierName()); 345 } 346 347 @Test testGetDefaultDownloadableSubscriptionList_serviceUnavailable()348 public void testGetDefaultDownloadableSubscriptionList_serviceUnavailable() throws Exception { 349 setHasWriteEmbeddedPermission(true); 350 callGetDefaultDownloadableSubscriptionList(false /* complete */, null /* result */); 351 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 352 0 /* detailedCode */); 353 } 354 355 @Test testGetDefaultDownloadableSubscriptionList_error()356 public void testGetDefaultDownloadableSubscriptionList_error() throws Exception { 357 setHasWriteEmbeddedPermission(true); 358 GetDefaultDownloadableSubscriptionListResult result = 359 new GetDefaultDownloadableSubscriptionListResult(42, null /* subscriptions */); 360 callGetDefaultDownloadableSubscriptionList(true /* complete */, result); 361 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 362 42 /* detailedCode */); 363 verify(mMockConnector).getDefaultDownloadableSubscriptionList(anyInt(), anyBoolean(), 364 any()); 365 } 366 367 @Test testGetDefaultDownloadableSubscriptionList_mustDeactivateSim()368 public void testGetDefaultDownloadableSubscriptionList_mustDeactivateSim() 369 throws Exception { 370 setHasWriteEmbeddedPermission(true); 371 GetDefaultDownloadableSubscriptionListResult result = 372 new GetDefaultDownloadableSubscriptionListResult( 373 EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscriptions */); 374 callGetDefaultDownloadableSubscriptionList(true /* complete */, result); 375 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 376 0 /* detailedCode */); 377 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM, 378 EuiccOperation.ACTION_GET_DEFAULT_LIST_DEACTIVATE_SIM); 379 } 380 381 @Test testGetDefaultDownloadableSubscriptionList_success()382 public void testGetDefaultDownloadableSubscriptionList_success() throws Exception { 383 setHasWriteEmbeddedPermission(true); 384 GetDefaultDownloadableSubscriptionListResult result = 385 new GetDefaultDownloadableSubscriptionListResult( 386 EuiccService.RESULT_OK, 387 new DownloadableSubscription[] { SUBSCRIPTION_WITH_METADATA }); 388 callGetDefaultDownloadableSubscriptionList(true /* complete */, result); 389 Intent intent = verifyIntentSent( 390 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 391 Parcelable[] receivedSubscriptions = intent.getParcelableArrayExtra( 392 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DOWNLOADABLE_SUBSCRIPTIONS); 393 assertNotNull(receivedSubscriptions); 394 assertEquals(1, receivedSubscriptions.length); 395 assertEquals(CARRIER_NAME, 396 ((DownloadableSubscription) receivedSubscriptions[0]).getCarrierName()); 397 } 398 399 @Test testDownloadSubscription_serviceUnavailable()400 public void testDownloadSubscription_serviceUnavailable() throws Exception { 401 setHasWriteEmbeddedPermission(true); 402 callDownloadSubscription( 403 SUBSCRIPTION, true /* switchAfterDownload */, false /* complete */, 404 0 /* result */, 0 /* resolvableError */, "whatever" /* callingPackage */); 405 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 406 0 /* detailedCode */); 407 verify(mMockConnector).downloadSubscription(anyInt(), 408 any(), anyBoolean(), anyBoolean(), any(), any()); 409 } 410 411 @Test testDownloadSubscription_error()412 public void testDownloadSubscription_error() throws Exception { 413 setHasWriteEmbeddedPermission(true); 414 callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */, 415 42, 0 /* resolvableError */, "whatever" /* callingPackage */); 416 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 417 42 /* detailedCode */); 418 } 419 420 @Test testDownloadSubscription_mustDeactivateSim()421 public void testDownloadSubscription_mustDeactivateSim() throws Exception { 422 setHasWriteEmbeddedPermission(true); 423 callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */, 424 EuiccService.RESULT_MUST_DEACTIVATE_SIM, 0 /* resolvableError */, 425 "whatever" /* callingPackage */); 426 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 427 0 /* detailedCode */); 428 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM, 429 EuiccOperation.ACTION_DOWNLOAD_DEACTIVATE_SIM); 430 } 431 432 @Test testDownloadSubscription_needConfirmationCode()433 public void testDownloadSubscription_needConfirmationCode() throws Exception { 434 setHasWriteEmbeddedPermission(true); 435 callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */, 436 EuiccService.RESULT_RESOLVABLE_ERRORS, 0b01 /* resolvableError */, 437 "whatever" /* callingPackage */); 438 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 439 0 /* detailedCode */); 440 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_RESOLVABLE_ERRORS, 441 EuiccOperation.ACTION_DOWNLOAD_RESOLVABLE_ERRORS); 442 } 443 444 @Test testDownloadSubscription_success()445 public void testDownloadSubscription_success() throws Exception { 446 setHasWriteEmbeddedPermission(true); 447 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 448 EuiccService.RESULT_OK, 0 /* resolvableError */, "whatever" /* callingPackage */); 449 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 450 // switchAfterDownload = true so no refresh should occur. 451 assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult); 452 } 453 454 @Test testDownloadSubscription_noSwitch_success()455 public void testDownloadSubscription_noSwitch_success() throws Exception { 456 setHasWriteEmbeddedPermission(true); 457 callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */, 458 EuiccService.RESULT_OK, 0 /* resolvableError */, "whatever" /* callingPackage */); 459 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 460 assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult); 461 } 462 463 @Test testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable()464 public void testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable() 465 throws Exception { 466 setHasWriteEmbeddedPermission(false); 467 prepareGetDownloadableSubscriptionMetadataCall(false /* complete */, null /* result */); 468 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 469 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 470 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 471 0 /* detailedCode */); 472 verify(mMockConnector, never()).downloadSubscription(anyInt(), 473 any(), anyBoolean(), anyBoolean(), any(), any()); 474 } 475 476 @Test testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable_canManageSim()477 public void testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable_canManageSim() 478 throws Exception { 479 setHasWriteEmbeddedPermission(false); 480 prepareGetDownloadableSubscriptionMetadataCall(false /* complete */, null /* result */); 481 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 482 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 483 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 484 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 485 0 /* detailedCode */); 486 verify(mMockConnector, never()).downloadSubscription(anyInt(), 487 any(), anyBoolean(), anyBoolean(), any(), any()); 488 } 489 490 @Test testDownloadSubscription_noPrivileges_getMetadata_error()491 public void testDownloadSubscription_noPrivileges_getMetadata_error() 492 throws Exception { 493 setHasWriteEmbeddedPermission(false); 494 GetDownloadableSubscriptionMetadataResult result = 495 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */); 496 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 497 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 498 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 499 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 500 0 /* detailedCode */); 501 verify(mMockConnector, never()).downloadSubscription(anyInt(), 502 any(), anyBoolean(), anyBoolean(), any(), any()); 503 } 504 505 @Test testDownloadSubscription_noPrivileges_getMetadata_error_canManageSim()506 public void testDownloadSubscription_noPrivileges_getMetadata_error_canManageSim() 507 throws Exception { 508 setHasWriteEmbeddedPermission(false); 509 GetDownloadableSubscriptionMetadataResult result = 510 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */); 511 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 512 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 513 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 514 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 515 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 516 42 /* detailedCode */); 517 verify(mMockConnector, never()).downloadSubscription(anyInt(), 518 any(), anyBoolean(), anyBoolean(), any(), any()); 519 } 520 521 @Test testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim()522 public void testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim() 523 throws Exception { 524 setHasWriteEmbeddedPermission(false); 525 GetDownloadableSubscriptionMetadataResult result = 526 new GetDownloadableSubscriptionMetadataResult( 527 EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */); 528 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 529 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 530 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 531 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 532 0 /* detailedCode */); 533 // In this case we go with the potentially stronger NO_PRIVILEGES consent dialog to avoid 534 // double prompting. 535 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 536 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA); 537 } 538 539 @Test testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim_canManageSim()540 public void testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim_canManageSim() 541 throws Exception { 542 setHasWriteEmbeddedPermission(false); 543 GetDownloadableSubscriptionMetadataResult result = 544 new GetDownloadableSubscriptionMetadataResult( 545 EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */); 546 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 547 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 548 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 549 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 550 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 551 0 /* detailedCode */); 552 // In this case we go with the potentially stronger NO_PRIVILEGES consent dialog to avoid 553 // double prompting. 554 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM, 555 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA); 556 } 557 558 @Test testDownloadSubscription_noPrivileges_hasCarrierPrivileges()559 public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges() throws Exception { 560 setHasWriteEmbeddedPermission(false); 561 GetDownloadableSubscriptionMetadataResult result = 562 new GetDownloadableSubscriptionMetadataResult( 563 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 564 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 565 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 566 setHasCarrierPrivilegesOnActiveSubscription(true); 567 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 568 EuiccService.RESULT_OK, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 569 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 570 // switchAfterDownload = true so no refresh should occur. 571 assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult); 572 } 573 574 @Test testDownloadSubscription_noPrivileges_hasCarrierPrivileges_multiSim()575 public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_multiSim() 576 throws Exception { 577 setHasWriteEmbeddedPermission(false); 578 GetDownloadableSubscriptionMetadataResult result = 579 new GetDownloadableSubscriptionMetadataResult( 580 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 581 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 582 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 583 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 584 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 585 EuiccService.RESULT_OK, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 586 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 587 // switchAfterDownload = true so no refresh should occur. 588 assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult); 589 } 590 591 @Test testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent()592 public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent() 593 throws Exception { 594 setHasWriteEmbeddedPermission(false); 595 GetDownloadableSubscriptionMetadataResult result = 596 new GetDownloadableSubscriptionMetadataResult( 597 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 598 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 599 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 600 setHasCarrierPrivilegesOnActiveSubscription(false); 601 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 602 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 603 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 604 0 /* detailedCode */); 605 verify(mMockConnector, never()).downloadSubscription(anyInt(), 606 any(), anyBoolean(), anyBoolean(), any(), any()); 607 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 608 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA); 609 } 610 611 @Test testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent_multiSim()612 public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent_multiSim() 613 throws Exception { 614 setHasWriteEmbeddedPermission(false); 615 GetDownloadableSubscriptionMetadataResult result = 616 new GetDownloadableSubscriptionMetadataResult( 617 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 618 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 619 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 620 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, false /* hasPrivileges */); 621 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 622 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 623 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 624 0 /* detailedCode */); 625 verify(mMockConnector, never()).downloadSubscription(anyInt(), 626 any(), anyBoolean(), anyBoolean(), any(), any()); 627 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 628 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA); 629 } 630 631 @Test testDownloadSubscription_noPriv_hasCarrierPrivi_needsConsent_multiSim_targetPsim()632 public void testDownloadSubscription_noPriv_hasCarrierPrivi_needsConsent_multiSim_targetPsim() 633 throws Exception { 634 setHasWriteEmbeddedPermission(false); 635 GetDownloadableSubscriptionMetadataResult result = 636 new GetDownloadableSubscriptionMetadataResult( 637 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 638 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 639 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 640 setCanManageSubscriptionOnTargetSim(false /* isTargetEuicc */, true /* hasPrivileges */); 641 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 642 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 643 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 644 0 /* detailedCode */); 645 verify(mMockConnector, never()).downloadSubscription(anyInt(), 646 any(), anyBoolean(), anyBoolean(), any(), any()); 647 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 648 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA); 649 } 650 651 @Test testDownloadSubscription_noPrivileges_noCarrierPrivileges()652 public void testDownloadSubscription_noPrivileges_noCarrierPrivileges() throws Exception { 653 setHasWriteEmbeddedPermission(false); 654 GetDownloadableSubscriptionMetadataResult result = 655 new GetDownloadableSubscriptionMetadataResult( 656 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 657 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 658 PackageInfo pi = new PackageInfo(); 659 pi.packageName = PACKAGE_NAME; 660 pi.signatures = new Signature[] { new Signature(new byte[] { 5, 4, 3, 2, 1 }) }; 661 when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi); 662 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 663 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 664 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 665 0 /* detailedCode */); 666 verify(mTelephonyManager, never()).checkCarrierPrivilegesForPackage(PACKAGE_NAME); 667 verify(mMockConnector, never()).downloadSubscription(anyInt(), 668 any(), anyBoolean(), anyBoolean(), any(), any()); 669 } 670 671 @Test testDownloadSubscription_noPrivileges_noCarrierPrivileges_canManagerTargetSim()672 public void testDownloadSubscription_noPrivileges_noCarrierPrivileges_canManagerTargetSim() 673 throws Exception { 674 setHasWriteEmbeddedPermission(false); 675 GetDownloadableSubscriptionMetadataResult result = 676 new GetDownloadableSubscriptionMetadataResult( 677 EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA); 678 prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result); 679 PackageInfo pi = new PackageInfo(); 680 pi.packageName = PACKAGE_NAME; 681 pi.signatures = new Signature[] { new Signature(new byte[] { 5, 4, 3, 2, 1 }) }; 682 when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi); 683 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 684 callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */, 685 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */); 686 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 687 0 /* detailedCode */); 688 verify(mTelephonyManager, never()).checkCarrierPrivilegesForPackage(PACKAGE_NAME); 689 verify(mMockConnector, never()).downloadSubscription(anyInt(), 690 any(), anyBoolean(), anyBoolean(), any(), any()); 691 } 692 693 @Test testDeleteSubscription_noSuchSubscription()694 public void testDeleteSubscription_noSuchSubscription() throws Exception { 695 setHasWriteEmbeddedPermission(true); 696 callDeleteSubscription( 697 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 698 0 /* result */, "whatever" /* callingPackage */); 699 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 700 0 /* detailedCode */); 701 verify(mMockConnector, never()).deleteSubscription(anyInt(), anyString(), any()); 702 } 703 704 @Test testDeleteSubscription_serviceUnavailable()705 public void testDeleteSubscription_serviceUnavailable() throws Exception { 706 setHasWriteEmbeddedPermission(true); 707 prepareOperationSubscription(false /* hasPrivileges */); 708 callDeleteSubscription( 709 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 710 0 /* result */, "whatever" /* callingPackage */); 711 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 712 0 /* detailedCode */); 713 } 714 715 @Test testDeleteSubscription_error()716 public void testDeleteSubscription_error() throws Exception { 717 setHasWriteEmbeddedPermission(true); 718 prepareOperationSubscription(false /* hasPrivileges */); 719 callDeleteSubscription( 720 SUBSCRIPTION_ID, ICC_ID, true /* complete */, 721 42 /* result */, "whatever" /* callingPackage */); 722 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 723 42 /* detailedCode */); 724 } 725 726 @Test testDeleteSubscription_success()727 public void testDeleteSubscription_success() throws Exception { 728 setHasWriteEmbeddedPermission(true); 729 prepareOperationSubscription(false /* hasPrivileges */); 730 callDeleteSubscription( 731 SUBSCRIPTION_ID, ICC_ID, true /* complete */, 732 EuiccService.RESULT_OK, "whatever" /* callingPackage */); 733 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 734 assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult); 735 } 736 737 @Test testDeleteSubscription_noPrivileges()738 public void testDeleteSubscription_noPrivileges() throws Exception { 739 setHasWriteEmbeddedPermission(false); 740 prepareOperationSubscription(false /* hasPrivileges */); 741 callDeleteSubscription( 742 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 743 0 /* result */, "whatever" /* callingPackage */); 744 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 745 0 /* detailedCode */); 746 verify(mMockConnector, never()).deleteSubscription(anyInt(), anyString(), any()); 747 } 748 749 @Test testDeleteSubscription_carrierPrivileges_success()750 public void testDeleteSubscription_carrierPrivileges_success() throws Exception { 751 setHasWriteEmbeddedPermission(false); 752 prepareOperationSubscription(true /* hasPrivileges */); 753 callDeleteSubscription( 754 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME); 755 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 756 assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult); 757 } 758 759 @Test testSwitchToSubscription_noSuchSubscription()760 public void testSwitchToSubscription_noSuchSubscription() throws Exception { 761 setHasWriteEmbeddedPermission(true); 762 callSwitchToSubscription( 763 12345, ICC_ID, false /* complete */, 0 /* result */, 764 "whatever" /* callingPackage */); 765 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 766 0 /* detailedCode */); 767 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 768 any()); 769 } 770 771 @Test testSwitchToSubscription_emptySubscription_noPrivileges()772 public void testSwitchToSubscription_emptySubscription_noPrivileges() throws Exception { 773 setHasWriteEmbeddedPermission(false); 774 callSwitchToSubscription( 775 SubscriptionManager.INVALID_SUBSCRIPTION_ID, null /* iccid */, false /* complete */, 776 0 /* result */, "whatever" /* callingPackage */); 777 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 778 0 /* detailedCode */); 779 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 780 any()); 781 } 782 783 @Test testSwitchToSubscription_serviceUnavailable()784 public void testSwitchToSubscription_serviceUnavailable() throws Exception { 785 setHasWriteEmbeddedPermission(true); 786 prepareOperationSubscription(false /* hasPrivileges */); 787 callSwitchToSubscription( 788 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, 789 "whatever" /* callingPackage */); 790 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 791 0 /* detailedCode */); 792 verify(mMockConnector).switchToSubscription(anyInt(), anyString(), anyBoolean(), any()); 793 } 794 795 @Test testSwitchToSubscription_error()796 public void testSwitchToSubscription_error() throws Exception { 797 setHasWriteEmbeddedPermission(true); 798 prepareOperationSubscription(false /* hasPrivileges */); 799 callSwitchToSubscription( 800 SUBSCRIPTION_ID, ICC_ID, true /* complete */, 42 /* result */, 801 "whatever" /* callingPackage */); 802 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 803 42 /* detailedCode */); 804 } 805 806 @Test testSwitchToSubscription_success()807 public void testSwitchToSubscription_success() throws Exception { 808 setHasWriteEmbeddedPermission(true); 809 prepareOperationSubscription(false /* hasPrivileges */); 810 callSwitchToSubscription( 811 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, 812 "whatever" /* callingPackage */); 813 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 814 } 815 816 @Test testSwitchToSubscription_emptySubscription_success()817 public void testSwitchToSubscription_emptySubscription_success() throws Exception { 818 setHasWriteEmbeddedPermission(true); 819 callSwitchToSubscription( 820 SubscriptionManager.INVALID_SUBSCRIPTION_ID, null /* iccid */, true /* complete */, 821 EuiccService.RESULT_OK, "whatever" /* callingPackage */); 822 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 823 } 824 825 @Test testSwitchToSubscription_noPrivileges()826 public void testSwitchToSubscription_noPrivileges() throws Exception { 827 setHasWriteEmbeddedPermission(false); 828 prepareOperationSubscription(false /* hasPrivileges */); 829 callSwitchToSubscription( 830 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, 831 "whatever" /* callingPackage */); 832 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 833 0 /* detailedCode */); 834 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 835 any()); 836 } 837 838 @Test testSwitchToSubscription_hasCarrierPrivileges()839 public void testSwitchToSubscription_hasCarrierPrivileges() throws Exception { 840 setHasWriteEmbeddedPermission(false); 841 prepareOperationSubscription(true /* hasPrivileges */); 842 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 843 setHasCarrierPrivilegesOnActiveSubscription(true); 844 callSwitchToSubscription( 845 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME); 846 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 847 } 848 849 @Test testSwitchToSubscription_hasCarrierPrivileges_multiSim()850 public void testSwitchToSubscription_hasCarrierPrivileges_multiSim() throws Exception { 851 setHasWriteEmbeddedPermission(false); 852 prepareOperationSubscription(true /* hasPrivileges */); 853 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 854 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */); 855 callSwitchToSubscription( 856 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME); 857 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 858 } 859 860 @Test testSwitchToSubscription_hasCarrierPrivileges_needsConsent()861 public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent() throws Exception { 862 setHasWriteEmbeddedPermission(false); 863 prepareOperationSubscription(true /* hasPrivileges */); 864 setHasCarrierPrivilegesOnActiveSubscription(false); 865 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 866 callSwitchToSubscription( 867 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME); 868 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 869 0 /* detailedCode */); 870 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 871 any()); 872 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 873 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES); 874 } 875 876 @Test testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim()877 public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim() 878 throws Exception { 879 setHasWriteEmbeddedPermission(false); 880 prepareOperationSubscription(true /* hasPrivileges */); 881 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 882 setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, false /* hasPrivileges */); 883 callSwitchToSubscription( 884 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME); 885 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 886 0 /* detailedCode */); 887 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 888 any()); 889 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 890 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES); 891 } 892 893 @Test testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim_targetPsim()894 public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim_targetPsim() 895 throws Exception { 896 setHasWriteEmbeddedPermission(false); 897 prepareOperationSubscription(true /* hasPrivileges */); 898 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 899 setCanManageSubscriptionOnTargetSim(false /* isTargetEuicc */, true /* hasPrivileges */); 900 callSwitchToSubscription( 901 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME); 902 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR, 903 0 /* detailedCode */); 904 verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(), 905 any()); 906 verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES, 907 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES); 908 } 909 910 @Test testUpdateSubscriptionNickname_noPrivileges()911 public void testUpdateSubscriptionNickname_noPrivileges() throws Exception { 912 setHasWriteEmbeddedPermission(false); 913 prepareOperationSubscription(false); 914 callUpdateSubscriptionNickname( 915 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */, 916 PACKAGE_NAME); 917 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 918 0 /* detailedCode */); 919 verify(mMockConnector, never()).updateSubscriptionNickname(anyInt(), anyString(), 920 anyString(), any()); 921 } 922 923 @Test testUpdateSubscriptionNickname_noSuchSubscription()924 public void testUpdateSubscriptionNickname_noSuchSubscription() throws Exception { 925 setHasWriteEmbeddedPermission(true); 926 callUpdateSubscriptionNickname( 927 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */, 928 PACKAGE_NAME); 929 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 930 0 /* detailedCode */); 931 verify(mMockConnector, never()).updateSubscriptionNickname(anyInt(), anyString(), 932 anyString(), any()); 933 } 934 935 @Test testUpdateSubscriptionNickname_serviceUnavailable()936 public void testUpdateSubscriptionNickname_serviceUnavailable() throws Exception { 937 setHasWriteEmbeddedPermission(true); 938 prepareOperationSubscription(false /* hasPrivileges */); 939 callUpdateSubscriptionNickname( 940 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */, 941 PACKAGE_NAME); 942 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 943 0 /* detailedCode */); 944 verify(mMockConnector).updateSubscriptionNickname(anyInt(), anyString(), anyString(), 945 any()); 946 } 947 948 @Test testUpdateSubscriptionNickname_error()949 public void testUpdateSubscriptionNickname_error() throws Exception { 950 setHasWriteEmbeddedPermission(true); 951 prepareOperationSubscription(false /* hasPrivileges */); 952 callUpdateSubscriptionNickname( 953 SUBSCRIPTION_ID, ICC_ID, "nickname", true /* complete */, 42 /* result */, 954 PACKAGE_NAME); 955 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 956 42 /* detailedCode */); 957 } 958 959 @Test testUpdateSubscriptionNickname_success()960 public void testUpdateSubscriptionNickname_success() throws Exception { 961 setHasWriteEmbeddedPermission(true); 962 prepareOperationSubscription(false /* hasPrivileges */); 963 callUpdateSubscriptionNickname( 964 SUBSCRIPTION_ID, ICC_ID, "nickname", true /* complete */, EuiccService.RESULT_OK, 965 PACKAGE_NAME); 966 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 967 } 968 969 @Test(expected = SecurityException.class) testEraseSubscriptions_noPrivileges()970 public void testEraseSubscriptions_noPrivileges() throws Exception { 971 setHasWriteEmbeddedPermission(false); 972 callEraseSubscriptions(false /* complete */, 0 /* result */); 973 } 974 975 @Test testEraseSubscriptions_serviceUnavailable()976 public void testEraseSubscriptions_serviceUnavailable() throws Exception { 977 setHasWriteEmbeddedPermission(true); 978 callEraseSubscriptions(false /* complete */, 0 /* result */); 979 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 980 0 /* detailedCode */); 981 verify(mMockConnector).eraseSubscriptions(anyInt(), any()); 982 } 983 984 @Test testEraseSubscriptions_error()985 public void testEraseSubscriptions_error() throws Exception { 986 setHasWriteEmbeddedPermission(true); 987 callEraseSubscriptions(true /* complete */, 42 /* result */); 988 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 42 /* detailedCode */); 989 } 990 991 @Test testEraseSubscriptions_success()992 public void testEraseSubscriptions_success() throws Exception { 993 setHasWriteEmbeddedPermission(true); 994 callEraseSubscriptions(true /* complete */, EuiccService.RESULT_OK); 995 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 996 assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult); 997 } 998 999 @Test(expected = SecurityException.class) testEraseSubscriptionsWithOptions_noPrivileges()1000 public void testEraseSubscriptionsWithOptions_noPrivileges() throws Exception { 1001 setHasWriteEmbeddedPermission(false); 1002 callEraseSubscriptionsWithOptions(false /* complete */, 0 /* result */); 1003 } 1004 1005 @Test testEraseSubscriptionsWithOptions_serviceUnavailable()1006 public void testEraseSubscriptionsWithOptions_serviceUnavailable() throws Exception { 1007 setHasWriteEmbeddedPermission(true); 1008 callEraseSubscriptionsWithOptions(false /* complete */, 0 /* result */); 1009 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 1010 0 /* detailedCode */); 1011 verify(mMockConnector).eraseSubscriptionsWithOptions(anyInt(), anyInt(), any()); 1012 } 1013 1014 @Test testEraseSubscriptionsWithOptions_error()1015 public void testEraseSubscriptionsWithOptions_error() throws Exception { 1016 setHasWriteEmbeddedPermission(true); 1017 callEraseSubscriptionsWithOptions(true /* complete */, 42 /* result */); 1018 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 42 /* detailedCode */); 1019 } 1020 1021 @Test testEraseSubscriptionsWithOptions_success()1022 public void testEraseSubscriptionsWithOptions_success() throws Exception { 1023 setHasWriteEmbeddedPermission(true); 1024 callEraseSubscriptionsWithOptions(true /* complete */, EuiccService.RESULT_OK); 1025 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 1026 assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult); 1027 } 1028 1029 @Test(expected = SecurityException.class) testRetainSubscriptionsForFactoryReset_noPrivileges()1030 public void testRetainSubscriptionsForFactoryReset_noPrivileges() throws Exception { 1031 setHasMasterClearPermission(false); 1032 callRetainSubscriptionsForFactoryReset(false /* complete */, 0 /* result */); 1033 } 1034 1035 @Test testRetainSubscriptionsForFactoryReset_serviceUnavailable()1036 public void testRetainSubscriptionsForFactoryReset_serviceUnavailable() throws Exception { 1037 setHasMasterClearPermission(true); 1038 callRetainSubscriptionsForFactoryReset(false /* complete */, 0 /* result */); 1039 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 0 /* detailedCode */); 1040 verify(mMockConnector).retainSubscriptions(anyInt(), any()); 1041 } 1042 1043 @Test testRetainSubscriptionsForFactoryReset_error()1044 public void testRetainSubscriptionsForFactoryReset_error() throws Exception { 1045 setHasMasterClearPermission(true); 1046 callRetainSubscriptionsForFactoryReset(true /* complete */, 42 /* result */); 1047 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 42 /* detailedCode */); 1048 } 1049 1050 @Test testRetainSubscriptionsForFactoryReset_success()1051 public void testRetainSubscriptionsForFactoryReset_success() throws Exception { 1052 setHasMasterClearPermission(true); 1053 callRetainSubscriptionsForFactoryReset(true /* complete */, EuiccService.RESULT_OK); 1054 verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */); 1055 } 1056 1057 @Test testAddExtrasToResultIntent_withSmdxOperationCode_normal_case()1058 public void testAddExtrasToResultIntent_withSmdxOperationCode_normal_case() { 1059 // Same setup as testGetDownloadableSubscriptionMetadata_error 1060 setHasWriteEmbeddedPermission(true); 1061 GetDownloadableSubscriptionMetadataResult result = 1062 new GetDownloadableSubscriptionMetadataResult(0xA8b1051 /* result */, 1063 null /* subscription */); 1064 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 1065 1066 assertEquals(mController.mExtrasIntent.getIntExtra( 1067 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1), 1068 EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE); 1069 assertEquals(mController.mExtrasIntent.getStringExtra( 1070 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "8.11.1"); 1071 assertEquals(mController.mExtrasIntent.getStringExtra( 1072 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "5.1"); 1073 1074 } 1075 1076 @Test testAddExtrasToResultIntent_withSmdxOperationCode_general_case()1077 public void testAddExtrasToResultIntent_withSmdxOperationCode_general_case() { 1078 // Same setup as testGetDownloadableSubscriptionMetadata_error 1079 setHasWriteEmbeddedPermission(true); 1080 GetDownloadableSubscriptionMetadataResult result = 1081 new GetDownloadableSubscriptionMetadataResult(0xA123456 /* result */, 1082 null /* subscription */); 1083 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 1084 1085 assertEquals(mController.mExtrasIntent.getIntExtra( 1086 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1), 1087 EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE); 1088 assertEquals(mController.mExtrasIntent.getStringExtra( 1089 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "1.2.3"); 1090 assertEquals(mController.mExtrasIntent.getStringExtra( 1091 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "4.5.6"); 1092 1093 } 1094 1095 @Test testAddExtrasToResultIntent_withSmdxOperationCode_and_padding()1096 public void testAddExtrasToResultIntent_withSmdxOperationCode_and_padding() { 1097 // Same setup as testGetDownloadableSubscriptionMetadata_error 1098 setHasWriteEmbeddedPermission(true); 1099 GetDownloadableSubscriptionMetadataResult result = 1100 new GetDownloadableSubscriptionMetadataResult(0xA003006 /* result */, 1101 null /* subscription */); 1102 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 1103 1104 assertEquals(mController.mExtrasIntent.getIntExtra( 1105 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1), 1106 EuiccManager.OPERATION_SMDX_SUBJECT_REASON_CODE); 1107 assertEquals(mController.mExtrasIntent.getStringExtra( 1108 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_SUBJECT_CODE), "3"); 1109 assertEquals(mController.mExtrasIntent.getStringExtra( 1110 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_SMDX_REASON_CODE), "6"); 1111 } 1112 1113 @Test testAddExtrasToResultIntent_withOperationCode()1114 public void testAddExtrasToResultIntent_withOperationCode() { 1115 // Same setup as testGetDownloadableSubscriptionMetadata_error 1116 setHasWriteEmbeddedPermission(true); 1117 GetDownloadableSubscriptionMetadataResult result = 1118 new GetDownloadableSubscriptionMetadataResult(0x12345678 /* result */, 1119 null /* subscription */); 1120 callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result); 1121 1122 assertEquals(mController.mExtrasIntent.getIntExtra( 1123 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_OPERATION_CODE, -1), 1124 0x12); 1125 assertEquals(mController.mExtrasIntent.getIntExtra( 1126 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_ERROR_CODE, -1), 0x345678); 1127 } 1128 setGetEidPermissions( boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges)1129 private void setGetEidPermissions( 1130 boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges) throws Exception { 1131 doReturn(hasPhoneStatePrivileged 1132 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED) 1133 .when(mContext) 1134 .checkCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE); 1135 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 1136 setHasCarrierPrivilegesOnActiveSubscription(hasCarrierPrivileges); 1137 } 1138 setHasWriteEmbeddedPermission(boolean hasPermission)1139 private void setHasWriteEmbeddedPermission(boolean hasPermission) { 1140 doReturn(hasPermission 1141 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED) 1142 .when(mContext) 1143 .checkCallingOrSelfPermission(Manifest.permission.WRITE_EMBEDDED_SUBSCRIPTIONS); 1144 } 1145 setHasMasterClearPermission(boolean hasPermission)1146 private void setHasMasterClearPermission(boolean hasPermission) { 1147 Stubber stubber = hasPermission ? doNothing() : doThrow(new SecurityException()); 1148 stubber.when(mContext).enforceCallingPermission( 1149 eq(Manifest.permission.MASTER_CLEAR), anyString()); 1150 } 1151 setHasCarrierPrivilegesOnActiveSubscription(boolean hasPrivileges)1152 private void setHasCarrierPrivilegesOnActiveSubscription(boolean hasPrivileges) 1153 throws Exception { 1154 SubscriptionInfo subInfo = new SubscriptionInfo( 1155 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */, 1156 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID, 1157 false, null, false, 0, 0, 0, null, null, true); 1158 when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn( 1159 hasPrivileges); 1160 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn( 1161 Collections.singletonList(subInfo)); 1162 } 1163 setCanManageSubscriptionOnTargetSim(boolean isTargetEuicc, boolean hasPrivileges)1164 private void setCanManageSubscriptionOnTargetSim(boolean isTargetEuicc, boolean hasPrivileges) 1165 throws Exception { 1166 UiccCardInfo cardInfo1 = new UiccCardInfo(isTargetEuicc, CARD_ID, "", "", 0, 1167 false /* isRemovable */); 1168 UiccCardInfo cardInfo2 = new UiccCardInfo(true /* isEuicc */, 1 /* cardId */, 1169 "", "", 0, false /* isRemovable */); 1170 ArrayList<UiccCardInfo> cardInfos = new ArrayList<>(); 1171 cardInfos.add(cardInfo1); 1172 cardInfos.add(cardInfo2); 1173 when(mTelephonyManager.getUiccCardsInfo()).thenReturn(cardInfos); 1174 1175 SubscriptionInfo subInfo1 = new SubscriptionInfo( 1176 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */, 1177 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID, 1178 false, null, false, 0, 0, 0, null, null, true); 1179 SubscriptionInfo subInfo2 = new SubscriptionInfo( 1180 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */, 1181 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", 1182 1 /* cardId */, false, null, false, 0, 0, 0, null, null, true); 1183 when(mSubscriptionManager.canManageSubscription(subInfo1, PACKAGE_NAME)).thenReturn( 1184 hasPrivileges); 1185 when(mSubscriptionManager.canManageSubscription(subInfo2, PACKAGE_NAME)).thenReturn( 1186 hasPrivileges); 1187 ArrayList<SubscriptionInfo> subInfos = new ArrayList<>(Arrays.asList(subInfo1, subInfo2)); 1188 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(subInfos); 1189 } 1190 prepareOperationSubscription(boolean hasPrivileges)1191 private void prepareOperationSubscription(boolean hasPrivileges) throws Exception { 1192 SubscriptionInfo subInfo = new SubscriptionInfo( 1193 SUBSCRIPTION_ID, ICC_ID, 0, "", "", 0, 0, "", 0, null, "0", "0", "", 1194 true /* isEmbedded */, hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, 1195 null); 1196 when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn( 1197 hasPrivileges); 1198 when(mSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn( 1199 Collections.singletonList(subInfo)); 1200 } 1201 callGetEid(final boolean success, final @Nullable String eid, int cardId)1202 private String callGetEid(final boolean success, final @Nullable String eid, int cardId) { 1203 doAnswer(new Answer<Void>() { 1204 @Override 1205 public Void answer(InvocationOnMock invocation) throws Exception { 1206 EuiccConnector.GetEidCommandCallback cb = invocation 1207 .getArgument(1 /* resultCallback */); 1208 if (success) { 1209 cb.onGetEidComplete(eid); 1210 } else { 1211 cb.onEuiccServiceUnavailable(); 1212 } 1213 return null; 1214 } 1215 }).when(mMockConnector).getEid(anyInt(), 1216 Mockito.<EuiccConnector.GetEidCommandCallback>any()); 1217 return mController.getEid(cardId, PACKAGE_NAME); 1218 } 1219 callGetOtaStatus(final boolean success, final int status)1220 private int callGetOtaStatus(final boolean success, final int status) { 1221 doAnswer(new Answer<Void>() { 1222 @Override 1223 public Void answer(InvocationOnMock invocation) throws Exception { 1224 GetOtaStatusCommandCallback cb = invocation.getArgument(1 /* resultCallback */); 1225 if (success) { 1226 cb.onGetOtaStatusComplete(status); 1227 } else { 1228 cb.onEuiccServiceUnavailable(); 1229 } 1230 return null; 1231 } 1232 }).when(mMockConnector).getOtaStatus(anyInt(), Mockito.<GetOtaStatusCommandCallback>any()); 1233 return mController.getOtaStatus(CARD_ID); 1234 } 1235 callStartOtaUpdatingIfNecessary( final boolean serviceAvailable, int status)1236 private void callStartOtaUpdatingIfNecessary( 1237 final boolean serviceAvailable, int status) { 1238 doAnswer(new Answer<Void>() { 1239 @Override 1240 public Void answer(InvocationOnMock invocation) throws Exception { 1241 OtaStatusChangedCallback cb = invocation.getArgument(1 /* resultCallback */); 1242 if (!serviceAvailable) { 1243 cb.onEuiccServiceUnavailable(); 1244 } else { 1245 cb.onOtaStatusChanged(status); 1246 } 1247 return null; 1248 } 1249 }).when(mMockConnector).startOtaIfNecessary(anyInt(), 1250 Mockito.<OtaStatusChangedCallback>any()); 1251 1252 mController.startOtaUpdatingIfNecessary(); 1253 } 1254 callGetEuiccInfo(final boolean success, final @Nullable EuiccInfo euiccInfo)1255 private EuiccInfo callGetEuiccInfo(final boolean success, final @Nullable EuiccInfo euiccInfo) { 1256 doAnswer(new Answer<Void>() { 1257 @Override 1258 public Void answer(InvocationOnMock invocation) throws Exception { 1259 EuiccConnector.GetEuiccInfoCommandCallback cb = invocation 1260 .getArgument(1 /* resultCallback */); 1261 if (success) { 1262 cb.onGetEuiccInfoComplete(euiccInfo); 1263 } else { 1264 cb.onEuiccServiceUnavailable(); 1265 } 1266 return null; 1267 } 1268 }).when(mMockConnector).getEuiccInfo(anyInt(), any()); 1269 return mController.getEuiccInfo(CARD_ID); 1270 } 1271 prepareGetDownloadableSubscriptionMetadataCall( final boolean complete, final GetDownloadableSubscriptionMetadataResult result)1272 private void prepareGetDownloadableSubscriptionMetadataCall( 1273 final boolean complete, final GetDownloadableSubscriptionMetadataResult result) { 1274 doAnswer(new Answer<Void>() { 1275 @Override 1276 public Void answer(InvocationOnMock invocation) throws Exception { 1277 EuiccConnector.GetMetadataCommandCallback cb = invocation 1278 .getArgument(3 /* resultCallback */); 1279 if (complete) { 1280 cb.onGetMetadataComplete(CARD_ID, result); 1281 } else { 1282 cb.onEuiccServiceUnavailable(); 1283 } 1284 return null; 1285 } 1286 }).when(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(), 1287 any()); 1288 } 1289 callGetDownloadableSubscriptionMetadata(DownloadableSubscription subscription, boolean complete, GetDownloadableSubscriptionMetadataResult result)1290 private void callGetDownloadableSubscriptionMetadata(DownloadableSubscription subscription, 1291 boolean complete, GetDownloadableSubscriptionMetadataResult result) { 1292 prepareGetDownloadableSubscriptionMetadataCall(complete, result); 1293 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1294 mController.getDownloadableSubscriptionMetadata(0, subscription, PACKAGE_NAME, 1295 resultCallback); 1296 } 1297 callGetDefaultDownloadableSubscriptionList( boolean complete, GetDefaultDownloadableSubscriptionListResult result)1298 private void callGetDefaultDownloadableSubscriptionList( 1299 boolean complete, GetDefaultDownloadableSubscriptionListResult result) { 1300 doAnswer(new Answer<Void>() { 1301 @Override 1302 public Void answer(InvocationOnMock invocation) throws Exception { 1303 EuiccConnector.GetDefaultListCommandCallback cb = invocation 1304 .getArgument(2 /* resultCallBack */); 1305 if (complete) { 1306 cb.onGetDefaultListComplete(CARD_ID, result); 1307 } else { 1308 cb.onEuiccServiceUnavailable(); 1309 } 1310 return null; 1311 } 1312 }).when(mMockConnector).getDefaultDownloadableSubscriptionList(anyInt(), anyBoolean(), 1313 any()); 1314 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1315 mController.getDefaultDownloadableSubscriptionList(CARD_ID, PACKAGE_NAME, resultCallback); 1316 } 1317 callDownloadSubscription(DownloadableSubscription subscription, boolean switchAfterDownload, final boolean complete, final int result, final int resolvableError, String callingPackage)1318 private void callDownloadSubscription(DownloadableSubscription subscription, 1319 boolean switchAfterDownload, final boolean complete, final int result, 1320 final int resolvableError, String callingPackage) { 1321 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1322 doAnswer(new Answer<Void>() { 1323 @Override 1324 public Void answer(InvocationOnMock invocation) throws Exception { 1325 EuiccConnector.DownloadCommandCallback cb = invocation 1326 .getArgument(5 /* resultCallback */); 1327 if (complete) { 1328 DownloadSubscriptionResult downloadRes = new DownloadSubscriptionResult( 1329 result, resolvableError, -1 /* cardId */); 1330 cb.onDownloadComplete(downloadRes); 1331 } else { 1332 cb.onEuiccServiceUnavailable(); 1333 } 1334 return null; 1335 } 1336 }).when(mMockConnector).downloadSubscription(anyInt(), 1337 any(), eq(switchAfterDownload), anyBoolean(), any(), any()); 1338 mController.downloadSubscription(CARD_ID, subscription, switchAfterDownload, callingPackage, 1339 null /* resolvedBundle */, resultCallback); 1340 // EUICC_PROVISIONED setting should match whether the download was successful. 1341 assertEquals(complete && result == EuiccService.RESULT_OK ? 1 : 0, 1342 Settings.Global.getInt(mContext.getContentResolver(), 1343 Settings.Global.EUICC_PROVISIONED, 0)); 1344 } 1345 callDeleteSubscription(int subscriptionId, String iccid, final boolean complete, final int result, String callingPackage)1346 private void callDeleteSubscription(int subscriptionId, String iccid, final boolean complete, 1347 final int result, String callingPackage) { 1348 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1349 doAnswer(new Answer<Void>() { 1350 @Override 1351 public Void answer(InvocationOnMock invocation) throws Exception { 1352 EuiccConnector.DeleteCommandCallback cb = invocation 1353 .getArgument(2 /* resultCallback */); 1354 if (complete) { 1355 cb.onDeleteComplete(result); 1356 } else { 1357 cb.onEuiccServiceUnavailable(); 1358 } 1359 return null; 1360 } 1361 }).when(mMockConnector).deleteSubscription(anyInt(), eq(iccid), any()); 1362 mController.deleteSubscription(CARD_ID, subscriptionId, callingPackage, resultCallback); 1363 } 1364 callSwitchToSubscription(int subscriptionId, String iccid, final boolean complete, final int result, String callingPackage)1365 private void callSwitchToSubscription(int subscriptionId, String iccid, final boolean complete, 1366 final int result, String callingPackage) { 1367 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1368 doAnswer(new Answer<Void>() { 1369 @Override 1370 public Void answer(InvocationOnMock invocation) throws Exception { 1371 EuiccConnector.SwitchCommandCallback cb = invocation 1372 .getArgument(3 /* resultCallback */); 1373 if (complete) { 1374 cb.onSwitchComplete(result); 1375 } else { 1376 cb.onEuiccServiceUnavailable(); 1377 } 1378 return null; 1379 } 1380 }).when(mMockConnector).switchToSubscription(anyInt(), eq(iccid), anyBoolean(), any()); 1381 mController.switchToSubscription(CARD_ID, subscriptionId, callingPackage, resultCallback); 1382 } 1383 callUpdateSubscriptionNickname(int subscriptionId, String iccid, String nickname, final boolean complete, final int result, String callingPackage)1384 private void callUpdateSubscriptionNickname(int subscriptionId, String iccid, String nickname, 1385 final boolean complete, final int result, String callingPackage) { 1386 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1387 doAnswer(new Answer<Void>() { 1388 @Override 1389 public Void answer(InvocationOnMock invocation) throws Exception { 1390 EuiccConnector.UpdateNicknameCommandCallback cb = invocation 1391 .getArgument(3 /* resultCallback */); 1392 if (complete) { 1393 cb.onUpdateNicknameComplete(result); 1394 } else { 1395 cb.onEuiccServiceUnavailable(); 1396 } 1397 return null; 1398 } 1399 }).when(mMockConnector).updateSubscriptionNickname(anyInt(), eq(iccid), eq(nickname), 1400 any()); 1401 mController.updateSubscriptionNickname(CARD_ID, subscriptionId, nickname, callingPackage, 1402 resultCallback); 1403 } 1404 callEraseSubscriptions(final boolean complete, final int result)1405 private void callEraseSubscriptions(final boolean complete, final int result) { 1406 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1407 doAnswer(new Answer<Void>() { 1408 @Override 1409 public Void answer(InvocationOnMock invocation) throws Exception { 1410 EuiccConnector.EraseCommandCallback cb = invocation 1411 .getArgument(1 /* resultCallback */); 1412 if (complete) { 1413 cb.onEraseComplete(result); 1414 } else { 1415 cb.onEuiccServiceUnavailable(); 1416 } 1417 return null; 1418 } 1419 }).when(mMockConnector).eraseSubscriptions(anyInt(), any()); 1420 mController.eraseSubscriptions(CARD_ID, resultCallback); 1421 } 1422 callEraseSubscriptionsWithOptions(final boolean complete, final int result)1423 private void callEraseSubscriptionsWithOptions(final boolean complete, final int result) { 1424 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1425 doAnswer(new Answer<Void>() { 1426 @Override 1427 public Void answer(InvocationOnMock invocation) throws Exception { 1428 EuiccConnector.EraseCommandCallback cb = invocation 1429 .getArgument(2 /* resultCallback */); 1430 if (complete) { 1431 cb.onEraseComplete(result); 1432 } else { 1433 cb.onEuiccServiceUnavailable(); 1434 } 1435 return null; 1436 } 1437 }).when(mMockConnector).eraseSubscriptionsWithOptions(anyInt(), anyInt(), any()); 1438 mController.eraseSubscriptionsWithOptions(CARD_ID, 1439 RESET_OPTION_DELETE_OPERATIONAL_PROFILES, resultCallback); 1440 } 1441 callRetainSubscriptionsForFactoryReset(final boolean complete, final int result)1442 private void callRetainSubscriptionsForFactoryReset(final boolean complete, final int result) { 1443 PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0); 1444 doAnswer(new Answer<Void>() { 1445 @Override 1446 public Void answer(InvocationOnMock invocation) throws Exception { 1447 EuiccConnector.RetainSubscriptionsCommandCallback cb = invocation 1448 .getArgument(1 /* resultCallback */); 1449 if (complete) { 1450 cb.onRetainSubscriptionsComplete(result); 1451 } else { 1452 cb.onEuiccServiceUnavailable(); 1453 } 1454 return null; 1455 } 1456 }).when(mMockConnector).retainSubscriptions(anyInt(), any()); 1457 mController.retainSubscriptionsForFactoryReset(CARD_ID, resultCallback); 1458 } 1459 verifyResolutionIntent(String euiccUiAction, @EuiccOperation.Action int action)1460 private void verifyResolutionIntent(String euiccUiAction, @EuiccOperation.Action int action) { 1461 assertEquals(euiccUiAction, mController.mResolutionAction); 1462 assertNotNull(mController.mOp); 1463 assertEquals(action, mController.mOp.mAction); 1464 } 1465 verifyIntentSent(int resultCode, int detailedCode)1466 private Intent verifyIntentSent(int resultCode, int detailedCode) 1467 throws RemoteException { 1468 assertNotNull(mController.mCallbackIntent); 1469 assertEquals(resultCode, mController.mResultCode); 1470 if (mController.mExtrasIntent == null) { 1471 assertEquals(0, detailedCode); 1472 } else { 1473 assertEquals(detailedCode, 1474 mController.mExtrasIntent.getIntExtra( 1475 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0)); 1476 } 1477 return mController.mExtrasIntent; 1478 } 1479 } 1480