Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2014 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.telephony4.cts;
     18 
     19 import static androidx.test.InstrumentationRegistry.getContext;
     20 
     21 import static org.junit.Assert.fail;
     22 
     23 import android.content.Context;
     24 import android.telephony.SmsManager;
     25 import android.telephony.TelephonyManager;
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 
     29 public class SimRestrictedApisTest {
     30     private static final byte[] TEST_PDU = { 0, 0 };
     31     private TelephonyManager mTelephonyManager;
     32 
     33     @Before
     34     public void setUp() throws Exception {
     35         mTelephonyManager =
     36                 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
     37     }
     38 
     39     private boolean isSimCardPresent() {
     40         return mTelephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE &&
     41                 mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT;
     42     }
     43 
     44     /**
     45      * Tests the SmsManager.injectSmsPdu() API. This makes a call to injectSmsPdu() API and expects
     46      * a SecurityException since the test apk is not signed by a certificate on the SIM.
     47      */
     48     @Test
     49     public void testInjectSmsPdu() {
     50         try {
     51             if (isSimCardPresent()) {
     52                 SmsManager.getDefault().injectSmsPdu(TEST_PDU, "3gpp", null);
     53                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     54             }
     55         } catch (SecurityException expected) {
     56         }
     57     }
     58 
     59     /**
     60      * Tests the TelephonyManager.setLine1NumberForDisplay() API. This makes a call to
     61      * setLine1NumberForDisplay() API and expects a SecurityException since the test apk is not
     62      * signed by a certificate on the SIM.
     63      */
     64     @Test
     65     public void testSetLine1NumberForDisplay() {
     66         try {
     67             if (isSimCardPresent()) {
     68                 mTelephonyManager.setLine1NumberForDisplay("", "");
     69                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     70             }
     71         } catch (SecurityException expected) {
     72         }
     73     }
     74 
     75     /**
     76      * Tests the TelephonyManager.iccOpenLogicalChannel() API. This makes a call to
     77      * iccOpenLogicalChannel() API and expects a SecurityException since the test apk is not signed
     78      * by certificate on the SIM.
     79      */
     80     @Test
     81     public void testIccOpenLogicalChannel() {
     82         try {
     83             if (isSimCardPresent()) {
     84                 mTelephonyManager.iccCloseLogicalChannel(
     85                         mTelephonyManager.iccOpenLogicalChannel("").getChannel());
     86                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     87             }
     88         } catch (SecurityException expected) {
     89         }
     90     }
     91 
     92     /**
     93      * Tests the TelephonyManager.iccOpenLogicalChannelBySlot() API. This makes a call to
     94      * iccOpenLogicalChannelBySlot() API and expects a SecurityException since the test apk is not
     95      * signed by certificate on the SIM.
     96      */
     97     @Test
     98     public void testIccOpenLogicalChannelBySlot() {
     99         try {
    100             if (isSimCardPresent()) {
    101                 mTelephonyManager.iccCloseLogicalChannelBySlot(0,
    102                         mTelephonyManager.iccOpenLogicalChannel("").getChannel());
    103                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    104             }
    105         } catch (SecurityException expected) {
    106         }
    107     }
    108 
    109     /**
    110      * Tests the TelephonyManager.iccCloseLogicalChannel() API. This makes a call to
    111      * iccCloseLogicalChannel() API and expects a SecurityException since the test apk is not signed
    112      * by certificate on the SIM.
    113      */
    114     @Test
    115     public void testIccCloseLogicalChannel() {
    116         try {
    117             if (isSimCardPresent()) {
    118                 mTelephonyManager.iccCloseLogicalChannel(0);
    119                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    120             }
    121         } catch (SecurityException expected) {
    122         }
    123     }
    124 
    125     /**
    126      * Tests the TelephonyManager.iccCloseLogicalChannelBySlot() API. This makes a call to
    127      * iccCloseLogicalChannelBySlot() API and expects a SecurityException since the test apk is not
    128      * signed by certificate on the SIM.
    129      */
    130     @Test
    131     public void testIccCloseLogicalChannelBySlot() {
    132         try {
    133             if (isSimCardPresent()) {
    134                 mTelephonyManager.iccCloseLogicalChannelBySlot(0, 0);
    135                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    136             }
    137         } catch (SecurityException expected) {
    138         }
    139     }
    140 
    141     /**
    142      * Tests the TelephonyManager.iccTransmitApduBasicChannel() API. This makes a call to
    143      * iccTransmitApduBasicChannel() API and expects a SecurityException since the test apk is not
    144      * signed by a certificate on the SIM.
    145      */
    146     @Test
    147     public void testIccTransmitApduBasicChannel() {
    148         try {
    149             if (isSimCardPresent()) {
    150                 mTelephonyManager.iccTransmitApduBasicChannel(0, 0, 0, 0, 0, "");
    151                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    152             }
    153         } catch (SecurityException expected) {
    154         }
    155     }
    156 
    157     /**
    158      * Tests the TelephonyManager.iccTransmitApduBasicChannelBySlot() API. This makes a call to
    159      * iccTransmitApduBasicChannelBySlot() API and expects a SecurityException since the test apk is
    160      * not signed by a certificate on the SIM.
    161      */
    162     @Test
    163     public void testIccTransmitApduBasicChannelBySlot() {
    164         try {
    165             if (isSimCardPresent()) {
    166                 mTelephonyManager.iccTransmitApduBasicChannelBySlot(0, 0, 0, 0, 0, 0, "");
    167                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    168             }
    169         } catch (SecurityException expected) {
    170         }
    171     }
    172 
    173     /**
    174      * Tests the TelephonyManager.iccTransmitApduLogicalChannel() API. This makes a call to
    175      * iccTransmitApduLogicalChannel() API and expects a SecurityException since the test apk is not
    176      * signed by a certificate on the SIM.
    177      */
    178     @Test
    179     public void testIccTransmitApduLogicalChannel() {
    180         try {
    181             if (isSimCardPresent()) {
    182                 mTelephonyManager.iccTransmitApduLogicalChannel(0, 0, 0, 0, 0, 0, "");
    183                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    184             }
    185         } catch (SecurityException expected) {
    186         }
    187     }
    188 
    189     /**
    190      * Tests the TelephonyManager.iccTransmitApduLogicalChannelBySlot() API. This makes a call to
    191      * iccTransmitApduLogicalChannelBySlot() API and expects a SecurityException since the test apk
    192      * is not signed by a certificate on the SIM.
    193      */
    194     @Test
    195     public void testIccTransmitApduLogicalChannelBySlot() {
    196         try {
    197             if (isSimCardPresent()) {
    198                 mTelephonyManager.iccTransmitApduLogicalChannelBySlot(0, 0, 0, 0, 0, 0, 0, "");
    199                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    200             }
    201         } catch (SecurityException expected) {
    202         }
    203     }
    204 
    205     /**
    206      * Tests the TelephonyManager.sendEnvelopeWithStatus() API. This makes a call to
    207      * sendEnvelopeWithStatus() API and expects a SecurityException since the test apk is not signed
    208      * by certificate on the SIM.
    209      */
    210     @Test
    211     public void testSendEnvelopeWithStatus() {
    212         try {
    213             if (isSimCardPresent()) {
    214                 mTelephonyManager.sendEnvelopeWithStatus("");
    215                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    216             }
    217         } catch (SecurityException expected) {
    218         }
    219     }
    220 
    221     /**
    222      * Tests the TelephonyManager.nvReadItem() API. This makes a call to nvReadItem() API and
    223      * expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    224      */
    225     @Test
    226     public void testNvReadItem() {
    227         try {
    228             if (isSimCardPresent()) {
    229                 mTelephonyManager.nvReadItem(0);
    230                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    231             }
    232         } catch (SecurityException expected) {
    233         }
    234     }
    235 
    236     /**
    237      * Tests the TelephonyManager.nvResetConfig() API. This makes a call to nvResetConfig() API and
    238      * expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    239      */
    240     @Test
    241     public void testNvResetConfig() {
    242         try {
    243             if (isSimCardPresent()) {
    244                 mTelephonyManager.nvResetConfig(1);
    245                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    246             }
    247         } catch (SecurityException expected) {
    248         }
    249     }
    250 
    251     /**
    252      * Tests the TelephonyManager.getPreferredNetworkType() API. This makes a call to
    253      * getPreferredNetworkType() API and expects a SecurityException since the test apk is not
    254      * signed by certificate on the SIM.
    255      */
    256     @Test
    257     public void testGetPreferredNetworkType() {
    258         try {
    259             if (isSimCardPresent()) {
    260                 mTelephonyManager.getPreferredNetworkType(0);
    261                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    262             }
    263         } catch (SecurityException expected) {
    264         }
    265     }
    266 
    267     /**
    268      * Tests the TelephonyManager.setPreferredNetworkTypeToGlobal() API. This makes a call to
    269      * setPreferredNetworkTypeToGlobal() API and expects a SecurityException since the test apk is not
    270      * signed by certificate on the SIM.
    271      */
    272     @Test
    273     public void testSetPreferredNetworkTypeToGlobal() {
    274         try {
    275             if (isSimCardPresent()) {
    276                 mTelephonyManager.setPreferredNetworkTypeToGlobal();
    277                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    278             }
    279         } catch (SecurityException expected) {
    280         }
    281     }
    282 
    283     /**
    284      * Tests that the test apk doesn't have carrier previliges.
    285      */
    286     @Test
    287     public void testHasCarrierPrivileges() {
    288         if (mTelephonyManager.hasCarrierPrivileges()) {
    289             fail("App unexpectedly has carrier privileges");
    290         }
    291     }
    292 
    293     /**
    294      * Tests the TelephonyManager.setOperatorBrandOverride() API. This makes a call to
    295      * setOperatorBrandOverride() API and expects a SecurityException since the test apk is not
    296      * signed by certificate on the SIM.
    297      */
    298     @Test
    299     public void testSetOperatorBrandOverride() {
    300         try {
    301             if (isSimCardPresent()) {
    302                 mTelephonyManager.setOperatorBrandOverride("");
    303                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    304             }
    305         } catch (SecurityException expected) {
    306         }
    307     }
    308 
    309     /**
    310      * Tests the TelephonyManager.getIccAuthentication() API. This makes a call to
    311      * getIccAuthentication() API and expects a SecurityException since the test apk is not
    312      * signed by certificate on the SIM.
    313      */
    314     @Test
    315     public void testGetIccAuthentication() {
    316         try {
    317             if (isSimCardPresent()) {
    318                 mTelephonyManager.getIccAuthentication(TelephonyManager.APPTYPE_USIM,
    319                         TelephonyManager.AUTHTYPE_EAP_AKA, "");
    320                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    321             }
    322         } catch (SecurityException expected) {
    323         }
    324     }
    325 
    326     /**
    327      * Tests the TelephonyManager.getUiccCardsInfo() API. This makes a call to  getUiccCardsInfo()
    328      * API and expects a SecurityException since the test apk is not signed by certficate on the
    329      * SIM.
    330      */
    331     @Test
    332     public void testGetUiccCardsInfo() {
    333         try {
    334             if (isSimCardPresent()) {
    335                 mTelephonyManager.getUiccCardsInfo();
    336                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    337             }
    338         } catch (SecurityException expected) {
    339         }
    340     }
    341 }
    342