Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2017 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.WallpaperManager;
     21 import android.content.Context;
     22 import android.graphics.Bitmap;
     23 import android.media.MediaPlayer;
     24 import android.net.wifi.WifiManager;
     25 import android.net.wifi.WifiManager.WifiLock;
     26 import android.os.PowerManager;
     27 import android.platform.test.annotations.AppModeFull;
     28 import android.test.AndroidTestCase;
     29 import android.test.suitebuilder.annotation.SmallTest;
     30 
     31 import java.io.ByteArrayInputStream;
     32 import java.io.IOException;
     33 
     34 import static android.app.WallpaperManager.FLAG_SYSTEM;
     35 import static android.app.WallpaperManager.FLAG_LOCK;
     36 
     37 /**
     38  * Verify that Wallpaper-related operations enforce the correct permissions.
     39  */
     40 @AppModeFull(reason = "instant apps cannot access the WallpaperManager")
     41 public class NoWallpaperPermissionsTest extends AndroidTestCase {
     42     private WallpaperManager mWM;
     43 
     44     @Override
     45     protected void setUp() throws Exception {
     46         super.setUp();
     47         mWM = (WallpaperManager) mContext.getSystemService(Context.WALLPAPER_SERVICE);
     48     }
     49 
     50     /**
     51      * Verify that the setResource(...) methods enforce the SET_WALLPAPER permission
     52      */
     53     @SmallTest
     54     public void testSetResource() throws IOException {
     55         if (wallpaperNotSupported()) {
     56             return;
     57         }
     58 
     59         try {
     60             mWM.setResource(R.drawable.robot);
     61             fail("WallpaperManager.setResource(id) did not enforce SET_WALLPAPER");
     62         } catch (SecurityException expected) { /* expected */ }
     63 
     64         try {
     65             mWM.setResource(R.drawable.robot, FLAG_LOCK);
     66             fail("WallpaperManager.setResource(id, which) did not enforce SET_WALLPAPER");
     67         } catch (SecurityException expected) { /* expected */ }
     68     }
     69 
     70     /**
     71      * Verify that the setBitmap(...) methods enforce the SET_WALLPAPER permission
     72      */
     73     @SmallTest
     74     public void testSetBitmap() throws IOException  {
     75         if (wallpaperNotSupported()) {
     76             return;
     77         }
     78 
     79         Bitmap b = Bitmap.createBitmap(160, 120, Bitmap.Config.RGB_565);
     80 
     81         try {
     82             mWM.setBitmap(b);
     83             fail("setBitmap(b) did not enforce SET_WALLPAPER");
     84         } catch (SecurityException expected) { /* expected */ }
     85 
     86         try {
     87             mWM.setBitmap(b, null, false);
     88             fail("setBitmap(b, crop, allowBackup) did not enforce SET_WALLPAPER");
     89         } catch (SecurityException expected) { /* expected */ }
     90 
     91         try {
     92             mWM.setBitmap(b, null, false, FLAG_SYSTEM);
     93             fail("setBitmap(b, crop, allowBackup, which) did not enforce SET_WALLPAPER");
     94         } catch (SecurityException expected) { /* expected */ }
     95     }
     96 
     97     /**
     98      * Verify that the setStream(...) methods enforce the SET_WALLPAPER permission
     99      */
    100     @SmallTest
    101     public void testSetStream() throws IOException  {
    102         if (wallpaperNotSupported()) {
    103             return;
    104         }
    105 
    106         ByteArrayInputStream stream = new ByteArrayInputStream(new byte[32]);
    107 
    108         try {
    109             mWM.setStream(stream);
    110             fail("setStream(stream) did not enforce SET_WALLPAPER");
    111         } catch (SecurityException expected) { /* expected */ }
    112 
    113         try {
    114             mWM.setStream(stream, null, false);
    115             fail("setStream(stream, crop, allowBackup) did not enforce SET_WALLPAPER");
    116         } catch (SecurityException expected) { /* expected */ }
    117 
    118         try {
    119             mWM.setStream(stream, null, false, FLAG_LOCK);
    120             fail("setStream(stream, crop, allowBackup, which) did not enforce SET_WALLPAPER");
    121         } catch (SecurityException expected) { /* expected */ }
    122     }
    123 
    124     /**
    125      * Verify that the clearWallpaper(...) methods enforce the SET_WALLPAPER permission
    126      */
    127     @SmallTest
    128     public void testClearWallpaper() throws IOException  {
    129         if (wallpaperNotSupported()) {
    130             return;
    131         }
    132 
    133         try {
    134             mWM.clear();
    135             fail("clear() did not enforce SET_WALLPAPER");
    136         } catch (SecurityException expected) { /* expected */ }
    137 
    138         try {
    139             mWM.clear(FLAG_SYSTEM);
    140             fail("clear(which) did not enforce SET_WALLPAPER");
    141         } catch (SecurityException expected) { /* expected */ }
    142     }
    143 
    144     /**
    145      * Verify that reading the current wallpaper requires READ_EXTERNAL_STORAGE
    146      */
    147     @SmallTest
    148     public void testReadWallpaper() {
    149         if (wallpaperNotSupported()) {
    150             return;
    151         }
    152 
    153         try {
    154             /* ignore result */ mWM.getFastDrawable();
    155             fail("getFastDrawable() did not enforce READ_EXTERNAL_STORAGE");
    156         } catch (SecurityException expected) { /* expected */ }
    157 
    158         try {
    159             /* ignore result */ mWM.peekFastDrawable();
    160             fail("peekFastDrawable() did not enforce READ_EXTERNAL_STORAGE");
    161         } catch (SecurityException expected) { /* expected */ }
    162 
    163         try {
    164             /* ignore result */ mWM.getWallpaperFile(FLAG_SYSTEM);
    165             fail("getWallpaperFile(FLAG_SYSTEM) did not enforce READ_EXTERNAL_STORAGE");
    166         } catch (SecurityException expected) { /* expected */ }
    167     }
    168 
    169     // ---------- Utility methods ----------
    170 
    171     private boolean wallpaperNotSupported() {
    172         return !(mWM.isWallpaperSupported() && mWM.isSetWallpaperAllowed());
    173     }
    174 }
    175