Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2019 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.telephony.euicc.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.os.IBinder;
     27 import android.service.euicc.DownloadSubscriptionResult;
     28 import android.service.euicc.EuiccService;
     29 import android.service.euicc.GetDefaultDownloadableSubscriptionListResult;
     30 import android.service.euicc.GetDownloadableSubscriptionMetadataResult;
     31 import android.service.euicc.GetEuiccProfileInfoListResult;
     32 import android.service.euicc.IDeleteSubscriptionCallback;
     33 import android.service.euicc.IDownloadSubscriptionCallback;
     34 import android.service.euicc.IEraseSubscriptionsCallback;
     35 import android.service.euicc.IEuiccService;
     36 import android.service.euicc.IGetDefaultDownloadableSubscriptionListCallback;
     37 import android.service.euicc.IGetDownloadableSubscriptionMetadataCallback;
     38 import android.service.euicc.IGetEidCallback;
     39 import android.service.euicc.IGetEuiccInfoCallback;
     40 import android.service.euicc.IGetEuiccProfileInfoListCallback;
     41 import android.service.euicc.IGetOtaStatusCallback;
     42 import android.service.euicc.IOtaStatusChangedCallback;
     43 import android.service.euicc.IRetainSubscriptionsForFactoryResetCallback;
     44 import android.service.euicc.ISwitchToSubscriptionCallback;
     45 import android.service.euicc.IUpdateSubscriptionNicknameCallback;
     46 import android.telephony.euicc.DownloadableSubscription;
     47 import android.telephony.euicc.EuiccInfo;
     48 import android.telephony.euicc.EuiccManager;
     49 import android.telephony.euicc.cts.MockEuiccService.IMockEuiccServiceCallback;
     50 
     51 import androidx.test.InstrumentationRegistry;
     52 import androidx.test.rule.ServiceTestRule;
     53 import androidx.test.runner.AndroidJUnit4;
     54 
     55 import org.junit.After;
     56 import org.junit.Before;
     57 import org.junit.Rule;
     58 import org.junit.Test;
     59 import org.junit.runner.RunWith;
     60 
     61 import java.util.concurrent.CountDownLatch;
     62 import java.util.concurrent.TimeUnit;
     63 
     64 @RunWith(AndroidJUnit4.class)
     65 public class EuiccServiceTest {
     66     private static final String ACTIVATION_CODE = "12345-ABCDE";
     67 
     68     private static final int CALLBACK_TIMEOUT_MILLIS = 2000 /* 2 sec */;
     69 
     70     private static final int MOCK_SLOT_ID = 1;
     71     private static final String MOCK_ICCID = "12345";
     72     private static final String MOCK_NICK_NAME = "nick name";
     73 
     74     private IEuiccService mEuiccServiceBinder;
     75     private IMockEuiccServiceCallback mCallback;
     76 
     77     private CountDownLatch mCountDownLatch;
     78 
     79     @Rule public ServiceTestRule mServiceTestRule = new ServiceTestRule();
     80 
     81     @Before
     82     public void setUp() throws Exception {
     83         mCallback = new MockEuiccServiceCallback();
     84         MockEuiccService.setCallback(mCallback);
     85 
     86         Intent mockServiceIntent = new Intent(getContext(), MockEuiccService.class);
     87         IBinder binder = mServiceTestRule.bindService(mockServiceIntent);
     88         mEuiccServiceBinder = IEuiccService.Stub.asInterface(binder);
     89     }
     90 
     91     @After
     92     public void tearDown() throws Exception {
     93         mServiceTestRule.unbindService();
     94         mCallback.reset();
     95     }
     96 
     97     static class MockEuiccServiceCallback implements IMockEuiccServiceCallback {
     98         private boolean mMethodCalled;
     99 
    100         @Override
    101         public void setMethodCalled() {
    102             mMethodCalled = true;
    103         }
    104 
    105         @Override
    106         public boolean isMethodCalled() {
    107             return mMethodCalled;
    108         }
    109 
    110         @Override
    111         public void reset() {
    112             mMethodCalled = false;
    113         }
    114     }
    115 
    116     private Context getContext() {
    117         return InstrumentationRegistry.getContext();
    118     }
    119 
    120     @Test
    121     public void testOnGetEid() throws Exception {
    122         mCountDownLatch = new CountDownLatch(1);
    123 
    124         mEuiccServiceBinder.getEid(
    125                 MOCK_SLOT_ID,
    126                 new IGetEidCallback.Stub() {
    127                     @Override
    128                     public void onSuccess(String eid) {
    129                         assertEquals(MockEuiccService.MOCK_EID, eid);
    130                         mCountDownLatch.countDown();
    131                     }
    132                 });
    133 
    134         try {
    135             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    136         } catch (InterruptedException e) {
    137             fail(e.toString());
    138         }
    139 
    140         assertTrue(mCallback.isMethodCalled());
    141     }
    142 
    143     @Test
    144     public void testOnGetOtaStatus() throws Exception {
    145         mCountDownLatch = new CountDownLatch(1);
    146 
    147         mEuiccServiceBinder.getOtaStatus(
    148                 MOCK_SLOT_ID,
    149                 new IGetOtaStatusCallback.Stub() {
    150                     @Override
    151                     public void onSuccess(int status) {
    152                         assertEquals(EuiccManager.EUICC_OTA_SUCCEEDED, status);
    153                         mCountDownLatch.countDown();
    154                     }
    155                 });
    156 
    157         try {
    158             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    159         } catch (InterruptedException e) {
    160             fail(e.toString());
    161         }
    162 
    163         assertTrue(mCallback.isMethodCalled());
    164     }
    165 
    166     @Test
    167     public void testOnGetDownloadableSubscriptionMetadata() throws Exception {
    168         DownloadableSubscription subscription =
    169                 DownloadableSubscription.forActivationCode(ACTIVATION_CODE);
    170 
    171         mCountDownLatch = new CountDownLatch(1);
    172 
    173         mEuiccServiceBinder.getDownloadableSubscriptionMetadata(
    174                 MOCK_SLOT_ID,
    175                 subscription,
    176                 true /*forceDeactivateSim*/,
    177                 new IGetDownloadableSubscriptionMetadataCallback.Stub() {
    178                     @Override
    179                     public void onComplete(GetDownloadableSubscriptionMetadataResult result) {
    180                         assertNotNull(result);
    181                         assertEquals(EuiccService.RESULT_OK, result.getResult());
    182                     }
    183                 });
    184 
    185         try {
    186             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    187         } catch (InterruptedException e) {
    188             fail(e.toString());
    189         }
    190 
    191         assertTrue(mCallback.isMethodCalled());
    192     }
    193 
    194     @Test
    195     public void testOnStartOtaIfNecessary() throws Exception {
    196         mCountDownLatch = new CountDownLatch(1);
    197 
    198         mEuiccServiceBinder.startOtaIfNecessary(
    199                 MOCK_SLOT_ID,
    200                 new IOtaStatusChangedCallback.Stub() {
    201                     @Override
    202                     public void onOtaStatusChanged(int status) {
    203                         assertEquals(EuiccManager.EUICC_OTA_SUCCEEDED, status);
    204                         mCountDownLatch.countDown();
    205                     }
    206                 });
    207 
    208         try {
    209             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    210         } catch (InterruptedException e) {
    211             fail(e.toString());
    212         }
    213 
    214         assertTrue(mCallback.isMethodCalled());
    215     }
    216 
    217     @Test
    218     public void testOnGetDefaultDownloadableSubscriptionList() throws Exception {
    219         mCountDownLatch = new CountDownLatch(1);
    220 
    221         mEuiccServiceBinder.getDefaultDownloadableSubscriptionList(
    222                 MOCK_SLOT_ID,
    223                 true /*forceDeactivateSim*/,
    224                 new IGetDefaultDownloadableSubscriptionListCallback.Stub() {
    225                     @Override
    226                     public void onComplete(GetDefaultDownloadableSubscriptionListResult result) {
    227                         assertNotNull(result);
    228                         assertEquals(EuiccService.RESULT_RESOLVABLE_ERRORS, result.getResult());
    229                         mCountDownLatch.countDown();
    230                     }
    231                 });
    232 
    233         try {
    234             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    235         } catch (InterruptedException e) {
    236             fail(e.toString());
    237         }
    238 
    239         assertTrue(mCallback.isMethodCalled());
    240     }
    241 
    242     @Test
    243     public void testOnDownloadSubscription() throws Exception {
    244         DownloadableSubscription subscription =
    245                 DownloadableSubscription.forActivationCode(ACTIVATION_CODE);
    246 
    247         mCountDownLatch = new CountDownLatch(1);
    248 
    249         mEuiccServiceBinder.downloadSubscription(
    250                 MOCK_SLOT_ID,
    251                 subscription,
    252                 true /*switchAfterDownload*/,
    253                 true /*forceDeactivateSim*/,
    254                 null /*resolvedBundle*/,
    255                 new IDownloadSubscriptionCallback.Stub() {
    256                     @Override
    257                     public void onComplete(DownloadSubscriptionResult result) {
    258                         assertNotNull(result);
    259                         assertEquals(EuiccService.RESULT_OK, result.getResult());
    260                         mCountDownLatch.countDown();
    261                     }
    262                 });
    263 
    264         try {
    265             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    266         } catch (InterruptedException e) {
    267             fail(e.toString());
    268         }
    269 
    270         assertTrue(mCallback.isMethodCalled());
    271     }
    272 
    273     @Test
    274     public void testOnGetEuiccProfileInfoList() throws Exception {
    275         mCountDownLatch = new CountDownLatch(1);
    276 
    277         mEuiccServiceBinder.getEuiccProfileInfoList(
    278                 MOCK_SLOT_ID,
    279                 new IGetEuiccProfileInfoListCallback.Stub() {
    280                     @Override
    281                     public void onComplete(GetEuiccProfileInfoListResult result) {
    282                         assertNotNull(result);
    283                         assertEquals(EuiccService.RESULT_RESOLVABLE_ERRORS, result.getResult());
    284                         mCountDownLatch.countDown();
    285                     }
    286                 });
    287 
    288         try {
    289             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    290         } catch (InterruptedException e) {
    291             fail(e.toString());
    292         }
    293 
    294         assertTrue(mCallback.isMethodCalled());
    295     }
    296 
    297     @Test
    298     public void testOnGetEuiccInfo() throws Exception {
    299         mCountDownLatch = new CountDownLatch(1);
    300 
    301         mEuiccServiceBinder.getEuiccInfo(
    302                 MOCK_SLOT_ID,
    303                 new IGetEuiccInfoCallback.Stub() {
    304                     @Override
    305                     public void onSuccess(EuiccInfo euiccInfo) {
    306                         assertNotNull(euiccInfo);
    307                         assertEquals(MockEuiccService.MOCK_OS_VERSION, euiccInfo.getOsVersion());
    308                         mCountDownLatch.countDown();
    309                     }
    310                 });
    311 
    312         try {
    313             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    314         } catch (InterruptedException e) {
    315             fail(e.toString());
    316         }
    317 
    318         assertTrue(mCallback.isMethodCalled());
    319     }
    320 
    321     @Test
    322     public void testOnDeleteSubscription() throws Exception {
    323         mCountDownLatch = new CountDownLatch(1);
    324 
    325         mEuiccServiceBinder.deleteSubscription(
    326                 MOCK_SLOT_ID,
    327                 MOCK_ICCID,
    328                 new IDeleteSubscriptionCallback.Stub() {
    329                     @Override
    330                     public void onComplete(int result) {
    331                         assertEquals(EuiccService.RESULT_OK, result);
    332                     }
    333                 });
    334 
    335         try {
    336             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    337         } catch (InterruptedException e) {
    338             fail(e.toString());
    339         }
    340 
    341         assertTrue(mCallback.isMethodCalled());
    342     }
    343 
    344     @Test
    345     public void testOnSwitchToSubscription() throws Exception {
    346         mCountDownLatch = new CountDownLatch(1);
    347 
    348         mEuiccServiceBinder.switchToSubscription(
    349                 MOCK_SLOT_ID,
    350                 MOCK_ICCID,
    351                 true /*forceDeactivateSim*/,
    352                 new ISwitchToSubscriptionCallback.Stub() {
    353                     @Override
    354                     public void onComplete(int result) {
    355                         assertEquals(EuiccService.RESULT_OK, result);
    356                     }
    357                 });
    358 
    359         try {
    360             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    361         } catch (InterruptedException e) {
    362             fail(e.toString());
    363         }
    364 
    365         assertTrue(mCallback.isMethodCalled());
    366     }
    367 
    368     @Test
    369     public void testOnUpdateSubscriptionNickname() throws Exception {
    370         mCountDownLatch = new CountDownLatch(1);
    371 
    372         mEuiccServiceBinder.updateSubscriptionNickname(
    373                 MOCK_SLOT_ID,
    374                 MOCK_ICCID,
    375                 MOCK_NICK_NAME,
    376                 new IUpdateSubscriptionNicknameCallback.Stub() {
    377                     @Override
    378                     public void onComplete(int result) {
    379                         assertEquals(EuiccService.RESULT_OK, result);
    380                     }
    381                 });
    382 
    383         try {
    384             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    385         } catch (InterruptedException e) {
    386             fail(e.toString());
    387         }
    388 
    389         assertTrue(mCallback.isMethodCalled());
    390     }
    391 
    392     @Test
    393     public void testOnEraseSubscriptions() throws Exception {
    394         mCountDownLatch = new CountDownLatch(1);
    395 
    396         mEuiccServiceBinder.eraseSubscriptions(
    397                 MOCK_SLOT_ID,
    398                 new IEraseSubscriptionsCallback.Stub() {
    399                     @Override
    400                     public void onComplete(int result) {
    401                         assertEquals(EuiccService.RESULT_OK, result);
    402                     }
    403                 });
    404 
    405         try {
    406             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    407         } catch (InterruptedException e) {
    408             fail(e.toString());
    409         }
    410 
    411         assertTrue(mCallback.isMethodCalled());
    412     }
    413 
    414     @Test
    415     public void testOnRetainSubscriptionsForFactoryReset() throws Exception {
    416         mCountDownLatch = new CountDownLatch(1);
    417 
    418         mEuiccServiceBinder.retainSubscriptionsForFactoryReset(
    419                 MOCK_SLOT_ID,
    420                 new IRetainSubscriptionsForFactoryResetCallback.Stub() {
    421                     @Override
    422                     public void onComplete(int result) {
    423                         assertEquals(EuiccService.RESULT_OK, result);
    424                     }
    425                 });
    426 
    427         try {
    428             mCountDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    429         } catch (InterruptedException e) {
    430             fail(e.toString());
    431         }
    432 
    433         assertTrue(mCallback.isMethodCalled());
    434     }
    435 }
    436