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 
     18 package android.permission.cts;
     19 
     20 import android.app.ActivityManager;
     21 import android.app.AlarmManager;
     22 import android.content.Context;
     23 import android.graphics.Bitmap;
     24 import android.os.Vibrator;
     25 import android.telephony.gsm.SmsManager;
     26 import android.test.AndroidTestCase;
     27 import android.test.suitebuilder.annotation.SmallTest;
     28 
     29 import java.io.IOException;
     30 import java.io.InputStream;
     31 import java.util.TimeZone;
     32 
     33 /**
     34  * Verify the system function require specific permissions.
     35  */
     36 @SuppressWarnings("deprecation")
     37 public class NoSystemFunctionPermissionTest extends AndroidTestCase {
     38 
     39     /**
     40      * Verify that ActivityManager.restartPackage() requires permissions.
     41      * <p>Requires Permission:
     42      *   {@link android.Manifest.permission#RESTART_PACKAGES}.
     43      */
     44     @SmallTest
     45     public void testRestartPackage() {
     46         ActivityManager activityManager = (ActivityManager) mContext.getSystemService(
     47                 Context.ACTIVITY_SERVICE);
     48 
     49         try {
     50             activityManager.restartPackage("packageName");
     51             fail("ActivityManager.restartPackage() didn't throw SecurityException as expected.");
     52         } catch (SecurityException e) {
     53             // expected
     54         }
     55     }
     56 
     57     /**
     58      * Verify that AlarmManager.setTimeZone() requires permissions.
     59      * <p>Requires Permission:
     60      *   {@link android.Manifest.permission#SET_TIME_ZONE}.
     61      */
     62     @SmallTest
     63     public void testSetTimeZone() {
     64         AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(
     65                 Context.ALARM_SERVICE);
     66         String[] timeZones = TimeZone.getAvailableIDs();
     67         String timeZone = timeZones[0];
     68 
     69         try {
     70             alarmManager.setTimeZone(timeZone);
     71             fail("AlarmManager.setTimeZone() did not throw SecurityException as expected.");
     72         } catch (SecurityException e) {
     73             // expected
     74         }
     75     }
     76 
     77     /**
     78      * Verify that setting wallpaper relate methods require permissions.
     79      * <p>Requires Permission:
     80      *   {@link android.Manifest.permission#SET_WALLPAPER}.
     81      * @throws IOException
     82      */
     83     @SmallTest
     84     public void testSetWallpaper() throws IOException {
     85         Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565);
     86 
     87         try {
     88             mContext.setWallpaper(bitmap);
     89             fail("Context.setWallpaper(BitMap) did not throw SecurityException as expected.");
     90         } catch (SecurityException e) {
     91             // expected
     92         }
     93 
     94         try {
     95             mContext.setWallpaper((InputStream) null);
     96             fail("Context.setWallpaper(InputStream) did not throw SecurityException as expected.");
     97         } catch (SecurityException e) {
     98             // expected
     99         }
    100 
    101         try {
    102             mContext.clearWallpaper();
    103             fail("Context.clearWallpaper() did not throw SecurityException as expected.");
    104         } catch (SecurityException e) {
    105             // expected
    106         }
    107     }
    108 
    109     /**
    110      * Verify that Vibrator's vibrating related methods requires permissions.
    111      * <p>Requires Permission:
    112      *   {@link android.Manifest.permission#VIBRATE}.
    113      */
    114     @SmallTest
    115     public void testVibrator() {
    116         Vibrator vibrator = (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE);
    117 
    118         try {
    119             vibrator.cancel();
    120             fail("Vibrator.cancel() did not throw SecurityException as expected.");
    121         } catch (SecurityException e) {
    122             // expected
    123         }
    124 
    125         try {
    126             vibrator.vibrate(1);
    127             fail("Vibrator.vibrate(long) did not throw SecurityException as expected.");
    128         } catch (SecurityException e) {
    129             // expected
    130         }
    131 
    132         long[] testPattern = {1, 1, 1, 1, 1};
    133 
    134         try {
    135             vibrator.vibrate(testPattern, 1);
    136             fail("Vibrator.vibrate(long[], int) not throw SecurityException as expected.");
    137         } catch (SecurityException e) {
    138             // expected
    139         }
    140     }
    141 
    142     /**
    143      * Verify that sending sms requires permissions.
    144      * <p>Requires Permission:
    145      *   {@link android.Manifest.permission#SMS}.
    146      */
    147     @SmallTest
    148     public void testSendSms() {
    149         SmsManager smsManager = SmsManager.getDefault();
    150         byte[] testData = new byte[10];
    151         try {
    152             smsManager.sendDataMessage("1233", "1233", (short) 0, testData, null, null);
    153             fail("SmsManager.sendDataMessage() did not throw SecurityException as expected.");
    154         } catch (SecurityException e) {
    155             // expected
    156         }
    157     }
    158 }
    159