1/*
2 * Copyright (C) 2010, 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
17package com.android.connectivitymanagertest.stress;
18
19import com.android.connectivitymanagertest.ConnectivityManagerStressTestRunner;
20import com.android.connectivitymanagertest.ConnectivityManagerTestActivity;
21import com.android.connectivitymanagertest.UtilHelper;
22
23import android.content.Context;
24import android.net.ConnectivityManager;
25import android.net.NetworkInfo.State;
26import android.net.wifi.ScanResult;
27import android.net.wifi.WifiConfiguration;
28import android.net.wifi.WifiConfiguration.IpAssignment;
29import android.net.wifi.WifiConfiguration.KeyMgmt;
30import android.net.wifi.WifiConfiguration.ProxySettings;
31import android.net.wifi.WifiManager;
32import android.os.Environment;
33import android.os.PowerManager;
34import android.os.IPowerManager;
35import android.os.SystemClock;
36import android.os.ServiceManager;
37import android.provider.Settings;
38import android.test.ActivityInstrumentationTestCase2;
39import android.test.suitebuilder.annotation.LargeTest;
40
41import android.util.Log;
42
43import java.io.BufferedWriter;
44import java.io.File;
45import java.io.FileWriter;
46import java.io.IOException;
47import java.util.List;
48
49/**
50 * Stress Wi-Fi connection, scanning and reconnection after sleep.
51 *
52 * To run this stress test suite, type
53 * adb shell am instrument -e class com.android.connectivitymanagertest.stress.WifiStressTest
54 *                  -w com.android.connectivitymanagertest/.ConnectivityManagerStressTestRunner
55 */
56public class WifiStressTest
57    extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> {
58    private final static String TAG = "WifiStressTest";
59
60    /**
61     * Wi-Fi idle time for default sleep policy
62     */
63    private final static long WIFI_IDLE_MS = 60 * 1000;
64
65    /**
66     * Delay after issuing wifi shutdown.
67     * The framework keep driver up for at leat 2 minutes to avoid problems
68     * that a quick shutdown could cause on wext driver and protentially
69     * on cfg based driver
70     */
71    private final static long WIFI_SHUTDOWN_DELAY = 2 * 60 * 1000;
72
73    private final static String OUTPUT_FILE = "WifiStressTestOutput.txt";
74    private ConnectivityManagerTestActivity mAct;
75    private int mReconnectIterations;
76    private int mWifiSleepTime;
77    private int mScanIterations;
78    private String mSsid;
79    private String mPassword;
80    private ConnectivityManagerStressTestRunner mRunner;
81    private BufferedWriter mOutputWriter = null;
82
83    public WifiStressTest() {
84        super(ConnectivityManagerTestActivity.class);
85    }
86
87    @Override
88    public void setUp() throws Exception {
89        super.setUp();
90        mAct = getActivity();
91        mRunner = (ConnectivityManagerStressTestRunner) getInstrumentation();
92        mReconnectIterations = mRunner.mReconnectIterations;
93        mSsid = mRunner.mReconnectSsid;
94        mPassword = mRunner.mReconnectPassword;
95        mScanIterations = mRunner.mScanIterations;
96        mWifiSleepTime = mRunner.mSleepTime;
97        log(String.format("mReconnectIterations(%d), mSsid(%s), mPassword(%s),"
98            + "mScanIterations(%d), mWifiSleepTime(%d)", mReconnectIterations, mSsid,
99            mPassword, mScanIterations, mWifiSleepTime));
100        mOutputWriter = new BufferedWriter(new FileWriter(new File(
101                Environment.getExternalStorageDirectory(), OUTPUT_FILE), true));
102        mAct.turnScreenOn();
103        if (!mAct.mWifiManager.isWifiEnabled()) {
104            log("Enable wi-fi before stress tests.");
105            if (!mAct.enableWifi()) {
106                tearDown();
107                fail("enable wifi failed.");
108            }
109            sleep(ConnectivityManagerTestActivity.SHORT_TIMEOUT,
110                    "Interruped while waiting for wifi on");
111        }
112    }
113
114    @Override
115    public void tearDown() throws Exception {
116        log("tearDown()");
117        if (mOutputWriter != null) {
118            mOutputWriter.close();
119        }
120        super.tearDown();
121    }
122
123    private void writeOutput(String s) {
124        log("write message: " + s);
125        if (mOutputWriter == null) {
126            log("no writer attached to file " + OUTPUT_FILE);
127            return;
128        }
129        try {
130            mOutputWriter.write(s + "\n");
131            mOutputWriter.flush();
132        } catch (IOException e) {
133            log("failed to write output.");
134        }
135    }
136
137    public void log(String message) {
138        Log.v(TAG, message);
139    }
140
141    private void sleep(long sometime, String errorMsg) {
142        try {
143            Thread.sleep(sometime);
144        } catch (InterruptedException e) {
145            fail(errorMsg);
146        }
147    }
148
149    /**
150     *  Stress Wifi Scanning
151     *  TODO: test the scanning quality for each frequency band
152     */
153    @LargeTest
154    public void testWifiScanning() {
155        int scanTimeSum = 0;
156        int i;
157        int ssidAppearInScanResultsCount = 0; // count times of given ssid appear in scan results.
158        for (i = 0; i < mScanIterations; i++) {
159            log("testWifiScanning: iteration: " + i);
160            int averageScanTime = 0;
161            if (i > 0) {
162                averageScanTime = scanTimeSum/i;
163            }
164            writeOutput(String.format("iteration %d out of %d",
165                    i, mScanIterations));
166            writeOutput(String.format("average scanning time is %d", averageScanTime));
167            writeOutput(String.format("ssid appear %d out of %d scan iterations",
168                    ssidAppearInScanResultsCount, i));
169            long startTime = System.currentTimeMillis();
170            mAct.scanResultAvailable = false;
171            assertTrue("start scan failed", mAct.mWifiManager.startScanActive());
172            while (true) {
173                if ((System.currentTimeMillis() - startTime) >
174                ConnectivityManagerTestActivity.WIFI_SCAN_TIMEOUT) {
175                    fail("Wifi scanning takes more than " +
176                            ConnectivityManagerTestActivity.WIFI_SCAN_TIMEOUT + " ms");
177                }
178                synchronized(mAct) {
179                    try {
180                        mAct.wait(ConnectivityManagerTestActivity.WAIT_FOR_SCAN_RESULT);
181                    } catch (InterruptedException e) {
182                        e.printStackTrace();
183                    }
184                    if (mAct.scanResultAvailable) {
185                        long scanTime = (System.currentTimeMillis() - startTime);
186                        scanTimeSum += scanTime;
187                        break;
188                    }
189                }
190            }
191            if ((mAct.mWifiManager.getScanResults() == null) ||
192                    (mAct.mWifiManager.getScanResults().size() <= 0)) {
193                fail("Scan results are empty ");
194            }
195
196            List<ScanResult> netList = mAct.mWifiManager.getScanResults();
197            if (netList != null) {
198                log("size of scan result list: " + netList.size());
199                for (int s = 0; s < netList.size(); s++) {
200                    ScanResult sr= netList.get(s);
201                    log(String.format("scan result for %s is: %s", sr.SSID, sr.toString()));
202                    log(String.format("signal level for %s is %d ", sr.SSID, sr.level));
203                    if (sr.SSID.equals(mSsid)) {
204                        ssidAppearInScanResultsCount += 1;
205                        log("Number of times " + mSsid + " appear in the scan list: " +
206                                ssidAppearInScanResultsCount);
207                        break;
208                    }
209                }
210            }
211        }
212        if (i == mScanIterations) {
213            writeOutput(String.format("iteration %d out of %d",
214                    i, mScanIterations));
215            writeOutput(String.format("average scanning time is %d", scanTimeSum/mScanIterations));
216            writeOutput(String.format("ssid appear %d out of %d scan iterations",
217                    ssidAppearInScanResultsCount, mScanIterations));
218        }
219    }
220
221    // Stress Wifi reconnection to secure net after sleep
222    @LargeTest
223    public void testWifiReconnectionAfterSleep() {
224        int value = Settings.System.getInt(mRunner.getContext().getContentResolver(),
225                Settings.System.WIFI_SLEEP_POLICY, -1);
226        if (value < 0) {
227            Settings.System.putInt(mRunner.getContext().getContentResolver(),
228                    Settings.System.WIFI_SLEEP_POLICY, Settings.System.WIFI_SLEEP_POLICY_DEFAULT);
229            log("set wifi sleep policy to default value");
230        }
231        Settings.Secure.putLong(mRunner.getContext().getContentResolver(),
232                Settings.Secure.WIFI_IDLE_MS, WIFI_IDLE_MS);
233
234        // Connect to a Wi-Fi network
235        WifiConfiguration config = new WifiConfiguration();
236        config.SSID = mSsid;
237        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
238        if (mPassword.matches("[0-9A-Fa-f]{64}")) {
239            config.preSharedKey = mPassword;
240        } else {
241            config.preSharedKey = '"' + mPassword + '"';
242        }
243        config.ipAssignment = IpAssignment.DHCP;
244        config.proxySettings = ProxySettings.NONE;
245
246        assertTrue("Failed to connect to Wi-Fi network: " + mSsid,
247                mAct.connectToWifiWithConfiguration(config));
248        assertTrue(mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
249                ConnectivityManagerTestActivity.SHORT_TIMEOUT));
250        assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
251                ConnectivityManagerTestActivity.LONG_TIMEOUT));
252        // Run ping test to verify the data connection
253        assertTrue("Wi-Fi is connected, but no data connection.", mAct.pingTest(null));
254
255        int i;
256        long sum = 0;
257        for (i = 0; i < mReconnectIterations; i++) {
258            // 1. Put device into sleep mode
259            // 2. Wait for the device to sleep for sometime, verify wi-fi is off and mobile is on.
260            // 3. Maintain the sleep mode for some time,
261            // 4. Verify the Wi-Fi is still off, and data is on
262            // 5. Wake up the device, verify Wi-Fi is enabled and connected.
263            writeOutput(String.format("iteration %d out of %d",
264                    i, mReconnectIterations));
265            log("iteration: " + i);
266            mAct.turnScreenOff();
267            PowerManager pm =
268                (PowerManager)mRunner.getContext().getSystemService(Context.POWER_SERVICE);
269            assertFalse(pm.isScreenOn());
270            sleep(WIFI_IDLE_MS + WIFI_SHUTDOWN_DELAY, "Interruped while wait for wifi to be idle");
271            assertTrue("Wait for Wi-Fi to idle timeout",
272                    mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED,
273                    6 * ConnectivityManagerTestActivity.SHORT_TIMEOUT));
274            if (!UtilHelper.isWifiOnly()) {
275                // use long timeout as the pppd startup may take several retries.
276                assertTrue("Wait for cellular connection timeout",
277                        mAct.waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED,
278                        2 * ConnectivityManagerTestActivity.LONG_TIMEOUT));
279            }
280            sleep(mWifiSleepTime, "Interrupted while device is in sleep mode");
281            // Verify the wi-fi is still off and data connection is on
282            assertEquals("Wi-Fi is reconnected", State.DISCONNECTED,
283                    mAct.mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState());
284
285            if (!UtilHelper.isWifiOnly()) {
286                assertEquals("Cellular connection is down", State.CONNECTED,
287                             mAct.mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState());
288                assertTrue("Mobile is connected, but no data connection.", mAct.pingTest(null));
289            }
290
291            // Turn screen on again
292            mAct.turnScreenOn();
293            // Measure the time for Wi-Fi to get connected
294            long startTime = System.currentTimeMillis();
295            assertTrue("Wait for Wi-Fi enable timeout after wake up",
296                    mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
297                    ConnectivityManagerTestActivity.SHORT_TIMEOUT));
298            assertTrue("Wait for Wi-Fi connection timeout after wake up",
299                    mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
300                    6 * ConnectivityManagerTestActivity.LONG_TIMEOUT));
301            long connectionTime = System.currentTimeMillis() - startTime;
302            sum += connectionTime;
303            log("average reconnection time is: " + sum/(i+1));
304
305            assertTrue("Reconnect to Wi-Fi network, but no data connection.", mAct.pingTest(null));
306        }
307        if (i == mReconnectIterations) {
308            writeOutput(String.format("iteration %d out of %d",
309                    i, mReconnectIterations));
310        }
311    }
312}
313