Home | History | Annotate | Download | only in deviceowner
      1 /*
      2  * Copyright (C) 2015 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.cts.deviceowner;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.net.wifi.WifiConfiguration;
     22 import android.net.wifi.WifiManager;
     23 import android.provider.Settings;
     24 
     25 import com.android.compatibility.common.util.WifiConfigCreator;
     26 
     27 import java.util.List;
     28 
     29 import static com.android.compatibility.common.util.WifiConfigCreator.ACTION_CREATE_WIFI_CONFIG;
     30 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_NETID;
     31 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_PASSWORD;
     32 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_SECURITY_TYPE;
     33 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_SSID;
     34 import static com.android.compatibility.common.util.WifiConfigCreator.ACTION_REMOVE_WIFI_CONFIG;
     35 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_NONE;
     36 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_WPA;
     37 import static com.android.compatibility.common.util.WifiConfigCreator.ACTION_UPDATE_WIFI_CONFIG;
     38 
     39 /**
     40  * Testing WiFi configuration lockdown by Device Owner
     41  */
     42 public class WifiConfigLockdownTest extends BaseDeviceOwnerTest {
     43     private static final String TAG = "WifiConfigLockdownTest";
     44     private static final String ORIGINAL_DEVICE_OWNER_SSID = "DOCTSTest";
     45     private static final String CHANGED_DEVICE_OWNER_SSID = "DOChangedCTSTest";
     46     private static final String ORIGINAL_REGULAR_SSID = "RegularCTSTest";
     47     private static final String CHANGED_REGULAR_SSID = "RegularChangedCTSTest";
     48     private static final String ORIGINAL_PASSWORD = "originalpassword";
     49     private WifiManager mWifiManager;
     50     private WifiConfigCreator mConfigCreator;
     51 
     52     @Override
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55         mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
     56         mConfigCreator = new WifiConfigCreator(mContext);
     57         mDevicePolicyManager.setGlobalSetting(getWho(),
     58                 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, "1");
     59         mConfigCreator.addNetwork(ORIGINAL_DEVICE_OWNER_SSID, true, SECURITY_TYPE_WPA,
     60                 ORIGINAL_PASSWORD);
     61         startRegularActivity(ACTION_CREATE_WIFI_CONFIG, -1, ORIGINAL_REGULAR_SSID,
     62                 SECURITY_TYPE_WPA, ORIGINAL_PASSWORD);
     63     }
     64 
     65     @Override
     66     protected void tearDown() throws Exception {
     67         mDevicePolicyManager.setGlobalSetting(getWho(),
     68                 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN, "0");
     69         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
     70         for (WifiConfiguration config : configs) {
     71             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID) ||
     72                     areMatchingSsids(CHANGED_DEVICE_OWNER_SSID, config.SSID) ||
     73                     areMatchingSsids(ORIGINAL_REGULAR_SSID, config.SSID) ||
     74                     areMatchingSsids(CHANGED_REGULAR_SSID, config.SSID)) {
     75                 mWifiManager.removeNetwork(config.networkId);
     76             }
     77         }
     78         super.tearDown();
     79     }
     80 
     81     public void testDeviceOwnerCanUpdateConfig() throws Exception {
     82         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
     83         int updateCount = 0;
     84         for (WifiConfiguration config : configs) {
     85             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID)) {
     86                 assertFalse(-1 == mConfigCreator.updateNetwork(config,
     87                         CHANGED_DEVICE_OWNER_SSID, true, SECURITY_TYPE_NONE, null));
     88                 ++updateCount;
     89             }
     90             if (areMatchingSsids(ORIGINAL_REGULAR_SSID, config.SSID)) {
     91                 assertFalse(-1 == mConfigCreator.updateNetwork(config,
     92                         CHANGED_REGULAR_SSID, true, SECURITY_TYPE_NONE, null));
     93                 ++updateCount;
     94             }
     95         }
     96         assertEquals("Expected to update two configs: the DO created one and the regular one." +
     97                 " Instead updated: " + updateCount, 2, updateCount);
     98     }
     99 
    100     public void testDeviceOwnerCanRemoveConfig() throws Exception {
    101         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    102         int removeCount = 0;
    103         for (WifiConfiguration config : configs) {
    104             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID) ||
    105                     areMatchingSsids(ORIGINAL_REGULAR_SSID, config.SSID)) {
    106                 assertTrue(mWifiManager.removeNetwork(config.networkId));
    107                 ++removeCount;
    108             }
    109         }
    110         assertEquals("Expected to remove two configs: the DO created one and the regular one." +
    111                 " Instead removed: " + removeCount, 2, removeCount);
    112     }
    113 
    114     public void testRegularAppCannotUpdateDeviceOwnerConfig() throws Exception {
    115         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    116         int updateCount = 0;
    117         for (WifiConfiguration config : configs) {
    118             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID)) {
    119                 startRegularActivity(ACTION_UPDATE_WIFI_CONFIG, config.networkId,
    120                         CHANGED_DEVICE_OWNER_SSID, SECURITY_TYPE_NONE, null);
    121                 ++updateCount;
    122             }
    123         }
    124         assertEquals("Expected to have tried to update one config: the DO created one" +
    125                 " Instead tried to update: " + updateCount, 1, updateCount);
    126 
    127         // Assert nothing has changed
    128         configs = mWifiManager.getConfiguredNetworks();
    129         int notChangedCount = 0;
    130         for (WifiConfiguration config : configs) {
    131             assertFalse(areMatchingSsids(CHANGED_DEVICE_OWNER_SSID, config.SSID));
    132             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID)) {
    133                 ++notChangedCount;
    134             }
    135         }
    136         assertEquals("Expected to see one unchanged config, saw instead: " + notChangedCount, 1,
    137                 notChangedCount);
    138     }
    139 
    140     public void testRegularAppCannotRemoveDeviceOwnerConfig() throws Exception {
    141         List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
    142         int removeCount = 0;
    143         for (WifiConfiguration config : configs) {
    144             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID)) {
    145                 startRegularActivity(ACTION_REMOVE_WIFI_CONFIG, config.networkId,
    146                         null, SECURITY_TYPE_NONE, null);
    147                 ++removeCount;
    148             }
    149         }
    150         assertEquals("Expected to try to remove one config: the DO created one." +
    151                 " Instead tried to remove: " + removeCount, 1, removeCount);
    152 
    153         // Assert nothing has changed
    154         configs = mWifiManager.getConfiguredNetworks();
    155         int notChangedCount = 0;
    156         for (WifiConfiguration config : configs) {
    157             if (areMatchingSsids(ORIGINAL_DEVICE_OWNER_SSID, config.SSID)) {
    158                 ++notChangedCount;
    159             }
    160         }
    161         assertEquals("Expected to see one unchanged config, saw instead: " + notChangedCount, 1,
    162                 notChangedCount);
    163     }
    164 
    165     private void startRegularActivity(String action, int netId, String ssid, int securityType,
    166             String password) throws InterruptedException {
    167         Intent createRegularConfig = new Intent(action);
    168         createRegularConfig.putExtra(EXTRA_NETID, netId);
    169         createRegularConfig.putExtra(EXTRA_SSID, ssid);
    170         createRegularConfig.putExtra(EXTRA_SECURITY_TYPE, securityType);
    171         createRegularConfig.putExtra(EXTRA_PASSWORD, password);
    172         createRegularConfig.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    173         mContext.startActivity(createRegularConfig);
    174 
    175         // Give some time for the other app to finish the action
    176         Thread.sleep(2000);
    177     }
    178 
    179     private boolean areMatchingSsids(String s1, String s2) {
    180         if (s1 == null || s2 == null) {
    181             return false;
    182         }
    183         return s1.replace("\"", "").equals(s2.replace("\"", ""));
    184     }
    185 }
    186