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