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 package android.permission.cts;
     18 
     19 import android.content.Context;
     20 import android.net.wifi.WifiConfiguration;
     21 import android.net.wifi.WifiManager;
     22 import android.platform.test.annotations.AppModeFull;
     23 import android.test.AndroidTestCase;
     24 import android.test.suitebuilder.annotation.SmallTest;
     25 
     26 /**
     27  * Verify WifiManager related methods without specific Wifi state permissions.
     28  */
     29 @AppModeFull(reason = "Instant apps cannot access the WifiManager")
     30 @SmallTest
     31 public class NoWifiStatePermissionTest extends AndroidTestCase {
     32     private static final int TEST_NET_ID = 1;
     33     private static final WifiConfiguration TEST_WIFI_CONFIGURATION = new WifiConfiguration();
     34     private WifiManager mWifiManager;
     35 
     36     @Override
     37     protected void setUp() throws Exception {
     38         super.setUp();
     39         mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
     40         assertNotNull(mWifiManager);
     41     }
     42 
     43     /**
     44      * Verify that WifiManager#getWifiState() requires permissions.
     45      * <p>Requires Permission:
     46      *   {@link android.Manifest.permission#ACCESS_WIFI_STATE}.
     47      */
     48     public void testGetWifiState() {
     49         try {
     50             mWifiManager.getWifiState();
     51             fail("WifiManager.getWifiState didn't throw SecurityException as expected");
     52         } catch (SecurityException e) {
     53             // expected
     54         }
     55     }
     56 
     57     /**
     58      * Verify that WifiManager#getConfiguredNetworks() requires permissions.
     59      * <p>Requires Permission:
     60      *   {@link android.Manifest.permission#ACCESS_WIFI_STATE}.
     61      */
     62     public void testGetConfiguredNetworks() {
     63         try {
     64             mWifiManager.getConfiguredNetworks();
     65             fail("WifiManager.getConfiguredNetworks didn't throw SecurityException as expected");
     66         } catch (SecurityException e) {
     67             // expected
     68         }
     69     }
     70 
     71     /**
     72      * Verify that WifiManager#getConnectionInfo() requires permissions.
     73      * <p>Requires Permission:
     74      *   {@link android.Manifest.permission#ACCESS_WIFI_STATE}.
     75      */
     76     public void testGetConnectionInfo() {
     77         try {
     78             mWifiManager.getConnectionInfo();
     79             fail("WifiManager.getConnectionInfo didn't throw SecurityException as expected");
     80         } catch (SecurityException e) {
     81             // expected
     82         }
     83     }
     84 
     85     /**
     86      * Verify that WifiManager#getScanResults() requires permissions.
     87      * <p>Requires Permission:
     88      *   {@link android.Manifest.permission#ACCESS_WIFI_STATE}.
     89      */
     90     public void testGetScanResults() {
     91         try {
     92             mWifiManager.getScanResults();
     93             fail("WifiManager.getScanResults didn't throw SecurityException as expected");
     94         } catch (SecurityException e) {
     95             // expected
     96         }
     97     }
     98 
     99     /**
    100      * Verify that WifiManager#getDhcpInfo() requires permissions.
    101      * <p>Requires Permission:
    102      *   {@link android.Manifest.permission#ACCESS_WIFI_STATE}.
    103      */
    104     public void testGetDhcpInfo() {
    105         try {
    106             mWifiManager.getDhcpInfo();
    107             fail("WifiManager.getDhcpInfo didn't throw SecurityException as expected");
    108         } catch (SecurityException e) {
    109             // expected
    110         }
    111     }
    112 
    113     /**
    114      * Verify that WifiManager#disconnect() requires permissions.
    115      * <p>Requires Permission:
    116      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    117      */
    118     public void testDisconnect() {
    119         try {
    120             mWifiManager.disconnect();
    121             fail("WifiManager.disconnect didn't throw SecurityException as expected");
    122         } catch (SecurityException e) {
    123             // expected
    124         }
    125     }
    126 
    127     /**
    128      * Verify that WifiManager#reconnect() requires permissions.
    129      * <p>Requires Permission:
    130      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    131      */
    132     public void testReconnect() {
    133         try {
    134             mWifiManager.reconnect();
    135             fail("WifiManager.reconnect didn't throw SecurityException as expected");
    136         } catch (SecurityException e) {
    137             // expected
    138         }
    139     }
    140 
    141     /**
    142      * Verify that WifiManager#reassociate() requires permissions.
    143      * <p>Requires Permission:
    144      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    145      */
    146     public void testReassociate() {
    147         try {
    148             mWifiManager.reassociate();
    149             fail("WifiManager.reassociate didn't throw SecurityException as expected");
    150         } catch (SecurityException e) {
    151             // expected
    152         }
    153     }
    154 
    155     /**
    156      * Verify that WifiManager#addNetwork() requires permissions.
    157      * <p>Requires Permission:
    158      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    159      */
    160     public void testAddNetwork() {
    161         try {
    162             mWifiManager.addNetwork(TEST_WIFI_CONFIGURATION);
    163             fail("WifiManager.addNetwork didn't throw SecurityException as expected");
    164         } catch (SecurityException e) {
    165             // expected
    166         }
    167     }
    168 
    169     /**
    170      * Verify that WifiManager#updateNetwork() requires permissions.
    171      * <p>Requires Permission:
    172      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    173      */
    174     public void testUpdateNetwork() {
    175         TEST_WIFI_CONFIGURATION.networkId = 2;
    176 
    177         try {
    178             mWifiManager.updateNetwork(TEST_WIFI_CONFIGURATION);
    179             fail("WifiManager.updateNetwork didn't throw SecurityException as expected");
    180         } catch (SecurityException e) {
    181             // expected
    182         }
    183     }
    184 
    185     /**
    186      * Verify that WifiManager#removeNetwork() requires permissions.
    187      * <p>Requires Permission:
    188      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    189      */
    190     public void testRemoveNetwork() {
    191         try {
    192             mWifiManager.removeNetwork(TEST_NET_ID);
    193             fail("WifiManager.removeNetwork didn't throw SecurityException as expected");
    194         } catch (SecurityException e) {
    195             // expected
    196         }
    197     }
    198 
    199     /**
    200      * Verify that WifiManager#enableNetwork() requires permissions.
    201      * <p>Requires Permission:
    202      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    203      */
    204     public void testEnableNetwork() {
    205         try {
    206             mWifiManager.enableNetwork(TEST_NET_ID, false);
    207             fail("WifiManager.enableNetwork didn't throw SecurityException as expected");
    208         } catch (SecurityException e) {
    209             // expected
    210         }
    211     }
    212 
    213     /**
    214      * Verify that WifiManager#disableNetwork() requires permissions.
    215      * <p>Requires Permission:
    216      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    217      */
    218     public void testDisableNetwork() {
    219         try {
    220             mWifiManager.disableNetwork(TEST_NET_ID);
    221             fail("WifiManager.disableNetwork didn't throw SecurityException as expected");
    222         } catch (SecurityException e) {
    223             // expected
    224         }
    225     }
    226 
    227     /**
    228      * Verify that WifiManager#pingSupplicant() requires permissions.
    229      * <p>Requires Permission:
    230      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    231      */
    232     public void testPingSupplicant() {
    233         try {
    234             mWifiManager.pingSupplicant();
    235             fail("WifiManager.pingSupplicant didn't throw SecurityException as expected");
    236         } catch (SecurityException e) {
    237             // expected
    238         }
    239     }
    240 
    241     /**
    242      * Verify that WifiManager#startScan() requires permissions.
    243      * <p>Requires Permission:
    244      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    245      */
    246     public void testStartScan() {
    247         try {
    248             mWifiManager.startScan();
    249             fail("WifiManager.startScan didn't throw SecurityException as expected");
    250         } catch (SecurityException e) {
    251             // expected
    252         }
    253     }
    254 
    255     /**
    256      * Verify that WifiManager#setWifiEnabled() requires permissions.
    257      * <p>Requires Permission:
    258      *   {@link android.Manifest.permission#CHANGE_WIFI_STATE}.
    259      */
    260     public void testSetWifiEnabled() {
    261         try {
    262             mWifiManager.setWifiEnabled(true);
    263             fail("WifiManager.setWifiEnabled didn't throw SecurityException as expected");
    264         } catch (SecurityException e) {
    265             // expected
    266         }
    267     }
    268 }
    269