Merge "fixes for connectivity manager test suite" into lmp-dev

This commit is contained in:
Guang Zhu
2014-08-21 00:34:11 +00:00
committed by Android (Google) Code Review
11 changed files with 603 additions and 1172 deletions

View File

@@ -66,7 +66,6 @@ import java.util.List;
* networkprefixlength.
*/
public class AccessPointParserHelper {
private static final String TAG = "AccessPointParserHelper";
static final int NONE = 0;
static final int WEP = 1;
static final int PSK = 2;

View File

@@ -35,14 +35,14 @@ import junit.framework.TestSuite;
*/
public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunner {
public int mSoftapIterations = 100;
public int mScanIterations = 100;
public int mReconnectIterations = 100;
private int mSoftApIterations = 100;
private int mScanIterations = 100;
private int mReconnectIterations = 100;
// sleep time before restart wifi, default is set to 2 minutes
public int mSleepTime = 2 * 60 * 1000;
public String mReconnectSsid = "securenetdhcp";
public String mReconnectPassword = "androidwifi";
public boolean mWifiOnlyFlag = false;
private long mSleepTime = 2 * 60 * 1000;
private String mReconnectSsid = null;
private String mReconnectPassword = null;
private boolean mWifiOnlyFlag = false;
@Override
public TestSuite getAllTests() {
@@ -60,15 +60,15 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String valueStr = (String) icicle.get("softap_iterations");
String valueStr = icicle.getString("softap_iterations");
if (valueStr != null) {
int iteration = Integer.parseInt(valueStr);
if (iteration > 0) {
mSoftapIterations = iteration;
mSoftApIterations = iteration;
}
}
String scanIterationStr = (String) icicle.get("scan_iterations");
String scanIterationStr = icicle.getString("scan_iterations");
if (scanIterationStr != null) {
int scanIteration = Integer.parseInt(scanIterationStr);
if (scanIteration > 0) {
@@ -76,17 +76,17 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
}
}
String ssidStr= (String) icicle.get("reconnect_ssid");
String ssidStr= icicle.getString("reconnect_ssid");
if (ssidStr != null) {
mReconnectSsid = ssidStr;
}
String passwordStr = (String) icicle.get("reconnect_password");
String passwordStr = icicle.getString("reconnect_password");
if (passwordStr != null) {
mReconnectPassword = passwordStr;
}
String reconnectStr = (String) icicle.get("reconnect_iterations");
String reconnectStr = icicle.getString("reconnect_iterations");
if (reconnectStr != null) {
int iteration = Integer.parseInt(reconnectStr);
if (iteration > 0) {
@@ -94,7 +94,7 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
}
}
String sleepTimeStr = (String) icicle.get("sleep_time");
String sleepTimeStr = icicle.getString("sleep_time");
if (sleepTimeStr != null) {
int sleepTime = Integer.parseInt(sleepTimeStr);
if (sleepTime > 0) {
@@ -102,9 +102,37 @@ public class ConnectivityManagerStressTestRunner extends InstrumentationTestRunn
}
}
String wifiOnlyFlag = (String) icicle.get("wifi-only");
String wifiOnlyFlag = icicle.getString("wifi-only");
if (wifiOnlyFlag != null) {
mWifiOnlyFlag = true;
}
}
public int getSoftApInterations() {
return mSoftApIterations;
}
public int getScanIterations() {
return mScanIterations;
}
public int getReconnectIterations() {
return mReconnectIterations;
}
public boolean isWifiOnly() {
return mWifiOnlyFlag;
}
public long getSleepTime() {
return mSleepTime;
}
public String getReconnectSsid() {
return mReconnectSsid;
}
public String getReconnectPassword() {
return mReconnectPassword;
}
}

View File

@@ -17,31 +17,28 @@
package com.android.connectivitymanagertest;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.os.Handler;
import android.os.Message;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.view.KeyEvent;
import com.android.internal.util.AsyncChannel;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
@@ -55,51 +52,39 @@ import java.util.List;
*/
public class ConnectivityManagerTestBase extends InstrumentationTestCase {
public static final String LOG_TAG = "ConnectivityManagerTestBase";
public static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 seconds
public static final int WIFI_SCAN_TIMEOUT = 50 * 1000; // 50 seconds
public static final int SHORT_TIMEOUT = 5 * 1000; // 5 seconds
public static final long LONG_TIMEOUT = 50 * 1000; // 50 seconds
public static final long WIFI_CONNECTION_TIMEOUT = 5 * 60 * 1000; // 5 minutes
// 2 minutes timer between wifi stop and start
public static final long WIFI_STOP_START_INTERVAL = 2 * 60 * 1000; // 2 minutes
// Set ping test timer to be 3 minutes
public static final long PING_TIMER = 3 * 60 *1000; // 3 minutes
public static final int SUCCESS = 0; // for Wifi tethering state change
public static final int FAILURE = 1;
public static final int INIT = -1;
private static final String LOG_TAG = "ConnectivityManagerTestBase";
private static final String ACCESS_POINT_FILE = "accesspoints.xml";
public ConnectivityReceiver mConnectivityReceiver = null;
public WifiReceiver mWifiReceiver = null;
private static final String PING_IP_ADDR = "8.8.8.8";
protected static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 seconds
protected static final int WIFI_SCAN_TIMEOUT = 50 * 1000; // 50 seconds
protected static final int SHORT_TIMEOUT = 5 * 1000; // 5 seconds
protected static final long LONG_TIMEOUT = 50 * 1000; // 50 seconds
protected static final long WIFI_CONNECTION_TIMEOUT = 5 * 60 * 1000; // 5 minutes
// 2 minutes timer between wifi stop and start
protected static final long WIFI_STOP_START_INTERVAL = 2 * 60 * 1000; // 2 minutes
// Set ping test timer to be 3 minutes
protected static final long PING_TIMER = 3 * 60 *1000; // 3 minutes
protected static final int SUCCESS = 0; // for Wifi tethering state change
protected static final int FAILURE = 1;
protected static final int INIT = -1;
private ConnectivityReceiver mConnectivityReceiver = null;
private WifiReceiver mWifiReceiver = null;
private AccessPointParserHelper mParseHelper = null;
/*
* Track network connectivity information
*/
public State mState;
public NetworkInfo mNetworkInfo;
public NetworkInfo mOtherNetworkInfo;
public boolean mIsFailOver;
public String mReason;
public boolean mScanResultIsAvailable = false;
public ConnectivityManager mCM;
public Object wifiObject = new Object();
public Object connectivityObject = new Object();
public int mWifiState;
public NetworkInfo mWifiNetworkInfo;
public String mBssid;
public String mPowerSsid = "opennet"; //Default power SSID
private long mLastConnectivityChangeTime = -1;
protected ConnectivityManager mCm;
private Context mContext;
public boolean scanResultAvailable = false;
protected List<ScanResult> mLastScanResult;
protected Object mWifiScanResultLock = new Object();
/* Control Wifi States */
public WifiManager mWifiManager;
/* Verify connectivity state */
public static final int NUM_NETWORK_TYPES = ConnectivityManager.MAX_NETWORK_TYPE + 1;
NetworkState[] connectivityState = new NetworkState[NUM_NETWORK_TYPES];
// For wifi tethering tests
private String[] mWifiRegexs;
public int mWifiTetherResult = INIT; // -1 is initialization state
protected long getLastConnectivityChangeTime() {
return mLastConnectivityChangeTime;
}
/**
* A wrapper of a broadcast receiver which provides network connectivity information
@@ -108,40 +93,12 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
private class ConnectivityReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
log("ConnectivityReceiver: onReceive() is called with " + intent);
mLastConnectivityChangeTime = SystemClock.uptimeMillis();
log("ConnectivityReceiver: " + intent);
String action = intent.getAction();
if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.v("ConnectivityReceiver", "onReceive() called with " + intent);
return;
}
boolean noConnectivity =
intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
if (noConnectivity) {
mState = State.DISCONNECTED;
} else {
mState = State.CONNECTED;
}
mNetworkInfo = (NetworkInfo)
intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
mOtherNetworkInfo = (NetworkInfo)
intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
mIsFailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
log("mNetworkInfo: " + mNetworkInfo.toString());
if (mOtherNetworkInfo != null) {
log("mOtherNetworkInfo: " + mOtherNetworkInfo.toString());
}
recordNetworkState(mNetworkInfo.getType(), mNetworkInfo.getState());
if (mOtherNetworkInfo != null) {
recordNetworkState(mOtherNetworkInfo.getType(), mOtherNetworkInfo.getState());
}
notifyNetworkConnectivityChange();
}
}
@@ -152,62 +109,21 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
Log.v("WifiReceiver", "onReceive() is calleld with " + intent);
if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
log("scan results are available");
notifyScanResult();
} else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
mWifiNetworkInfo =
(NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
log("mWifiNetworkInfo: " + mWifiNetworkInfo.toString());
if (mWifiNetworkInfo.getState() == State.CONNECTED) {
mBssid = intent.getStringExtra(WifiManager.EXTRA_BSSID);
synchronized (mWifiScanResultLock) {
mLastScanResult = mWifiManager.getScanResults();
mWifiScanResultLock.notifyAll();
}
notifyWifiState();
} else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);
notifyWifiState();
} else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
notifyWifiAPState();
} else if (action.equals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)) {
ArrayList<String> available = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_AVAILABLE_TETHER);
ArrayList<String> active = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ACTIVE_TETHER);
ArrayList<String> errored = intent.getStringArrayListExtra(
ConnectivityManager.EXTRA_ERRORED_TETHER);
updateTetherState(available.toArray(), active.toArray(), errored.toArray());
}
else {
return;
}
}
}
private class WifiServiceHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED:
if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) {
//AsyncChannel in msg.obj
} else {
log("Failed to establish AsyncChannel connection");
}
break;
default:
//Ignore
break;
}
}
}
@Override
public void setUp() throws Exception {
mState = State.UNKNOWN;
scanResultAvailable = false;
protected void setUp() throws Exception {
mLastScanResult = null;
mContext = getInstrumentation().getContext();
// Get an instance of ConnectivityManager
mCM = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
// Get an instance of WifiManager
mWifiManager =(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
@@ -217,8 +133,6 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
log("Disable soft ap");
}
initializeNetworkStates();
// register a connectivity receiver for CONNECTIVITY_ACTION;
mConnectivityReceiver = new ConnectivityReceiver();
mContext.registerReceiver(mConnectivityReceiver,
@@ -236,211 +150,73 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
log("Clear Wifi before we start the test.");
removeConfiguredNetworksAndDisableWifi();
mWifiRegexs = mCM.getTetherableWifiRegexs();
}
public List<WifiConfiguration> loadNetworkConfigurations() throws Exception {
protected List<WifiConfiguration> loadNetworkConfigurations() throws Exception {
InputStream in = mContext.getAssets().open(ACCESS_POINT_FILE);
mParseHelper = new AccessPointParserHelper(in);
return mParseHelper.getNetworkConfigurations();
}
// for each network type, initialize network states to UNKNOWN, and no verification flag is set
public void initializeNetworkStates() {
for (int networkType = NUM_NETWORK_TYPES - 1; networkType >=0; networkType--) {
connectivityState[networkType] = new NetworkState();
log("Initialize network state for " + networkType + ": " +
connectivityState[networkType].toString());
}
}
// deposit a network state
public void recordNetworkState(int networkType, State networkState) {
log("record network state for network " + networkType +
", state is " + networkState);
if (connectivityState == null) {
log("ConnectivityState is null");
}
if (connectivityState[networkType] == null) {
log("connectivityState[networkType] is null");
}
connectivityState[networkType].recordState(networkState);
}
// set the state transition criteria
public void setStateTransitionCriteria(int networkType, State initState,
int transitionDir, State targetState) {
connectivityState[networkType].setStateTransitionCriteria(
initState, transitionDir, targetState);
}
// Validate the states recorded
public boolean validateNetworkStates(int networkType) {
log("validate network state for " + networkType + ": ");
return connectivityState[networkType].validateStateTransition();
}
// return result from network state validation
public String getTransitionFailureReason(int networkType) {
log("get network state transition failure reason for " + networkType + ": " +
connectivityState[networkType].toString());
return connectivityState[networkType].getReason();
}
private void notifyNetworkConnectivityChange() {
synchronized(connectivityObject) {
log("notify network connectivity changed");
connectivityObject.notifyAll();
}
}
private void notifyScanResult() {
synchronized (this) {
log("notify that scan results are available");
scanResultAvailable = true;
this.notify();
}
}
private void notifyWifiState() {
synchronized (wifiObject) {
log("notify wifi state changed");
wifiObject.notify();
}
}
private void notifyWifiAPState() {
synchronized (this) {
log("notify wifi AP state changed");
this.notify();
}
}
// Update wifi tethering state
private void updateTetherState(Object[] available, Object[] tethered, Object[] errored) {
boolean wifiTethered = false;
boolean wifiErrored = false;
synchronized (this) {
for (Object obj: tethered) {
String str = (String)obj;
for (String tethRex: mWifiRegexs) {
log("str: " + str +"tethRex: " + tethRex);
if (str.matches(tethRex)) {
wifiTethered = true;
}
}
}
for (Object obj: errored) {
String str = (String)obj;
for (String tethRex: mWifiRegexs) {
log("error: str: " + str +"tethRex: " + tethRex);
if (str.matches(tethRex)) {
wifiErrored = true;
}
}
}
if (wifiTethered) {
mWifiTetherResult = SUCCESS; // wifi tethering is successful
} else if (wifiErrored) {
mWifiTetherResult = FAILURE; // wifi tethering failed
}
log("mWifiTetherResult: " + mWifiTetherResult);
this.notify();
}
}
// Wait for network connectivity state: CONNECTING, CONNECTED, SUSPENDED,
// DISCONNECTING, DISCONNECTED, UNKNOWN
public boolean waitForNetworkState(int networkType, State expectedState, long timeout) {
long startTime = System.currentTimeMillis();
// wait for network connectivity state: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING,
// DISCONNECTED, UNKNOWN
protected boolean waitForNetworkState(int networkType, State expectedState, long timeout) {
long startTime = SystemClock.uptimeMillis();
while (true) {
if ((System.currentTimeMillis() - startTime) > timeout) {
log("waitForNetworkState time out, the state of network type " + networkType +
" is: " + mCM.getNetworkInfo(networkType).getState());
if (mCM.getNetworkInfo(networkType).getState() != expectedState) {
return false;
} else {
// the broadcast has been sent out. the state has been changed.
log("networktype: " + networkType + " state: " +
mCM.getNetworkInfo(networkType));
return true;
}
}
log("Wait for the connectivity state for network: " + networkType +
" to be " + expectedState.toString());
synchronized (connectivityObject) {
try {
connectivityObject.wait(SHORT_TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ((mNetworkInfo.getType() != networkType) ||
(mNetworkInfo.getState() != expectedState)) {
log("network state for " + mNetworkInfo.getType() +
"is: " + mNetworkInfo.getState());
continue;
}
NetworkInfo ni = mCm.getNetworkInfo(networkType);
String niString = ni == null ? "null" : ni.toString();
if (ni != null && expectedState.equals(ni.getState())) {
log("waitForNetworkState success: " + niString);
return true;
}
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
log("waitForNetworkState timeout: " + niString);
return false;
}
log("waitForNetworkState interim: " + niString);
SystemClock.sleep(SHORT_TIMEOUT);
}
}
// Wait for Wifi state: WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED,
// wait for Wifi state: WIFI_STATE_DISABLED, WIFI_STATE_DISABLING, WIFI_STATE_ENABLED,
// WIFI_STATE_ENALBING, WIFI_STATE_UNKNOWN
public boolean waitForWifiState(int expectedState, long timeout) {
long startTime = System.currentTimeMillis();
protected boolean waitForWifiState(int expectedState, long timeout) {
long startTime = SystemClock.uptimeMillis();
while (true) {
if ((System.currentTimeMillis() - startTime) > timeout) {
if (mWifiState != expectedState) {
return false;
} else {
return true;
}
}
log("Wait for wifi state to be: " + expectedState);
synchronized (wifiObject) {
try {
wifiObject.wait(SHORT_TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mWifiState != expectedState) {
log("Wifi state is: " + mWifiState);
continue;
}
int state = mWifiManager.getWifiState();
if (state == expectedState) {
log("waitForWifiState success: state=" + state);
return true;
}
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
log(String.format("waitForWifiState timeout: expected=%d, actual=%d",
expectedState, state));
return false;
}
log(String.format("waitForWifiState interim: expected=%d, actual=%d",
expectedState, state));
SystemClock.sleep(SHORT_TIMEOUT);
}
}
// Wait for Wifi AP state: WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_DISABLING,
// WIFI_AP_STATE_ENABLED, WIFI_STATE_ENALBING, WIFI_STATE_UNKNOWN
public boolean waitForWifiAPState(int expectedState, long timeout) {
long startTime = System.currentTimeMillis();
protected boolean waitForWifiApState(int expectedState, long timeout) {
long startTime = SystemClock.uptimeMillis();
while (true) {
if ((System.currentTimeMillis() - startTime) > timeout) {
if (mWifiManager.getWifiApState() != expectedState) {
return false;
} else {
return true;
}
}
log("Wait for wifi AP state to be: " + expectedState);
synchronized (wifiObject) {
try {
wifiObject.wait(SHORT_TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mWifiManager.getWifiApState() != expectedState) {
log("Wifi state is: " + mWifiManager.getWifiApState());
continue;
}
int state = mWifiManager.getWifiApState();
if (state == expectedState) {
log("waitForWifiAPState success: state=" + state);
return true;
}
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
log(String.format("waitForWifiAPState timeout: expected=%d, actual=%d",
expectedState, state));
return false;
}
log(String.format("waitForWifiAPState interim: expected=%d, actual=%d",
expectedState, state));
SystemClock.sleep(SHORT_TIMEOUT);
}
}
@@ -450,44 +226,49 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
* @return SUCCESS if tethering result is successful
* FAILURE if tethering result returns error.
*/
public int waitForTetherStateChange(long timeout) {
long startTime = System.currentTimeMillis();
protected boolean waitForTetherStateChange(long timeout) {
long startTime = SystemClock.uptimeMillis();
String[] wifiRegexes = mCm.getTetherableWifiRegexs();
while (true) {
if ((System.currentTimeMillis() - startTime) > timeout) {
return mWifiTetherResult;
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
return false;
}
log("Wait for wifi tethering result.");
synchronized (this) {
try {
this.wait(SHORT_TIMEOUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mWifiTetherResult == INIT ) {
continue;
} else {
return mWifiTetherResult;
String[] active = mCm.getTetheredIfaces();
String[] error = mCm.getTetheringErroredIfaces();
for (String iface: active) {
for (String regex: wifiRegexes) {
if (iface.matches(regex)) {
return true;
}
}
}
for (String iface: error) {
for (String regex: wifiRegexes) {
if (iface.matches(regex)) {
return false;
}
}
}
SystemClock.sleep(SHORT_TIMEOUT);
}
}
// Return true if device is currently connected to mobile network
public boolean isConnectedToMobile() {
return (mNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE);
protected boolean isConnectedToMobile() {
return (mCm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_MOBILE);
}
// Return true if device is currently connected to Wifi
public boolean isConnectedToWifi() {
return (mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI);
protected boolean isConnectedToWifi() {
return (mCm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI);
}
public boolean enableWifi() {
protected boolean enableWifi() {
return mWifiManager.setWifiEnabled(true);
}
// Turn screen off
public void turnScreenOff() {
protected void turnScreenOff() {
log("Turn screen off");
PowerManager pm =
(PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
@@ -495,7 +276,7 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
}
// Turn screen on
public void turnScreenOn() {
protected void turnScreenOn() {
log("Turn screen on");
PowerManager pm =
(PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
@@ -511,7 +292,7 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
* @param pingServerList a list of servers that can be used for ping test, can be null
* @return true if the ping test is successful, false otherwise.
*/
public boolean pingTest(String[] pingServerList) {
protected boolean pingTest(String[] pingServerList) {
String[] hostList = {"www.google.com", "www.yahoo.com",
"www.bing.com", "www.facebook.com", "www.ask.com"};
if (pingServerList != null) {
@@ -549,7 +330,7 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
* If the device is already associated with a WiFi, disconnect and forget it,
* We don't verify whether the connection is successful or not, leave this to the test
*/
public boolean connectToWifi(String knownSSID) {
protected boolean connectToWifi(String knownSSID) {
WifiConfiguration config = new WifiConfiguration();
config.SSID = knownSSID;
config.allowedKeyManagement.set(KeyMgmt.NONE);
@@ -562,7 +343,7 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
* @param config
* @return
*/
public boolean connectToWifiWithConfiguration(WifiConfiguration config) {
protected boolean connectToWifiWithConfiguration(WifiConfiguration config) {
String ssid = config.SSID;
config.SSID = convertToQuotedString(ssid);
@@ -571,7 +352,7 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
log("Wifi is not enabled, enable it");
mWifiManager.setWifiEnabled(true);
// wait for the wifi state change before start scanning.
if (!waitForWifiState(WifiManager.WIFI_STATE_ENABLED, 2*SHORT_TIMEOUT)) {
if (!waitForWifiState(WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT)) {
log("wait for WIFI_STATE_ENABLED failed");
return false;
}
@@ -592,12 +373,12 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
/*
* Disconnect from the current AP and remove configured networks.
*/
public boolean disconnectAP() {
protected boolean disconnectAP() {
// remove saved networks
if (!mWifiManager.isWifiEnabled()) {
log("Enabled wifi before remove configured networks");
mWifiManager.setWifiEnabled(true);
sleep(SHORT_TIMEOUT);
SystemClock.sleep(SHORT_TIMEOUT);
}
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
@@ -623,37 +404,81 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
* Disable Wifi
* @return true if Wifi is disabled successfully
*/
public boolean disableWifi() {
protected boolean disableWifi() {
return mWifiManager.setWifiEnabled(false);
}
/**
* Remove configured networks and disable wifi
*/
public boolean removeConfiguredNetworksAndDisableWifi() {
protected boolean removeConfiguredNetworksAndDisableWifi() {
if (!disconnectAP()) {
return false;
}
sleep(SHORT_TIMEOUT);
SystemClock.sleep(SHORT_TIMEOUT);
if (!mWifiManager.setWifiEnabled(false)) {
return false;
}
sleep(SHORT_TIMEOUT);
SystemClock.sleep(SHORT_TIMEOUT);
return true;
}
private void sleep(long sleeptime) {
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {}
}
protected static String convertToQuotedString(String string) {
return "\"" + string + "\"";
}
protected boolean waitForActiveNetworkConnection(long timeout) {
long startTime = SystemClock.uptimeMillis();
while (true) {
NetworkInfo ni = mCm.getActiveNetworkInfo();
String niString = ni == null ? "null" : ni.toString();
if (ni != null && ni.isConnected()) {
return true;
}
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
log("waitForActiveNetworkConnection timeout: " + niString);
return false;
}
log("waitForActiveNetworkConnection interim: " + niString);
SystemClock.sleep(SHORT_TIMEOUT);
}
}
protected boolean waitUntilNoActiveNetworkConnection(long timeout) {
long startTime = SystemClock.uptimeMillis();
while (true) {
NetworkInfo ni = mCm.getActiveNetworkInfo();
if (ni == null) {
return true;
}
String niString = ni.toString();
if ((SystemClock.uptimeMillis() - startTime) > timeout) {
log("waitForActiveNetworkConnection timeout: " + niString);
return false;
}
log("waitForActiveNetworkConnection interim: " + niString);
SystemClock.sleep(SHORT_TIMEOUT);
}
}
// use ping request against Google public DNS to verify connectivity
protected boolean checkNetworkConnectivity() {
assertTrue("no active network connection", waitForActiveNetworkConnection(LONG_TIMEOUT));
try {
Process proc = Runtime.getRuntime().exec(new String[]{
"/system/bin/ping", "-W", "30", "-c", "1", PING_IP_ADDR});
int exitCode = proc.waitFor();
return exitCode == 0;
} catch (InterruptedException ie) {
Log.e(LOG_TAG, "InterruptedException while waiting for ping");
} catch (IOException ioe) {
Log.e(LOG_TAG, "IOException during ping", ioe);
}
return false;
}
@Override
public void tearDown() throws Exception{
protected void tearDown() throws Exception{
//Unregister receiver
if (mConnectivityReceiver != null) {
mContext.unregisterReceiver(mConnectivityReceiver);
@@ -667,4 +492,36 @@ public class ConnectivityManagerTestBase extends InstrumentationTestCase {
private void log(String message) {
Log.v(LOG_TAG, message);
}
/**
* Connect to the provided Wi-Fi network
* @param config is the network configuration
* @throws AssertionError if fails to associate and connect to wifi ap
*/
protected void connectToWifi(WifiConfiguration config) {
// step 1: connect to the test access point
assertTrue("failed to associate with " + config.SSID,
connectToWifiWithConfiguration(config));
// step 2: verify Wifi state and network state;
assertTrue("wifi state not connected with " + config.SSID,
waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.CONNECTED, LONG_TIMEOUT));
// step 3: verify the current connected network is the given SSID
assertNotNull("no active wifi info", mWifiManager.getConnectionInfo());
assertEquals("SSID mismatch", config.SSID, mWifiManager.getConnectionInfo().getSSID());
}
/**
* checks if the input is a hexadecimal string of given length
*
* @param input string to be checked
* @param length required length of the string
* @return
*/
protected boolean isHex(String input, int length) {
Pattern p = Pattern.compile(String.format("[0-9A-Fa-f]{%d}", length));
return p.matcher(input).matches();
}
}

View File

@@ -1,203 +0,0 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.connectivitymanagertest;
import android.net.NetworkInfo.State;
import android.util.Log;
import java.util.List;
import java.util.ArrayList;
public class NetworkState {
public static final int TO_DISCONNECTION = 0; // transition to disconnection
public static final int TO_CONNECTION = 1; // transition to connection
public static final int DO_NOTHING = -1; // no state change
private final String LOG_TAG = "NetworkState";
private List<State> mStateDepository;
private State mTransitionTarget;
private int mTransitionDirection;
private String mReason = null; // record mReason of state transition failure
public NetworkState() {
mStateDepository = new ArrayList<State>();
mTransitionDirection = DO_NOTHING;
mTransitionTarget = State.UNKNOWN;
}
public NetworkState(State currentState) {
mStateDepository = new ArrayList<State>();
mStateDepository.add(currentState);
mTransitionDirection = DO_NOTHING;
mTransitionTarget = State.UNKNOWN;
}
// Reinitialize the network state
public void resetNetworkState() {
mStateDepository.clear();
mTransitionDirection = DO_NOTHING;
mTransitionTarget = State.UNKNOWN;
}
// set the transition criteria, transitionDir could be:
// DO_NOTHING, TO_CONNECTION, TO_DISCONNECTION
public void setStateTransitionCriteria(State initState, int transitionDir, State targetState) {
if (!mStateDepository.isEmpty()) {
mStateDepository.clear();
}
mStateDepository.add(initState);
mTransitionDirection = transitionDir;
mTransitionTarget = targetState;
Log.v(LOG_TAG, "setStateTransitionCriteria: " + printStates());
}
public void recordState(State currentState) {
mStateDepository.add(currentState);
}
// Verify state transition
public boolean validateStateTransition() {
Log.v(LOG_TAG, "print state depository: " + printStates());
if (mTransitionDirection == DO_NOTHING) {
if (mStateDepository.isEmpty()) {
Log.v(LOG_TAG, "no state is recorded");
mReason = "no state is recorded.";
return false;
} else if (mStateDepository.size() > 1) {
for (int i = 0; i < mStateDepository.size(); i++) {
if (mStateDepository.get(i) != mTransitionTarget) {
Log.v(LOG_TAG, "state changed.");
mReason = "Unexpected state change";
return false;
}
}
} else if (mStateDepository.get(0) != mTransitionTarget) {
Log.v(LOG_TAG, mTransitionTarget + " is expected, but it is " +
mStateDepository.get(0));
mReason = mTransitionTarget + " is expected, but it is " + mStateDepository.get(0);
return false;
}
return true;
} else if (mTransitionDirection == TO_CONNECTION) {
Log.v(LOG_TAG, "transition to CONNECTED");
return transitToConnection();
} else {
Log.v(LOG_TAG, "transition to DISCONNECTED");
return transitToDisconnection();
}
}
/*
* Verifies state transition from CONNECTED->...-> DISCONNECTED.
*
* returns false if initial state or target state is not correct, or if there is
* any transition from DISCONNECTING/DISCONNECTED -> CONNECTED.
*/
public boolean transitToDisconnection () {
mReason = "states: " + printStates();
if (mStateDepository.get(0) != State.CONNECTED) {
mReason += " initial state should be CONNECTED, but it is " +
mStateDepository.get(0) + ".";
return false;
}
State lastState = mStateDepository.get(mStateDepository.size() - 1);
if ( lastState != mTransitionTarget) {
mReason += " the last state should be DISCONNECTED, but it is " + lastState;
return false;
}
for (int i = 1; i < mStateDepository.size() - 1; i++) {
State preState = mStateDepository.get(i-1);
State curState = mStateDepository.get(i);
if (preState == curState) {
continue;
} else if ((preState == State.CONNECTED) && ((curState == State.DISCONNECTING) ||
(curState == State.DISCONNECTED))) {
continue;
} else if ((preState == State.DISCONNECTING) && (curState == State.DISCONNECTED)) {
continue;
} else {
mReason += " Transition state from " + preState.toString() + " to " +
curState.toString() + " is not valid.";
return false;
}
}
return true;
}
/*
* Verifies state transition from DISCONNECTED->...-> CONNECTED.
*
* returns false if initial state or target state is not correct, or if there is
* any transition from CONNECED -> DISCONNECTED.
*/
public boolean transitToConnection() {
mReason = "states: " + printStates();
if (mStateDepository.get(0) != State.DISCONNECTED) {
mReason += " initial state should be DISCONNECTED, but it is " +
mStateDepository.get(0) + ".";
return false;
}
State lastState = mStateDepository.get(mStateDepository.size() - 1);
if ( lastState != mTransitionTarget) {
mReason += "The last state should be " + mTransitionTarget + ", but it is " + lastState;
return false;
}
for (int i = 1; i < mStateDepository.size(); i++) {
State preState = mStateDepository.get(i-1);
State curState = mStateDepository.get(i);
if (preState == curState) {
continue;
}
if ((preState == State.DISCONNECTED) && ((curState == State.CONNECTING) ||
(curState == State.CONNECTED))) {
continue;
} else if ((preState == State.CONNECTING) && (curState == State.CONNECTED)) {
continue;
} else {
mReason += " Transition state from " + preState.toString() + " to " +
curState.toString() + " is not valid.";
return false;
}
}
return true;
}
public List<State> getTransitionStates() {
return mStateDepository;
}
// return state failure mReason
public String getReason() {
return mReason;
}
public String printStates() {
StringBuilder stateBuilder = new StringBuilder("");
for (int i = 0; i < mStateDepository.size(); i++) {
stateBuilder.append(" ").append(mStateDepository.get(i).toString()).append("->");
}
return stateBuilder.toString();
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder(" ");
builder.append("mTransitionDirection: ").append(Integer.toString(mTransitionDirection)).
append("; ").append("states:").
append(printStates()).append("; ");
return builder.toString();
}
}

View File

@@ -57,8 +57,7 @@ public class WifiAssociationTestRunner extends InstrumentationTestRunner {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Bundle arguments = icicle;
String mFrequencyBand = arguments.getString("frequency-band");
String mFrequencyBand = icicle.getString("frequency-band");
if (mFrequencyBand != null) {
setFrequencyBand(mFrequencyBand);
}

View File

@@ -16,27 +16,23 @@
package com.android.connectivitymanagertest.functional;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiManager;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.provider.Settings;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
import com.android.connectivitymanagertest.ConnectivityManagerTestRunner;
import com.android.connectivitymanagertest.NetworkState;
public class ConnectivityManagerMobileTest extends
ConnectivityManagerTestBase {
private static final String TAG = "ConnectivityManagerMobileTest";
private String mTestAccessPoint;
private WakeLock wl;
private boolean mWifiOnlyFlag;
@Override
@@ -47,15 +43,11 @@ public class ConnectivityManagerMobileTest extends
mTestAccessPoint = mRunner.mTestSsid;
mWifiOnlyFlag = mRunner.mWifiOnlyFlag;
PowerManager pm = (PowerManager)getInstrumentation().
getContext().getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "CMWakeLock");
wl.acquire();
// Each test case will start with cellular connection
if (Settings.Global.getInt(getInstrumentation().getContext().getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON) == 1) {
log("airplane is not disabled, disable it.");
mCM.setAirplaneMode(false);
mCm.setAirplaneMode(false);
}
if (!mWifiOnlyFlag) {
@@ -72,15 +64,14 @@ public class ConnectivityManagerMobileTest extends
@Override
public void tearDown() throws Exception {
wl.release();
removeConfiguredNetworksAndDisableWifi();
mCM.setAirplaneMode(false);
mCm.setAirplaneMode(false);
super.tearDown();
}
// help function to verify 3G connection
public void verifyCellularConnection() {
NetworkInfo extraNetInfo = mCM.getActiveNetworkInfo();
NetworkInfo extraNetInfo = mCm.getActiveNetworkInfo();
assertEquals("network type is not MOBILE", ConnectivityManager.TYPE_MOBILE,
extraNetInfo.getType());
assertTrue("not connected to cellular network", extraNetInfo.isConnected());
@@ -90,431 +81,262 @@ public class ConnectivityManagerMobileTest extends
Log.v(TAG, message);
}
private void sleep(long sleeptime) {
try {
Thread.sleep(sleeptime);
} catch (InterruptedException e) {}
}
// Test case 1: Test enabling Wifi without associating with any AP, no broadcast on network
// event should be expected.
@LargeTest
public void test3GToWifiNotification() {
if (mWifiOnlyFlag) {
Log.v(TAG, this.getName() + " is excluded for wifi-only test");
Log.v(TAG, getName() + " is excluded for wifi-only test");
return;
}
// Enable Wi-Fi to avoid initial UNKNOWN state
enableWifi();
sleep(2 * SHORT_TIMEOUT);
// Wi-Fi is disabled
disableWifi();
// disable WiFi
assertTrue("failed to disable WiFi", disableWifi());
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.DISCONNECTED, LONG_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.CONNECTED, LONG_TIMEOUT));
// Wait for 10 seconds for broadcasts to be sent out
sleep(10 * 1000);
// wait for mobile
assertTrue("failed to wait for mobile connection", waitForNetworkState(
ConnectivityManager.TYPE_MOBILE, State.CONNECTED, LONG_TIMEOUT));
// As Wifi stays in DISCONNETED, Mobile statys in CONNECTED,
// the connectivity manager will not broadcast any network connectivity event for Wifi
NetworkInfo networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(), NetworkState.DO_NOTHING, State.CONNECTED);
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.DO_NOTHING, State.DISCONNECTED);
// Eanble Wifi without associating with any AP
enableWifi();
sleep(2 * SHORT_TIMEOUT);
// assert that we are indeed using mobile
NetworkInfo ni = mCm.getActiveNetworkInfo();
assertEquals("active network is not mobile", ConnectivityManager.TYPE_MOBILE, ni.getType());
// validate state and broadcast
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("the state for WIFI is changed");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue("state validation fail", false);
}
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("the state for MOBILE is changed");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue("state validation fail", false);
}
// Verify that the device is still connected to MOBILE
long timestamp = SystemClock.uptimeMillis();
// now enable WiFi
assertTrue("failed to enable WiFi", enableWifi());
// assert that WiFi state settles at disconnected since no AP should be configured
assertTrue("WiFi state is not DISCONNECTED after enabling", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
// assert that no connectivity change broadcast was sent since we enable wifi
assertTrue("connectivity has changed since wifi enable",
timestamp > getLastConnectivityChangeTime());
// verify that the device is still connected to MOBILE
verifyCellularConnection();
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
// Test case 2: test connection to a given AP
@LargeTest
public void testConnectToWifi() {
assertNotNull("SSID is null", mTestAccessPoint);
NetworkInfo networkInfo;
if (!mWifiOnlyFlag) {
//Prepare for connectivity verification
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(), NetworkState.TO_DISCONNECTION, State.DISCONNECTED);
}
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.TO_CONNECTION, State.CONNECTED);
// Enable Wifi and connect to a test access point
// assert that we are able to connect to the ap
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
// assert that WifiManager reports correct state
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
log("wifi state is enabled");
// assert that ConnectivityManager reports correct state for Wifi
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.DISCONNECTED, LONG_TIMEOUT));
}
// validate states
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("Wifi state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue(false);
}
if (!mWifiOnlyFlag) {
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("Mobile state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue(false);
}
}
// below check disbabled since we have bug in what ConnectivityManager returns
// if (!mWifiOnlyFlag) {
// // assert that ConnectivityManager reports correct state for mobile
// assertTrue("mobile not disconnected", waitForNetworkState(
// ConnectivityManager.TYPE_MOBILE, State.DISCONNECTED, LONG_TIMEOUT));
// }
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
// Test case 3: connect to Wifi with known AP
// Test case 3: connect & reconnect to Wifi with known AP
@LargeTest
public void testConnectToWifWithKnownAP() {
assertNotNull("SSID is null", mTestAccessPoint);
// Connect to mTestAccessPoint
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
// enable WiFi
assertTrue("failed to enable wifi", enableWifi());
// wait for wifi enable
assertTrue("wifi not enabled", waitForWifiState(
WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
// Connect to AP
assertTrue("failed to connect to " + mTestAccessPoint, connectToWifi(mTestAccessPoint));
// verify wifi connected as reported by ConnectivityManager
assertTrue("wifi not connected", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
sleep(SHORT_TIMEOUT);
// Disable Wifi
log("Disable Wifi");
if (!disableWifi()) {
log("disable Wifi failed");
return;
}
assertTrue("failed to disable wifi", disableWifi());
// Wait for the Wifi state to be DISABLED
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.DISCONNECTED, LONG_TIMEOUT));
assertTrue("wifi state not disabled", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
// below check disbabled since we have bug in what ConnectivityManager returns
// assertTrue("wifi not disconnected", waitForNetworkState(ConnectivityManager.TYPE_WIFI,
// State.DISCONNECTED, LONG_TIMEOUT));
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.CONNECTED, LONG_TIMEOUT));
assertTrue("mobile not connected after wifi disable", waitForNetworkState(
ConnectivityManager.TYPE_MOBILE, State.CONNECTED, LONG_TIMEOUT));
}
NetworkInfo networkInfo;
if (!mWifiOnlyFlag) {
//Prepare for connectivity state verification
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(), NetworkState.DO_NOTHING,
State.DISCONNECTED);
}
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.TO_CONNECTION, State.CONNECTED);
// wait for 2 minutes before restart wifi
sleep(WIFI_STOP_START_INTERVAL);
// Enable Wifi again
log("Enable Wifi again");
enableWifi();
// wait for 30s before restart wifi
SystemClock.sleep(LONG_TIMEOUT);
assertTrue("failed to enable wifi after disable", enableWifi());
// wait for wifi enable
assertTrue("wifi not enabled after toggle", waitForWifiState(
WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
// Wait for Wifi to be connected and mobile to be disconnected
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.DISCONNECTED, LONG_TIMEOUT));
}
// validate wifi states
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("Wifi state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue(false);
}
assertTrue("wifi not connected after toggle", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// below check disbabled since we have bug in what ConnectivityManager returns
// if (!mWifiOnlyFlag) {
// assertTrue("mobile not disconnected after wifi toggle", waitForNetworkState(
// ConnectivityManager.TYPE_MOBILE, State.DISCONNECTED, LONG_TIMEOUT));
// }
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
// Test case 4: test disconnect Wifi
// Test case 4: test disconnect and clear wifi settings
@LargeTest
public void testDisconnectWifi() {
assertNotNull("SSID is null", mTestAccessPoint);
// enable WiFi
assertTrue("failed to enable wifi", enableWifi());
// wait for wifi enable
assertTrue("wifi not enabled", waitForWifiState(
WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
// connect to Wifi
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
// Wait for a few seconds to avoid the state that both Mobile and Wifi is connected
sleep(SHORT_TIMEOUT);
NetworkInfo networkInfo;
if (!mWifiOnlyFlag) {
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(),
NetworkState.TO_CONNECTION,
State.CONNECTED);
}
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.TO_DISCONNECTION, State.DISCONNECTED);
assertTrue("wifi not connected", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// clear Wifi
removeConfiguredNetworksAndDisableWifi();
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.DISCONNECTED, LONG_TIMEOUT));
// assert that wifi has been disabled
assertTrue("wifi state not disabled", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
// assert that mobile is now connected
assertTrue("mobile not enabled", waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.CONNECTED, LONG_TIMEOUT));
}
// validate states
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("Wifi state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue(false);
}
if (!mWifiOnlyFlag) {
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("Mobile state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue(false);
}
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
}
// Test case 5: test connectivity from 3G to airplane mode, then to 3G again
// Test case 5: test connectivity with mobile->airplane mode->mobile
@LargeTest
public void testDataConnectionWith3GToAmTo3G() {
if (mWifiOnlyFlag) {
Log.v(TAG, this.getName() + " is excluded for wifi-only test");
Log.v(TAG, getName() + " is excluded for wifi-only test");
return;
}
//Prepare for state verification
NetworkInfo networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(),
NetworkState.TO_DISCONNECTION,
State.DISCONNECTED);
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
assertEquals(State.DISCONNECTED, networkInfo.getState());
// disable wifi
assertTrue("failed to disable wifi", disableWifi());
assertTrue("wifi state not disabled", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
// assert that we have mobile connection
assertTrue("no mobile connection", waitForNetworkState(
ConnectivityManager.TYPE_MOBILE, State.CONNECTED, LONG_TIMEOUT));
// Enable airplane mode
log("Enable airplane mode");
mCM.setAirplaneMode(true);
sleep(SHORT_TIMEOUT);
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
assertEquals(State.DISCONNECTED, networkInfo.getState());
// wait until mobile is turn off
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.DISCONNECTED, LONG_TIMEOUT));
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("Mobile state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue(false);
}
// reset state recorder
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(),
NetworkState.TO_CONNECTION,
State.CONNECTED);
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.DO_NOTHING, State.DISCONNECTED);
// enable airplane mode
mCm.setAirplaneMode(true);
// assert no active network connection after airplane mode enabled
assertTrue("still has active network connection",
waitUntilNoActiveNetworkConnection(LONG_TIMEOUT));
// disable airplane mode
mCM.setAirplaneMode(false);
mCm.setAirplaneMode(false);
// assert there is active network connection after airplane mode disabled
assertTrue("no active network connection after airplane mode disable",
waitForActiveNetworkConnection(LONG_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.CONNECTED, LONG_TIMEOUT));
// Validate the state transition
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("Mobile state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue(false);
}
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("Wifi state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue(false);
}
// assert that we have mobile connection
assertTrue("no mobile connection", waitForNetworkState(
ConnectivityManager.TYPE_MOBILE, State.CONNECTED, LONG_TIMEOUT));
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
// Test case 6: test connectivity with airplane mode Wifi connected
// Test case 6: test connectivity with airplane mode on but wifi enabled
@LargeTest
public void testDataConnectionOverAMWithWifi() {
if (mWifiOnlyFlag) {
Log.v(TAG, this.getName() + " is excluded for wifi-only test");
return;
}
assertNotNull("SSID is null", mTestAccessPoint);
// Eanble airplane mode
log("Enable airplane mode");
mCM.setAirplaneMode(true);
// enable airplane mode
mCm.setAirplaneMode(true);
// assert there is active network connection after airplane mode disabled
assertTrue("still has active network connection",
waitUntilNoActiveNetworkConnection(LONG_TIMEOUT));
NetworkInfo networkInfo;
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.DISCONNECTED, LONG_TIMEOUT));
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
setStateTransitionCriteria(ConnectivityManager.TYPE_MOBILE,
networkInfo.getState(),
NetworkState.DO_NOTHING,
State.DISCONNECTED);
}
networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI, networkInfo.getState(),
NetworkState.TO_CONNECTION, State.CONNECTED);
// Connect to Wifi
// connect to Wifi
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
// verify that connection actually works
assertTrue("no network connectivity after wifi enable", checkNetworkConnectivity());
// validate state and broadcast
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("state validate for Wifi failed");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue("State validation failed", false);
}
if (!mWifiOnlyFlag) {
if (!validateNetworkStates(ConnectivityManager.TYPE_MOBILE)) {
log("state validation for Mobile failed");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_MOBILE));
assertTrue("state validation failed", false);
}
}
mCM.setAirplaneMode(false);
// disable airplane mode
mCm.setAirplaneMode(false);
}
// Test case 7: test connectivity while transit from Wifi->AM->Wifi
@LargeTest
public void testDataConnectionWithWifiToAMToWifi () {
if (mWifiOnlyFlag) {
Log.v(TAG, this.getName() + " is excluded for wifi-only test");
return;
}
// Connect to mTestAccessPoint
// connect to mTestAccessPoint
assertNotNull("SSID is null", mTestAccessPoint);
// Connect to Wifi
// enable WiFi
assertTrue("failed to enable wifi", enableWifi());
// wait for wifi enable
assertTrue("wifi not enabled", waitForWifiState(
WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
// connect to Wifi
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
try {
Thread.sleep(SHORT_TIMEOUT);
} catch (Exception e) {
log("exception: " + e.toString());
}
// enable airplane mode without clearing Wifi
mCm.setAirplaneMode(true);
// assert there is active network connection after airplane mode disabled
assertTrue("still has active network connection",
waitUntilNoActiveNetworkConnection(LONG_TIMEOUT));
// Enable airplane mode without clearing Wifi
mCM.setAirplaneMode(true);
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.DISCONNECTED, LONG_TIMEOUT));
try {
Thread.sleep(SHORT_TIMEOUT);
} catch (Exception e) {
log("exception: " + e.toString());
}
// Prepare for state validation
NetworkInfo networkInfo = mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
assertEquals(State.DISCONNECTED, networkInfo.getState());
setStateTransitionCriteria(ConnectivityManager.TYPE_WIFI,
networkInfo.getState(), NetworkState.TO_CONNECTION, State.CONNECTED);
// Disable airplane mode
mCM.setAirplaneMode(false);
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
if (!mWifiOnlyFlag) {
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_MOBILE,
State.DISCONNECTED, LONG_TIMEOUT));
}
// validate the state transition
if (!validateNetworkStates(ConnectivityManager.TYPE_WIFI)) {
log("Wifi state transition validation failed.");
log("reason: " +
getTransitionFailureReason(ConnectivityManager.TYPE_WIFI));
assertTrue(false);
}
// disable airplane mode
mCm.setAirplaneMode(false);
// assert there is active network connection after airplane mode disabled
assertTrue("no active network connection after airplane mode disable",
waitForActiveNetworkConnection(LONG_TIMEOUT));
// assert that we have a Wifi connection
assertTrue("wifi not connected after airplane mode disable", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
// Test case 8: test wifi state change while connecting/disconnecting to/from an AP
@LargeTest
public void testWifiStateChange () {
assertNotNull("SSID is null", mTestAccessPoint);
//Connect to mTestAccessPoint
// enable WiFi
assertTrue("failed to enable wifi", enableWifi());
// wait for wifi enable
assertTrue("wifi not enabled", waitForWifiState(
WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
// connect to Wifi
assertTrue("failed to connect to " + mTestAccessPoint,
connectToWifi(mTestAccessPoint));
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_ENABLED, LONG_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
assertNotNull("Not associated with any AP",
mWifiManager.getConnectionInfo().getBSSID());
assertNotNull("not associated with any AP", mWifiManager.getConnectionInfo().getBSSID());
try {
Thread.sleep(SHORT_TIMEOUT);
} catch (Exception e) {
log("exception: " + e.toString());
}
// Disconnect from the current AP
log("disconnect from the AP");
if (!disconnectAP()) {
log("failed to disconnect from " + mTestAccessPoint);
}
// disconnect from the current AP
assertTrue("failed to disconnect from AP", disconnectAP());
// below check disbabled since we have bug in what ConnectivityManager returns
// Verify the connectivity state for Wifi is DISCONNECTED
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.DISCONNECTED, LONG_TIMEOUT));
// assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
// State.DISCONNECTED, LONG_TIMEOUT));
if (!disableWifi()) {
log("disable Wifi failed");
return;
}
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
// disable WiFi
assertTrue("failed to disable wifi", disableWifi());
assertTrue("wifi state not disabled", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
}
}

View File

@@ -16,24 +16,22 @@
package com.android.connectivitymanagertest.functional;
import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
import com.android.connectivitymanagertest.WifiAssociationTestRunner;
import android.content.Context;
import android.os.Bundle;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
import android.net.wifi.WifiConfiguration.GroupCipher;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.PairwiseCipher;
import android.net.wifi.WifiConfiguration.Protocol;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
import com.android.connectivitymanagertest.WifiAssociationTestRunner;
/**
* Test Wi-Fi connection with different configuration
* To run this tests:
@@ -41,8 +39,7 @@ import android.util.Log;
* -e security-type [OPEN|WEP64|WEP128|WPA_TKIP|WPA2_AES] -e frequency-band [2.4|5.0|auto]
* -w com.android.connectivitymanagertest/.WifiAssociationTestRunner"
*/
public class WifiAssociationTest
extends ConnectivityManagerTestBase {
public class WifiAssociationTest extends ConnectivityManagerTestBase {
private static final String TAG = "WifiAssociationTest";
private String mSsid = null;
private String mPassword = null;
@@ -55,67 +52,37 @@ public class WifiAssociationTest
}
@Override
public void setUp() throws Exception {
protected void setUp() throws Exception {
super.setUp();
WifiAssociationTestRunner mRunner = (WifiAssociationTestRunner)getInstrumentation();
Bundle arguments = mRunner.getArguments();
WifiAssociationTestRunner runner = (WifiAssociationTestRunner)getInstrumentation();
Bundle arguments = runner.getArguments();
mSecurityType = arguments.getString("security-type");
mSsid = arguments.getString("ssid");
mPassword = arguments.getString("password");
mFrequencyBand = arguments.getString("frequency-band");
mBand = mRunner.mBand;
assertNotNull("Security type is empty", mSecurityType);
assertNotNull("Ssid is empty", mSsid);
mBand = runner.mBand;
assertNotNull("security type is empty", mSecurityType);
assertNotNull("ssid is empty", mSsid);
validateFrequencyBand();
// enable Wifi and verify wpa_supplicant is started
assertTrue("enable Wifi failed", enableWifi());
sleep(2 * SHORT_TIMEOUT, "interrupted while waiting for WPA_SUPPLICANT to start");
WifiInfo mConnection = mWifiManager.getConnectionInfo();
assertNotNull(mConnection);
assertTrue("wpa_supplicant is not started ", mWifiManager.pingSupplicant());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
// enable wifi and verify wpa_supplicant is started
assertTrue("enable Wifi failed", enableWifi());
assertTrue("wifi not connected", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, LONG_TIMEOUT));
WifiInfo wi = mWifiManager.getConnectionInfo();
assertNotNull("no active wifi info", wi);
assertTrue("failed to ping wpa_supplicant ", mWifiManager.pingSupplicant());
}
private void validateFrequencyBand() {
if (mFrequencyBand != null) {
int currentFreq = mWifiManager.getFrequencyBand();
Log.v(TAG, "read frequency band: " + currentFreq);
assertTrue("device frequency band is not set successfully", (mBand == currentFreq));
assertEquals("specified frequency band does not match operational band of WifiManager",
currentFreq, mBand);
}
}
/**
* Connect to the provided Wi-Fi network
* @param config is the network configuration
* @return true if the connection is successful.
*/
private void connectToWifi(WifiConfiguration config) {
// step 1: connect to the test access point
assertTrue("failed to associate with " + config.SSID,
connectToWifiWithConfiguration(config));
// step 2: verify Wifi state and network state;
assertTrue("failed to connect with " + config.SSID,
waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// step 3: verify the current connected network is the given SSID
assertNotNull("Wifi connection returns null", mWifiManager.getConnectionInfo());
assertTrue(config.SSID.contains(mWifiManager.getConnectionInfo().getSSID()));
}
private void sleep(long sometime, String errorMsg) {
try {
Thread.sleep(sometime);
} catch (InterruptedException e) {
fail(errorMsg);
}
}
private void log(String message) {
Log.v(TAG, message);
}
@@ -133,42 +100,37 @@ public class WifiAssociationTest
config.allowedKeyManagement.set(KeyMgmt.NONE);
break;
case WEP64:
// always use hex pair for WEP-40
assertTrue("not a WEP64 security type?", mPassword.length() == 10);
assertNotNull("password is empty", mPassword);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(GroupCipher.WEP40);
if (mPassword != null) {
int length = mPassword.length();
// WEP-40
if (mPassword.matches("[0-9A-Fa-f]*")) {
// always use hex pair for WEP-40
if (isHex(mPassword, 10)) {
config.wepKeys[0] = mPassword;
} else {
fail("Please type hex pair for the password");
fail("password should be 10-character hex");
}
}
break;
case WEP128:
assertNotNull("password is empty", mPassword);
// always use hex pair for WEP-104
assertTrue("not a WEP128 security type?", mPassword.length() == 26);
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(GroupCipher.WEP104);
if (mPassword != null) {
int length = mPassword.length();
// WEP-40
if (mPassword.matches("[0-9A-Fa-f]*")) {
// always use hex pair for WEP-104
if (isHex(mPassword, 26)) {
config.wepKeys[0] = mPassword;
} else {
fail("Please type hex pair for the password");
fail("password should be 26-character hex");
}
}
break;
case WPA_TKIP:
assertNotNull("missing password", mPassword);
assertNotNull("password is empty", mPassword);
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedProtocols.set(Protocol.WPA);
@@ -181,7 +143,7 @@ public class WifiAssociationTest
}
break;
case WPA2_AES:
assertNotNull("missing password", mPassword);
assertNotNull("password is empty", mPassword);
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.allowedProtocols.set(Protocol.RSN);
@@ -200,5 +162,7 @@ public class WifiAssociationTest
}
Log.v(TAG, "network config: " + config.toString());
connectToWifi(config);
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
}
}

View File

@@ -16,21 +16,17 @@
package com.android.connectivitymanagertest.functional;
import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
import com.android.connectivitymanagertest.ConnectivityManagerTestRunner;
import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Test Wi-Fi connection with different configuration
@@ -43,28 +39,29 @@ public class WifiConnectionTest
extends ConnectivityManagerTestBase {
private static final String TAG = "WifiConnectionTest";
private static final boolean DEBUG = false;
private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
private List<WifiConfiguration> mNetworks = new ArrayList<WifiConfiguration>();
@Override
public void setUp() throws Exception {
super.setUp();
networks = loadNetworkConfigurations();
mNetworks = loadNetworkConfigurations();
if (DEBUG) {
printNetworkConfigurations();
}
// enable Wifi and verify wpa_supplicant is started
// enable wifi and verify wpa_supplicant is started
assertTrue("enable Wifi failed", enableWifi());
sleep(2 * SHORT_TIMEOUT, "interrupted while waiting for WPA_SUPPLICANT to start");
WifiInfo mConnection = mWifiManager.getConnectionInfo();
assertNotNull(mConnection);
assertTrue("wpa_supplicant is not started ", mWifiManager.pingSupplicant());
assertTrue("wifi not connected", waitForNetworkState(
ConnectivityManager.TYPE_WIFI, State.CONNECTED, LONG_TIMEOUT));
WifiInfo wi = mWifiManager.getConnectionInfo();
assertNotNull("no active wifi info", wi);
assertTrue("failed to ping wpa_supplicant ", mWifiManager.pingSupplicant());
}
private void printNetworkConfigurations() {
log("==== print network configurations parsed from XML file ====");
log("number of access points: " + networks.size());
for (WifiConfiguration config : networks) {
log("number of access points: " + mNetworks.size());
for (WifiConfiguration config : mNetworks) {
log(config.toString());
}
}
@@ -75,51 +72,25 @@ public class WifiConnectionTest
super.tearDown();
}
/**
* Connect to the provided Wi-Fi network
* @param config is the network configuration
* @return true if the connection is successful.
*/
private void connectToWifi(WifiConfiguration config) {
// step 1: connect to the test access point
assertTrue("failed to connect to " + config.SSID,
connectToWifiWithConfiguration(config));
// step 2: verify Wifi state and network state;
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// step 3: verify the current connected network is the given SSID
assertNotNull("Wifi connection returns null", mWifiManager.getConnectionInfo());
if (DEBUG) {
log("config.SSID = " + config.SSID);
log("mWifiManager.getConnectionInfo.getSSID()" +
mWifiManager.getConnectionInfo().getSSID());
}
assertTrue(config.SSID.contains(mWifiManager.getConnectionInfo().getSSID()));
}
private void sleep(long sometime, String errorMsg) {
try {
Thread.sleep(sometime);
} catch (InterruptedException e) {
fail(errorMsg);
}
}
private void log(String message) {
Log.v(TAG, message);
}
@LargeTest
public void testWifiConnections() {
for (int i = 0; i < networks.size(); i++) {
String ssid = networks.get(i).SSID;
for (int i = 0; i < mNetworks.size(); i++) {
String ssid = mNetworks.get(i).SSID;
log("-- START Wi-Fi connection test to : " + ssid + " --");
connectToWifi(networks.get(i));
// wait for 2 minutes between wifi stop and start
sleep(WIFI_STOP_START_INTERVAL, "interruped while connected to wifi");
connectToWifi(mNetworks.get(i));
// verify that connection actually works
assertTrue("no network connectivity at end of test", checkNetworkConnectivity());
log("-- END Wi-Fi connection test to " + ssid + " -- ");
log("pausing for 1 minute");
try {
Thread.sleep(60 * 1000);
} catch (InterruptedException e) {
// ignore
}
}
}
}

View File

@@ -33,7 +33,7 @@ import java.io.File;
import java.io.FileWriter;
/**
* Stress the wifi driver as access point.
* Stress test setting up device as wifi hotspot
*/
public class WifiApStress
extends ConnectivityManagerTestBase {
@@ -41,27 +41,28 @@ public class WifiApStress
private static String NETWORK_ID = "AndroidAPTest";
private static String PASSWD = "androidwifi";
private final static String OUTPUT_FILE = "WifiStressTestOutput.txt";
private int iterations;
private int mTotalIterations;
private BufferedWriter mOutputWriter = null;
private int mLastIteration = 0;
private boolean mWifiOnlyFlag;
@Override
public void setUp() throws Exception {
protected void setUp() throws Exception {
super.setUp();
ConnectivityManagerStressTestRunner mRunner =
(ConnectivityManagerStressTestRunner)getInstrumentation();
iterations = mRunner.mSoftapIterations;
mWifiOnlyFlag = mRunner.mWifiOnlyFlag;
mTotalIterations = mRunner.getSoftApInterations();
mWifiOnlyFlag = mRunner.isWifiOnly();
turnScreenOn();
}
@Override
public void tearDown() throws Exception {
protected void tearDown() throws Exception {
// write the total number of iterations into output file
mOutputWriter = new BufferedWriter(new FileWriter(new File(
Environment.getExternalStorageDirectory(), OUTPUT_FILE)));
mOutputWriter.write(String.format("iteration %d out of %d\n", mLastIteration, iterations));
mOutputWriter.write(String.format("iteration %d out of %d\n",
mLastIteration + 1, mTotalIterations));
mOutputWriter.flush();
mOutputWriter.close();
super.tearDown();
@@ -79,40 +80,44 @@ public class WifiApStress
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
config.preSharedKey = PASSWD;
// If Wifi is enabled, disable it
// if wifiap enabled, disable it
assertTrue("failed to disable wifi hotspot",
mWifiManager.setWifiApEnabled(config, false));
assertTrue("wifi hotspot not enabled", waitForWifiApState(
WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
// if Wifi is enabled, disable it
if (mWifiManager.isWifiEnabled()) {
disableWifi();
assertTrue("failed to disable wifi", disableWifi());
// wait for the wifi state to be DISABLED
assertTrue("wifi state not disabled", waitForWifiState(
WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
}
int i;
for (i = 0; i < iterations; i++) {
for (i = 0; i < mTotalIterations; i++) {
Log.v(TAG, "iteration: " + i);
mLastIteration = i;
// enable Wifi tethering
assertTrue(mWifiManager.setWifiApEnabled(config, true));
// Wait for wifi ap state to be ENABLED
assertTrue(waitForWifiAPState(WifiManager.WIFI_AP_STATE_ENABLED, 2 * LONG_TIMEOUT));
// Wait for wifi tethering result
assertEquals(SUCCESS, waitForTetherStateChange(2 * SHORT_TIMEOUT));
// Allow the wifi tethering to be enabled for 10 seconds
assertTrue("failed to enable wifi hotspot",
mWifiManager.setWifiApEnabled(config, true));
// wait for wifi ap state to be ENABLED
assertTrue("wifi hotspot not enabled", waitForWifiApState(
WifiManager.WIFI_AP_STATE_ENABLED, 2 * LONG_TIMEOUT));
// wait for wifi tethering result
assertTrue("tether state not changed", waitForTetherStateChange(LONG_TIMEOUT));
// allow the wifi tethering to be enabled for 10 seconds
try {
Thread.sleep(2 * SHORT_TIMEOUT);
} catch (Exception e) {
fail("thread in sleep is interrupted");
// ignore
}
assertTrue("no uplink data connection after Wi-Fi tethering", pingTest(null));
// Disable soft AP
assertTrue(mWifiManager.setWifiApEnabled(config, false));
// Wait for 30 seconds until Wi-Fi tethering is stopped
try {
Thread.sleep(30 * 1000);
Log.v(TAG, "wait for Wi-Fi tethering to be disabled.");
} catch (Exception e) {
fail("thread in sleep is interrupted");
}
assertFalse("Wi-Fi AP disable failed", mWifiManager.isWifiApEnabled());
}
if (i == iterations) {
mLastIteration = iterations;
// disable wifi hotspot
assertTrue("failed to disable wifi hotspot",
mWifiManager.setWifiApEnabled(config, false));
assertTrue("wifi hotspot not enabled", waitForWifiApState(
WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
assertFalse("wifi hotspot still enabled", mWifiManager.isWifiApEnabled());
}
}

View File

@@ -16,20 +16,22 @@
package com.android.connectivitymanagertest.stress;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.IpConfiguration.IpAssignment;
import android.net.IpConfiguration.ProxySettings;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.PowerManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.view.KeyEvent;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
@@ -49,15 +51,14 @@ import java.util.List;
* adb shell am instrument -e class com.android.connectivitymanagertest.stress.WifiStressTest
* -w com.android.connectivitymanagertest/.ConnectivityManagerStressTestRunner
*/
public class WifiStressTest
extends ConnectivityManagerTestBase {
public class WifiStressTest extends ConnectivityManagerTestBase {
private final static String TAG = "WifiStressTest";
private final static long SCREEN_OFF_TIMER = 500; //500ms
/**
* Wi-Fi idle time for default sleep policy
*/
private final static long WIFI_IDLE_MS = 60 * 1000;
private final static long WIFI_IDLE_MS = 15 * 1000;
/**
* Delay after issuing wifi shutdown.
@@ -69,7 +70,7 @@ public class WifiStressTest
private final static String OUTPUT_FILE = "WifiStressTestOutput.txt";
private int mReconnectIterations;
private int mWifiSleepTime;
private long mWifiSleepTime;
private int mScanIterations;
private String mSsid;
private String mPassword;
@@ -78,16 +79,16 @@ public class WifiStressTest
private boolean mWifiOnlyFlag;
@Override
public void setUp() throws Exception {
protected void setUp() throws Exception {
super.setUp();
mRunner = (ConnectivityManagerStressTestRunner) getInstrumentation();
mReconnectIterations = mRunner.mReconnectIterations;
mSsid = mRunner.mReconnectSsid;
mPassword = mRunner.mReconnectPassword;
mScanIterations = mRunner.mScanIterations;
mWifiSleepTime = mRunner.mSleepTime;
mWifiOnlyFlag = mRunner.mWifiOnlyFlag;
mReconnectIterations = mRunner.getReconnectIterations();
mSsid = mRunner.getReconnectSsid();
mPassword = mRunner.getReconnectPassword();
mScanIterations = mRunner.getScanIterations();
mWifiSleepTime = mRunner.getSleepTime();
mWifiOnlyFlag = mRunner.isWifiOnly();
log(String.format("mReconnectIterations(%d), mSsid(%s), mPassword(%s),"
+ "mScanIterations(%d), mWifiSleepTime(%d)", mReconnectIterations, mSsid,
mPassword, mScanIterations, mWifiSleepTime));
@@ -105,7 +106,7 @@ public class WifiStressTest
}
@Override
public void tearDown() throws Exception {
protected void tearDown() throws Exception {
log("tearDown()");
if (mOutputWriter != null) {
mOutputWriter.close();
@@ -145,109 +146,90 @@ public class WifiStressTest
*/
@LargeTest
public void testWifiScanning() {
int scanTimeSum = 0;
int i;
long scanTimeSum = 0, i, averageScanTime = -1;
int ssidAppearInScanResultsCount = 0; // count times of given ssid appear in scan results.
for (i = 0; i < mScanIterations; i++) {
for (i = 1; i <= mScanIterations; i++) {
log("testWifiScanning: iteration: " + i);
int averageScanTime = 0;
if (i > 0) {
averageScanTime = scanTimeSum/i;
}
writeOutput(String.format("iteration %d out of %d",
i, mScanIterations));
averageScanTime = scanTimeSum / i;
writeOutput(String.format("iteration %d out of %d", i, mScanIterations));
writeOutput(String.format("average scanning time is %d", averageScanTime));
writeOutput(String.format("ssid appear %d out of %d scan iterations",
ssidAppearInScanResultsCount, i));
long startTime = SystemClock.uptimeMillis();
scanResultAvailable = false;
assertTrue("start scan failed", mWifiManager.startScan());
while (true) {
if ((SystemClock.uptimeMillis() - startTime) >
WIFI_SCAN_TIMEOUT) {
fail("Wifi scanning takes more than " + WIFI_SCAN_TIMEOUT + " ms");
}
synchronized(this) {
try {
wait(WAIT_FOR_SCAN_RESULT);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (scanResultAvailable) {
long scanTime = (SystemClock.uptimeMillis() - startTime);
scanTimeSum += scanTime;
break;
}
List<ScanResult> scanResultLocal = null;
// wait for a scan result
long start = 0;
synchronized (mWifiScanResultLock) {
start = SystemClock.uptimeMillis();
assertTrue("start scan failed", mWifiManager.startScan());
try {
mWifiScanResultLock.wait(WAIT_FOR_SCAN_RESULT);
} catch (InterruptedException e) {
// ignore
}
scanTimeSum += SystemClock.uptimeMillis() - start;
// save the scan result while in lock
scanResultLocal = mLastScanResult;
}
if ((mWifiManager.getScanResults() == null) ||
(mWifiManager.getScanResults().size() <= 0)) {
if (scanResultLocal == null || scanResultLocal.isEmpty()) {
fail("Scan results are empty ");
}
List<ScanResult> netList = mWifiManager.getScanResults();
if (netList != null) {
log("size of scan result list: " + netList.size());
for (int s = 0; s < netList.size(); s++) {
ScanResult sr= netList.get(s);
log(String.format("scan result for %s is: %s", sr.SSID, sr.toString()));
log(String.format("signal level for %s is %d ", sr.SSID, sr.level));
if (sr.SSID.equals(mSsid)) {
ssidAppearInScanResultsCount += 1;
log("Number of times " + mSsid + " appear in the scan list: " +
ssidAppearInScanResultsCount);
break;
}
log("size of scan result list: " + scanResultLocal.size());
for (ScanResult sr : scanResultLocal) {
log(String.format("scan result: " + sr.toString()));
if (mSsid.equals(sr.SSID)) {
ssidAppearInScanResultsCount += 1;
break;
}
}
}
if (i == mScanIterations) {
writeOutput(String.format("iteration %d out of %d",
i, mScanIterations));
writeOutput(String.format("average scanning time is %d", scanTimeSum/mScanIterations));
Bundle result = new Bundle();
result.putLong("actual-iterations", i - 1);
result.putLong("avg-scan-time", averageScanTime);
result.putInt("ap-discovered", ssidAppearInScanResultsCount);
getInstrumentation().sendStatus(Activity.RESULT_FIRST_USER, result);
if (i == mScanIterations + 1) {
writeOutput(String.format("iteration %d out of %d", i, mScanIterations));
writeOutput(String.format("average scanning time is %d", scanTimeSum / (i - 1)));
writeOutput(String.format("ssid appear %d out of %d scan iterations",
ssidAppearInScanResultsCount, mScanIterations));
ssidAppearInScanResultsCount, i));
}
}
// Stress Wifi reconnection to secure net after sleep
@LargeTest
public void testWifiReconnectionAfterSleep() {
int value = Settings.Global.getInt(mRunner.getContext().getContentResolver(),
Settings.Global.WIFI_SLEEP_POLICY, -1);
log("wifi sleep policy is: " + value);
if (value != Settings.Global.WIFI_SLEEP_POLICY_DEFAULT) {
Settings.Global.putInt(mRunner.getContext().getContentResolver(),
Settings.Global.WIFI_SLEEP_POLICY, Settings.Global.WIFI_SLEEP_POLICY_DEFAULT);
log("set wifi sleep policy to default value");
}
// set wifi sleep policy to never on while in sleep
Settings.Global.putInt(mRunner.getContext().getContentResolver(),
Settings.Global.WIFI_SLEEP_POLICY, Settings.Global.WIFI_SLEEP_POLICY_NEVER);
// set idle timeout for wifi to 15s
Settings.Global.putLong(mRunner.getContext().getContentResolver(),
Settings.Global.WIFI_IDLE_MS, WIFI_IDLE_MS);
// Connect to a Wi-Fi network
WifiConfiguration config = new WifiConfiguration();
config.SSID = mSsid;
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
if (mPassword.matches("[0-9A-Fa-f]{64}")) {
config.preSharedKey = mPassword;
if (mPassword != null) {
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
if (isHex(mPassword, 64)) {
config.preSharedKey = mPassword;
} else {
config.preSharedKey = '"' + mPassword + '"';
}
} else {
config.preSharedKey = '"' + mPassword + '"';
config.allowedKeyManagement.set(KeyMgmt.NONE);
}
config.setIpAssignment(IpAssignment.DHCP);
config.setProxySettings(ProxySettings.NONE);
assertTrue("Failed to connect to Wi-Fi network: " + mSsid,
connectToWifiWithConfiguration(config));
assertTrue(waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
SHORT_TIMEOUT));
assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
assertTrue("wifi not connected", waitForNetworkState(ConnectivityManager.TYPE_WIFI,
State.CONNECTED, WIFI_CONNECTION_TIMEOUT));
// Run ping test to verify the data connection
assertTrue("Wi-Fi is connected, but no data connection.", pingTest(null));
int i;
long sum = 0;
for (i = 0; i < mReconnectIterations; i++) {
long i, sum = 0, avgReconnectTime = 0;
for (i = 1; i <= mReconnectIterations; i++) {
// 1. Put device into sleep mode
// 2. Wait for the device to sleep for sometime, verify wi-fi is off and mobile is on.
// 3. Maintain the sleep mode for some time,
@@ -261,53 +243,61 @@ public class WifiStressTest
long start = SystemClock.uptimeMillis();
PowerManager pm =
(PowerManager)mRunner.getContext().getSystemService(Context.POWER_SERVICE);
while (pm.isScreenOn() && ((SystemClock.uptimeMillis() - start) < SCREEN_OFF_TIMER)) {
sleep(100, "wait for screen off");
while (pm.isInteractive() &&
((SystemClock.uptimeMillis() - start) < SCREEN_OFF_TIMER)) {
SystemClock.sleep(100);
}
assertFalse(pm.isScreenOn());
sleep(WIFI_IDLE_MS + WIFI_SHUTDOWN_DELAY, "Interruped while wait for wifi to be idle");
assertTrue("Wait for Wi-Fi to idle timeout",
waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED,
6 * SHORT_TIMEOUT));
if (!mWifiOnlyFlag) {
assertFalse("screen still on", pm.isInteractive());
// wait for WiFi timeout
SystemClock.sleep(WIFI_IDLE_MS + WIFI_SHUTDOWN_DELAY);
// below check temporarily disabled due to bug in ConnectivityManager return
// assertTrue("Wait for Wi-Fi to idle timeout",
// waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.DISCONNECTED,
// 6 * SHORT_TIMEOUT));
if (mWifiOnlyFlag) {
assertTrue("expected wifi disconnect, still has active connection",
waitUntilNoActiveNetworkConnection(2 * LONG_TIMEOUT));
} else {
// use long timeout as the pppd startup may take several retries.
assertTrue("Wait for cellular connection timeout",
assertTrue("no fallback on mobile or wifi didn't disconnect",
waitForNetworkState(ConnectivityManager.TYPE_MOBILE, State.CONNECTED,
2 * LONG_TIMEOUT));
}
sleep(mWifiSleepTime, "Interrupted while device is in sleep mode");
// Verify the wi-fi is still off and data connection is on
assertEquals("Wi-Fi is reconnected", State.DISCONNECTED,
mCM.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState());
if (!mWifiOnlyFlag) {
assertEquals("Cellular connection is down", State.CONNECTED,
mCM.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState());
assertTrue("Mobile is connected, but no data connection.", pingTest(null));
SystemClock.sleep(mWifiSleepTime);
// verify the wi-fi is still off and either we have no connectivity or fallback on mobile
if (mWifiOnlyFlag) {
NetworkInfo ni = mCm.getActiveNetworkInfo();
if (ni != null) {
Log.e(TAG, "has active network while in wifi sleep: " + ni.toString());
fail("active network detected");
}
} else {
assertEquals("mobile not connected", State.CONNECTED,
mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState());
assertTrue("no connectivity over mobile", pingTest(null));
}
// Turn screen on again
turnScreenOn();
// Wait for 2 seconds for the lock screen
sleep(2 * 1000, "wait 2 seconds for lock screen");
// Disable lock screen by inject menu key event
mRunner.sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
// Measure the time for Wi-Fi to get connected
long startTime = SystemClock.uptimeMillis();
assertTrue("Wait for Wi-Fi enable timeout after wake up",
waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
SHORT_TIMEOUT));
assertTrue("Wait for Wi-Fi connection timeout after wake up",
assertTrue("screen on: wifi not enabled before timeout",
waitForWifiState(WifiManager.WIFI_STATE_ENABLED, SHORT_TIMEOUT));
assertTrue("screen on: wifi not connected before timeout",
waitForNetworkState(ConnectivityManager.TYPE_WIFI, State.CONNECTED,
WIFI_CONNECTION_TIMEOUT));
LONG_TIMEOUT));
long connectionTime = SystemClock.uptimeMillis() - startTime;
sum += connectionTime;
log("average reconnection time is: " + sum/(i+1));
avgReconnectTime = sum / i;
log("average reconnection time is: " + avgReconnectTime);
assertTrue("Reconnect to Wi-Fi network, but no data connection.", pingTest(null));
}
if (i == mReconnectIterations) {
Bundle result = new Bundle();
result.putLong("actual-iterations", i - 1);
result.putLong("avg-reconnect-time", avgReconnectTime);
getInstrumentation().sendStatus(Activity.RESULT_FIRST_USER, result);
if (i == mReconnectIterations + 1) {
writeOutput(String.format("iteration %d out of %d",
i, mReconnectIterations));
}

View File

@@ -38,7 +38,6 @@ import java.util.List;
public class WifiClientTest extends AndroidTestCase {
private WifiManager mWifiManager;
private final String TAG = "WifiClientTest";
//10s delay for turning on wifi
private static final int DELAY = 10000;