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 com.android.framework.permission.tests;
18 
19 import android.telephony.SmsManager;
20 import android.test.AndroidTestCase;
21 
22 import androidx.test.filters.SmallTest;
23 
24 import java.util.ArrayList;
25 
26 /**
27  * Verify that SmsManager apis cannot be called without required permissions.
28  */
29 public class SmsManagerPermissionTest extends AndroidTestCase {
30 
31     private static final String MSG_CONTENTS = "hi";
32     private static final short DEST_PORT = (short)1004;
33     private static final String DEST_NUMBER = "4567";
34     private static final String SRC_NUMBER = "1234";
35 
36     private static final int CELL_BROADCAST_MESSAGE_ID_START = 10;
37     private static final int CELL_BROADCAST_MESSAGE_ID_END = 20;
38     private static final int CELL_BROADCAST_GSM_RAN_TYPE = 0;
39 
40     /**
41      * Verify that SmsManager.sendTextMessage requires permissions.
42      * <p>Tests Permission:
43      *   {@link android.Manifest.permission#SEND_SMS}.
44      */
45     @SmallTest
testSendTextMessage()46     public void testSendTextMessage() {
47         try {
48             SmsManager.getDefault().sendTextMessage(SRC_NUMBER, DEST_NUMBER, MSG_CONTENTS, null,
49                     null);
50             fail("SmsManager.sendTextMessage did not throw SecurityException as expected");
51         } catch (SecurityException e) {
52             // expected
53         }
54     }
55 
56     /**
57      * Verify that SmsManager.sendDataMessage requires permissions.
58      * <p>Tests Permission:
59      *   {@link android.Manifest.permission#SEND_SMS}.
60      */
61     @SmallTest
testSendDataMessage()62     public void testSendDataMessage() {
63         try {
64             SmsManager.getDefault().sendDataMessage(SRC_NUMBER, DEST_NUMBER, DEST_PORT,
65                     MSG_CONTENTS.getBytes(), null, null);
66             fail("SmsManager.sendDataMessage did not throw SecurityException as expected");
67         } catch (SecurityException e) {
68             // expected
69         }
70     }
71 
72     /**
73      * Verify that SmsManager.sendMultipartMessage requires permissions.
74      * <p>Tests Permission:
75      *   {@link android.Manifest.permission#SEND_MMS}.
76      */
77     @SmallTest
testSendMultipartMessage()78     public void testSendMultipartMessage() {
79         try {
80             ArrayList<String> msgParts = new ArrayList<String>(2);
81             msgParts.add(MSG_CONTENTS);
82             msgParts.add("foo");
83             SmsManager.getDefault().sendMultipartTextMessage(SRC_NUMBER, DEST_NUMBER, msgParts,
84                     null, null);
85             fail("SmsManager.sendMultipartTextMessage did not throw SecurityException as expected");
86         } catch (SecurityException e) {
87             // expected
88         }
89     }
90 
91     /**
92      * Verify that SmsManager.enableCellBroadcastRange requires permissions.
93      * <p>Tests system permission: android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST
94      */
95     @SmallTest
testEnableCellBroadcastRange()96     public void testEnableCellBroadcastRange() {
97         try {
98             SmsManager.getDefault().enableCellBroadcastRange(CELL_BROADCAST_MESSAGE_ID_START,
99                     CELL_BROADCAST_MESSAGE_ID_END, CELL_BROADCAST_GSM_RAN_TYPE);
100             fail("SmsManager.sendDataMessage did not throw SecurityException as expected");
101         } catch (SecurityException e) {
102             // expected
103         }
104     }
105 
106     /**
107      * Verify that SmsManager.disableCellBroadcastRange requires permissions.
108      * <p>Tests system permission: android.Manifest.permission.RECEIVE_EMERGENCY_BROADCAST
109      */
110     @SmallTest
testDisableCellBroadcastRange()111     public void testDisableCellBroadcastRange() {
112         try {
113             SmsManager.getDefault().disableCellBroadcastRange(CELL_BROADCAST_MESSAGE_ID_START,
114                     CELL_BROADCAST_MESSAGE_ID_END, CELL_BROADCAST_GSM_RAN_TYPE);
115             fail("SmsManager.sendDataMessage did not throw SecurityException as expected");
116         } catch (SecurityException e) {
117             // expected
118         }
119     }
120 }
121