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