Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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.permission2.cts;
     18 
     19 import android.content.Intent;
     20 import android.content.pm.PackageManager;
     21 import android.test.AndroidTestCase;
     22 
     23 /**
     24  * Verify that applications can not send protected broadcasts.
     25  */
     26 public class ProtectedBroadcastsTest extends AndroidTestCase {
     27     private static final String BROADCASTS[] = new String[] {
     28         Intent.ACTION_SCREEN_OFF,
     29         Intent.ACTION_SCREEN_ON,
     30         Intent.ACTION_USER_PRESENT,
     31         Intent.ACTION_TIME_TICK,
     32         Intent.ACTION_TIMEZONE_CHANGED,
     33         Intent.ACTION_BOOT_COMPLETED,
     34         Intent.ACTION_PACKAGE_INSTALL,
     35         Intent.ACTION_PACKAGE_ADDED,
     36         Intent.ACTION_PACKAGE_REPLACED,
     37         Intent.ACTION_PACKAGE_REMOVED,
     38         Intent.ACTION_PACKAGE_CHANGED,
     39         Intent.ACTION_PACKAGE_RESTARTED,
     40         Intent.ACTION_PACKAGE_DATA_CLEARED,
     41         Intent.ACTION_UID_REMOVED,
     42         Intent.ACTION_CONFIGURATION_CHANGED,
     43         Intent.ACTION_BATTERY_CHANGED,
     44         Intent.ACTION_BATTERY_LOW,
     45         Intent.ACTION_BATTERY_OKAY,
     46         Intent.ACTION_POWER_CONNECTED,
     47         Intent.ACTION_POWER_DISCONNECTED,
     48         Intent.ACTION_SHUTDOWN,
     49         Intent.ACTION_DEVICE_STORAGE_LOW,
     50         Intent.ACTION_DEVICE_STORAGE_OK,
     51         Intent.ACTION_REBOOT,
     52         "com.android.server.WifiManager.action.START_SCAN",
     53         "com.android.server.WifiManager.action.DELAYED_DRIVER_STOP",
     54         "android.net.wifi.WIFI_STATE_CHANGED",
     55         "android.net.wifi.WIFI_AP_STATE_CHANGED",
     56         "android.net.wifi.SCAN_RESULTS",
     57         "android.net.wifi.RSSI_CHANGED",
     58         "android.net.wifi.STATE_CHANGE",
     59         "android.net.wifi.LINK_CONFIGURATION_CHANGED",
     60         "android.net.wifi.CONFIGURED_NETWORKS_CHANGE",
     61         "android.net.wifi.supplicant.CONNECTION_CHANGE",
     62         "android.net.wifi.supplicant.STATE_CHANGE",
     63         "android.net.wifi.p2p.STATE_CHANGED",
     64         "android.net.wifi.p2p.DISCOVERY_STATE_CHANGE",
     65         "android.net.wifi.p2p.THIS_DEVICE_CHANGED",
     66         "android.net.wifi.p2p.PEERS_CHANGED",
     67         "android.net.wifi.p2p.CONNECTION_STATE_CHANGE",
     68         "android.net.wifi.p2p.PERSISTENT_GROUPS_CHANGED",
     69         "android.net.conn.TETHER_STATE_CHANGED",
     70         "android.net.conn.INET_CONDITION_ACTION",
     71         "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED",
     72         "com.android.server.InputMethodManagerService.SHOW_INPUT_METHOD_PICKER"
     73     };
     74 
     75     private static final String BROADCASTS_TELEPHONY[] = new String[] {
     76         Intent.ACTION_NEW_OUTGOING_CALL,
     77         "android.intent.action.SERVICE_STATE",
     78         "android.intent.action.SIG_STR",
     79         "android.intent.action.RADIO_TECHNOLOGY",
     80         "android.intent.action.ANY_DATA_STATE",
     81         "android.intent.action.ACTION_MDN_STATE_CHANGED",
     82         "android.provider.Telephony.SPN_STRINGS_UPDATED",
     83         "android.intent.action.EMERGENCY_CALLBACK_MODE_CHANGED",
     84         "android.intent.action.SIM_STATE_CHANGED",
     85         "android.intent.action.DATA_CONNECTION_FAILED",
     86         "android.intent.action.NETWORK_SET_TIME",
     87         "android.intent.action.NETWORK_SET_TIMEZONE",
     88         "com.android.internal.intent.action.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS",
     89         "android.telephony.action.SUBSCRIPTION_CARRIER_IDENTITY_CHANGED",
     90     };
     91 
     92     /**
     93      * Verify that protected broadcast actions can't be sent.
     94      */
     95     public void testSendProtectedBroadcasts() {
     96         for (String action : BROADCASTS) {
     97             try {
     98                 Intent intent = new Intent(action);
     99                 getContext().sendBroadcast(intent);
    100                 fail("expected security exception broadcasting action: " + action);
    101             } catch (SecurityException expected) {
    102                 assertNotNull("security exception's error message.", expected.getMessage());
    103             }
    104         }
    105     }
    106 
    107     public void testSendProtectedTelephonyBroadcasts() {
    108         if (!getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
    109             return;
    110         }
    111         for (String action : BROADCASTS_TELEPHONY) {
    112             try {
    113                 Intent intent = new Intent(action);
    114                 getContext().sendBroadcast(intent);
    115                 fail("expected security exception broadcasting telephony action: " + action);
    116             } catch (SecurityException expected) {
    117                 assertNotNull("security exception's error message.", expected.getMessage());
    118             }
    119         }
    120     }
    121 }
    122