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.managedprofile;
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.os.SystemClock;
24 import android.test.AndroidTestCase;
25 
26 import java.util.concurrent.TimeUnit;
27 
28 import static com.android.compatibility.common.util.WifiConfigCreator.ACTION_CREATE_WIFI_CONFIG;
29 import static com.android.compatibility.common.util.WifiConfigCreator.ACTION_REMOVE_WIFI_CONFIG;
30 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_NETID;
31 import static com.android.compatibility.common.util.WifiConfigCreator.EXTRA_SSID;
32 
33 /**
34  * Driven by the host-side test: com.android.cts.devicepolicy.ManagedProfileTest
35  *
36  * Each of these tests can run independently but have side-effects. The side-effects are used as
37  * building blocks to test various cleanup routines, for example that networks belonging to one
38  * user are deleted
39  */
40 public class WifiTest extends AndroidTestCase {
41     private static final String TAG = WifiTest.class.getSimpleName();
42 
43     // Unique SSID to use for this test (max SSID length is 32)
44     private static final String NETWORK_SSID = "com.android.cts.xwde7ktvh8rmjuhr";
45 
46     // Time duration to allow before assuming that a WiFi operation failed and ceasing to wait.
47     private static final long UPDATE_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(5);
48     private static final long UPDATE_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1);
49 
50     // Shared WifiManager instance.
51     private WifiManager mWifiManager;
52 
53     @Override
setUp()54     public void setUp() throws Exception {
55         super.setUp();
56         mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
57     }
58 
59     /**
60      * Add a network through the WifiManager API. Verifies that the network was actually added.
61      *
62      * <p>Side effects:
63      * <ul>
64      *   <li>Network with SSID {@link WifiTest#NETWORK_SSID} is created.</li>
65      * </ul>
66      */
testAddWifiNetwork()67     public void testAddWifiNetwork() throws Exception {
68         Intent intent = new Intent(ACTION_CREATE_WIFI_CONFIG);
69         intent.putExtra(EXTRA_SSID, NETWORK_SSID);
70         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71         getContext().startActivity(intent);
72 
73         // Wait for configuration to appear in networks list.
74         assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ true));
75     }
76 
77     /**
78      * Remove any network through the WifiManager API with a certain SSID. Verifies that the network
79      * was actually removed.
80      *
81      * <p>Side effects:
82      * <ul>
83      *   <li>If a network with SSID {@link WifiTest#NETWORK_SSID} exists, it will be deleted.</li>
84      * </ul>
85      */
testRemoveWifiNetworkIfExists()86     public void testRemoveWifiNetworkIfExists() throws Exception {
87         WifiConfiguration config = getNetworkForSsid(NETWORK_SSID);
88 
89         if (config != null && config.networkId != -1) {
90             Intent intent = new Intent(ACTION_REMOVE_WIFI_CONFIG);
91             intent.putExtra(EXTRA_NETID, config.networkId);
92             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
93             getContext().startActivity(intent);
94         }
95 
96         assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ false));
97     }
98 
99     /**
100      * Verify that no network exists with a certain SSID.
101      *
102      * <p>The SSID that will be checked for is {@link WifiTest#NETWORK_SSID}.
103      */
testWifiNetworkDoesNotExist()104     public void testWifiNetworkDoesNotExist() throws Exception {
105         assertTrue(awaitNetworkState(NETWORK_SSID, /* exists */ false));
106     }
107 
108     /**
109      * Block until a network configuration with a certain SSID either exists or ceases to.
110      * Wait for up to {@link WifiTest#UPDATE_TIMEOUT_MS} milliseconds, in increments of
111      * {@link WifiTest#UPDATE_INTERVAL_MS}.
112      */
awaitNetworkState(String ssid, boolean exists)113     private boolean awaitNetworkState(String ssid, boolean exists) {
114         for (int probes = 0; probes * UPDATE_INTERVAL_MS <= UPDATE_TIMEOUT_MS; probes++) {
115             if (probes != 0) {
116                 SystemClock.sleep(UPDATE_INTERVAL_MS);
117             }
118             if ((getNetworkForSsid(ssid) != null) == exists) {
119                 return true;
120             }
121         }
122         return false;
123     }
124 
125     /**
126      * Internal method to find an existing {@link WifiConfiguration} with the given SSID.
127      *
128      * @return A {@link WifiConfiguration} matching the specification, or {@code null} if no such
129      *         configuration exists.
130      */
getNetworkForSsid(String ssid)131     private WifiConfiguration getNetworkForSsid(String ssid) {
132         if (!ssid.startsWith("\"")) {
133             ssid = '"' + ssid + '"';
134         }
135         for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
136             if (ssid.equals(config.SSID)) {
137                 return config;
138             }
139         }
140         return null;
141     }
142 }
143