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.telephony.cts;
     18 
     19 import android.content.Context;
     20 import android.telephony.SmsManager;
     21 import android.telephony.TelephonyManager;
     22 import android.test.AndroidTestCase;
     23 
     24 public class SimRestrictedApisTest extends AndroidTestCase {
     25     private static final byte[] TEST_PDU = { 0, 0 };
     26     private TelephonyManager mTelephonyManager;
     27 
     28     @Override
     29     protected void setUp() throws Exception {
     30         super.setUp();
     31         mTelephonyManager =
     32                 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
     33     }
     34 
     35     private boolean isSimCardPresent() {
     36         return mTelephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE &&
     37                 mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT;
     38     }
     39 
     40     /**
     41      * Tests the SmsManager.injectSmsPdu() API. This makes a call to injectSmsPdu() API and expects
     42      * a SecurityException since the test apk is not signed by a certificate on the SIM.
     43      */
     44     public void testInjectSmsPdu() {
     45         try {
     46             if (isSimCardPresent()) {
     47                 SmsManager.getDefault().injectSmsPdu(TEST_PDU, "3gpp", null);
     48                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     49             }
     50         } catch (SecurityException expected) {
     51         }
     52     }
     53 
     54     /**
     55      * Tests the TelephonyManager.setLine1NumberForDisplay() API. This makes a call to
     56      * setLine1NumberForDisplay() API and expects a SecurityException since the test apk is not
     57      * signed by a certificate on the SIM.
     58      */
     59     public void testSetLine1NumberForDisplay() {
     60         try {
     61             if (isSimCardPresent()) {
     62                 TelephonyManager.getDefault().setLine1NumberForDisplay("", "");
     63                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     64             }
     65         } catch (SecurityException expected) {
     66         }
     67     }
     68 
     69     /**
     70      * Tests the TelephonyManager.setLine1NumberForDisplay(long, string, string) API. This makes a
     71      * call to setLine1NumberForDisplay() API and expects a SecurityException since the test apk is
     72      * not signed by the certificate on the SIM.
     73      */
     74     public void testSetLine1NumberForDisplay2() {
     75         try {
     76             if (isSimCardPresent()) {
     77                 TelephonyManager.getDefault().setLine1NumberForDisplay(0, "", "");
     78                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     79             }
     80         } catch (SecurityException expected) {
     81         }
     82     }
     83 
     84     /**
     85      * Tests the TelephonyManager.iccOpenLogicalChannel() API. This makes a call to
     86      * iccOpenLogicalChannel() API and expects a SecurityException since the test apk is not signed
     87      * by certificate on the SIM.
     88      */
     89     public void testIccOpenLogicalChannel() {
     90         try {
     91             if (isSimCardPresent()) {
     92                 TelephonyManager.getDefault().iccOpenLogicalChannel("");
     93                 fail("Expected SecurityException. App doesn't have carrier privileges.");
     94             }
     95         } catch (SecurityException expected) {
     96         }
     97     }
     98 
     99     /**
    100      * Tests the TelephonyManager.iccCloseLogicalChannel() API. This makes a call to
    101      * iccCloseLogicalChannel() API and expects a SecurityException since the test apk is not signed
    102      * by certificate on the SIM.
    103      */
    104     public void testIccCloseLogicalChannel() {
    105         try {
    106             if (isSimCardPresent()) {
    107                 TelephonyManager.getDefault().iccCloseLogicalChannel(0);
    108                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    109             }
    110         } catch (SecurityException expected) {
    111         }
    112     }
    113 
    114     /**
    115      * Tests the TelephonyManager.iccTransmitApduLogicalChannel() API. This makes a call to
    116      * iccTransmitApduLogicalChannel() API and expects a SecurityException since the test apk is not
    117      * signed by a certificate on the SIM.
    118      */
    119     public void testIccTransmitApduLogicalChannel() {
    120         try {
    121             if (isSimCardPresent()) {
    122                 TelephonyManager.getDefault().iccTransmitApduLogicalChannel(0, 0, 0, 0, 0, 0, "");
    123                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    124             }
    125         } catch (SecurityException expected) {
    126         }
    127     }
    128 
    129     /**
    130      * Tests the TelephonyManager.iccTransmitApduBasicChannel() API. This makes a call to
    131      * iccTransmitApduBasicChannel() API and expects a SecurityException since the test apk is not
    132      * signed by a certificate on the SIM.
    133      */
    134     public void testIccTransmitApduBasicChannel() {
    135         try {
    136             if (isSimCardPresent()) {
    137                 TelephonyManager.getDefault().iccTransmitApduBasicChannel(0, 0, 0, 0, 0, "");
    138                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    139             }
    140         } catch (SecurityException expected) {
    141         }
    142     }
    143 
    144     /**
    145      * Tests the TelephonyManager.sendEnvelopeWithStatus() API. This makes a call to
    146      * sendEnvelopeWithStatus() API and expects a SecurityException since the test apk is not signed
    147      * by certificate on the SIM.
    148      */
    149     public void testSendEnvelopeWithStatus() {
    150         try {
    151             if (isSimCardPresent()) {
    152                 TelephonyManager.getDefault().sendEnvelopeWithStatus("");
    153                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    154             }
    155         } catch (SecurityException expected) {
    156         }
    157     }
    158 
    159     /**
    160      * Tests the TelephonyManager.nvReadItem() API. This makes a call to nvReadItem() API and
    161      * expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    162      */
    163     public void testNvReadItem() {
    164         try {
    165             if (isSimCardPresent()) {
    166                 TelephonyManager.getDefault().nvReadItem(0);
    167                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    168             }
    169         } catch (SecurityException expected) {
    170         }
    171     }
    172 
    173     /**
    174      * Tests the TelephonyManager.nvWriteItem() API. This makes a call to nvWriteItem() API and
    175      * expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    176      */
    177     public void testNvWriteItem() {
    178         try {
    179             if (isSimCardPresent()) {
    180                 TelephonyManager.getDefault().nvWriteItem(0, "");
    181                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    182             }
    183         } catch (SecurityException expected) {
    184         }
    185     }
    186 
    187     /**
    188      * Tests the TelephonyManager.nvWriteCdmaPrl() API. This makes a call to nvWriteCdmaPrl() API
    189      * and expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    190      */
    191     public void testNvWriteCdmaPrl() {
    192         try {
    193             if (isSimCardPresent()) {
    194                 TelephonyManager.getDefault().nvWriteCdmaPrl(null);
    195                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    196             }
    197         } catch (SecurityException expected) {
    198         }
    199     }
    200 
    201     /**
    202      * Tests the TelephonyManager.nvResetConfig() API. This makes a call to nvResetConfig() API and
    203      * expects a SecurityException since the test apk is not signed by a certificate on the SIM.
    204      */
    205     public void testNvResetConfig() {
    206         try {
    207             if (isSimCardPresent()) {
    208                 TelephonyManager.getDefault().nvResetConfig(0);
    209                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    210             }
    211         } catch (SecurityException expected) {
    212         }
    213     }
    214 
    215     /**
    216      * Tests the TelephonyManager.getPreferredNetworkType() API. This makes a call to
    217      * getPreferredNetworkType() API and expects a SecurityException since the test apk is not
    218      * signed by certificate on the SIM.
    219      */
    220     public void testGetPreferredNetworkType() {
    221         try {
    222             if (isSimCardPresent()) {
    223                 TelephonyManager.getDefault().getPreferredNetworkType(0);
    224                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    225             }
    226         } catch (SecurityException expected) {
    227         }
    228     }
    229 
    230     /**
    231      * Tests the TelephonyManager.setPreferredNetworkTypeToGlobal() API. This makes a call to
    232      * setPreferredNetworkTypeToGlobal() API and expects a SecurityException since the test apk is not
    233      * signed by certificate on the SIM.
    234      */
    235     public void testSetPreferredNetworkTypeToGlobal() {
    236         try {
    237             if (isSimCardPresent()) {
    238                 TelephonyManager.getDefault().setPreferredNetworkTypeToGlobal();
    239                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    240             }
    241         } catch (SecurityException expected) {
    242         }
    243     }
    244 
    245     /**
    246      * Tests that the test apk doesn't have carrier previliges.
    247      */
    248     public void testHasCarrierPrivileges() {
    249         if (TelephonyManager.getDefault().hasCarrierPrivileges()) {
    250             fail("App unexpectedly has carrier privileges");
    251         }
    252     }
    253 
    254     /**
    255      * Tests the TelephonyManager.setOperatorBrandOverride() API. This makes a call to
    256      * setOperatorBrandOverride() API and expects a SecurityException since the test apk is not
    257      * signed by certificate on the SIM.
    258      */
    259     public void testSetOperatorBrandOverride() {
    260         try {
    261             if (isSimCardPresent()) {
    262                 TelephonyManager.getDefault().setOperatorBrandOverride("");
    263                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    264             }
    265         } catch (SecurityException expected) {
    266         }
    267     }
    268 
    269     /**
    270      * Tests the TelephonyManager.getIccAuthentication() API. This makes a call to
    271      * getIccAuthentication() API and expects a SecurityException since the test apk is not
    272      * signed by certificate on the SIM.
    273      */
    274     public void testGetIccAuthentication() {
    275         try {
    276             if (isSimCardPresent()) {
    277                 TelephonyManager.getDefault().getIccAuthentication(TelephonyManager.APPTYPE_USIM,
    278                         TelephonyManager.AUTHTYPE_EAP_AKA, "");
    279                 fail("Expected SecurityException. App doesn't have carrier privileges.");
    280             }
    281         } catch (SecurityException expected) {
    282         }
    283     }
    284 }
    285