1947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee/* 2947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Copyright (C) 2015 The Android Open Source Project 3947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 4947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Licensed under the Apache License, Version 2.0 (the "License"); 5947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * you may not use this file except in compliance with the License. 6947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * You may obtain a copy of the License at 7947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 8947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * http://www.apache.org/licenses/LICENSE-2.0 9947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 10947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Unless required by applicable law or agreed to in writing, software 11947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * distributed under the License is distributed on an "AS IS" BASIS, 12947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * See the License for the specific language governing permissions and 14947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * limitations under the License. 15947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 16947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 17947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leepackage com.android.cts.managedprofile; 18947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 19947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.content.Context; 20947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.content.Intent; 21947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.net.wifi.WifiConfiguration; 22947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.net.wifi.WifiManager; 23947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.os.SystemClock; 24947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport android.test.AndroidTestCase; 25947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 26947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport java.util.concurrent.TimeUnit; 27947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 28947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport static com.android.compatibility.common.util.WifiConfigCreator.ACTION_CREATE_WIFI_CONFIG; 29947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport static com.android.compatibility.common.util.WifiConfigCreator.ACTION_REMOVE_WIFI_CONFIG; 30947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_NETID; 31947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leeimport static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_SSID; 32947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 33947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee/** 34947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Driven by the host-side test: com.android.cts.devicepolicy.ManagedProfileTest 35947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 36947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Each of these tests can run independently but have side-effects. The side-effects are used as 37947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * building blocks to test various cleanup routines, for example that networks belonging to one 38947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * user are deleted 39947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 40947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Leepublic class WifiTest extends AndroidTestCase { 41947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private static final String TAG = WifiTest.class.getSimpleName(); 42947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 43947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee // Unique SSID to use for this test (max SSID length is 32) 44947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private static final String NETWORK_SSID = "com.android.cts.xwde7ktvh8rmjuhr"; 45947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 46947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee // Time duration to allow before assuming that a WiFi operation failed and ceasing to wait. 47283400b9d7c0105b1331f4921f801013afcb7f6fLenka Trochtova private static final long UPDATE_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(5); 48947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private static final long UPDATE_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1); 49947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 50947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee // Shared WifiManager instance. 51947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private WifiManager mWifiManager; 52947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 53947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee @Override 54947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee public void setUp() throws Exception { 55947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee super.setUp(); 56947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 57947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 58947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 59947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee /** 60947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Add a network through the WifiManager API. Verifies that the network was actually added. 61947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 62947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <p>Side effects: 63947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <ul> 64947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <li>Network with SSID {@link WifiTest#NETWORK_SSID} is created.</li> 65947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * </ul> 66947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 67947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee public void testAddWifiNetwork() throws Exception { 68947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee Intent intent = new Intent(ACTION_CREATE_WIFI_CONFIG); 69947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee intent.putExtra(EXTRA_SSID, NETWORK_SSID); 70947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 71947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee getContext().startActivity(intent); 72947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 73947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee // Wait for configuration to appear in networks list. 74947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ true)); 75947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 76947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 77947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee /** 78947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Remove any network through the WifiManager API with a certain SSID. Verifies that the network 79947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * was actually removed. 80947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 81947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <p>Side effects: 82947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <ul> 83947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <li>If a network with SSID {@link WifiTest#NETWORK_SSID} exists, it will be deleted.</li> 84947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * </ul> 85947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 86947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee public void testRemoveWifiNetworkIfExists() throws Exception { 87947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee WifiConfiguration config = getNetworkForSsid(NETWORK_SSID); 88947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 89947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee if (config != null && config.networkId != -1) { 90947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee Intent intent = new Intent(ACTION_REMOVE_WIFI_CONFIG); 91947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee intent.putExtra(EXTRA_NETID, config.networkId); 92947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 93947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee getContext().startActivity(intent); 94947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 95947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 96947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ false)); 97947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 98947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 99947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee /** 100947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Verify that no network exists with a certain SSID. 101947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 102947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * <p>The SSID that will be checked for is {@link WifiTest#NETWORK_SSID}. 103947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 104947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee public void testWifiNetworkDoesNotExist() throws Exception { 105947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ false)); 106947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 107947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 108947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee /** 109947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Block until a network configuration with a certain SSID either exists or ceases to. 110947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Wait for up to {@link WifiTest#UPDATE_TIMEOUT_MS} milliseconds, in increments of 111947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * {@link WifiTest#UPDATE_INTERVAL_MS}. 112947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 113947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private boolean awaitNetworkState(String ssid, boolean exists) { 114947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee for (int probes = 0; probes * UPDATE_INTERVAL_MS <= UPDATE_TIMEOUT_MS; probes++) { 115947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee if (probes != 0) { 116947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee SystemClock.sleep(UPDATE_INTERVAL_MS); 117947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 118947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee if ((getNetworkForSsid(ssid) != null) == exists) { 119947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee return true; 120947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 121947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 122947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee return false; 123947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 124947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee 125947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee /** 126947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * Internal method to find an existing {@link WifiConfiguration} with the given SSID. 127947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * 128947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * @return A {@link WifiConfiguration} matching the specification, or {@code null} if no such 129947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee * configuration exists. 130947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee */ 131947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee private WifiConfiguration getNetworkForSsid(String ssid) { 132947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee if (!ssid.startsWith("\"")) { 133947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee ssid = '"' + ssid + '"'; 134947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 135947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) { 136947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee if (ssid.equals(config.SSID)) { 137947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee return config; 138947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 139947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 140947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee return null; 141947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee } 142947ffe324e7f39a0aa0000f07921d3483cf5bd1eRobin Lee} 143