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