diff options
author | Xia Wang <xiaw@google.com> | 2010-11-22 16:59:48 -0800 |
---|---|---|
committer | Xia Wang <xiaw@google.com> | 2010-12-02 16:25:26 -0800 |
commit | a42a1e6b6fb6acc9ca327523ae456e464f8aedc1 (patch) | |
tree | abcf8ddbb4bcaebf5d141bad80efba47906515e4 /core/tests | |
parent | 47e6b1b5eef8ee99872f278f66bc498c4fcca0d8 (diff) | |
download | frameworks_base-a42a1e6b6fb6acc9ca327523ae456e464f8aedc1.zip frameworks_base-a42a1e6b6fb6acc9ca327523ae456e464f8aedc1.tar.gz frameworks_base-a42a1e6b6fb6acc9ca327523ae456e464f8aedc1.tar.bz2 |
Add Wi-Fi connection test for static IP
- Add support in access point parser to support static ip settings
- Add Wi-Fi test with static IP configuration
- Use WifiManager new APIs
- Fix issue: if the expected AP is not in the scan list, retry scanning.
Maximum is 5.
- Fix issue: wait for wifi state change before connecting to AP.
Change-Id: Id09b921c8a6a4b14f3c65cdea0b58c7e6f395520
Diffstat (limited to 'core/tests')
4 files changed, 304 insertions, 150 deletions
diff --git a/core/tests/ConnectivityManagerTest/assets/accesspoints.xml b/core/tests/ConnectivityManagerTest/assets/accesspoints.xml index 2b0e4af..5e3252c 100755 --- a/core/tests/ConnectivityManagerTest/assets/accesspoints.xml +++ b/core/tests/ConnectivityManagerTest/assets/accesspoints.xml @@ -4,7 +4,7 @@ <ssid>opennet</ssid> <security>NONE</security> </accesspoint> - <accesspoint> + <accesspoint> <ssid>GoogleGuest</ssid> <security>NONE</security> </accesspoint> @@ -14,6 +14,16 @@ <password>androidwifi</password> </accesspoint> <accesspoint> + <ssid>securenetstatic</ssid> + <security>PSK</security> + <password>androidwifi</password> + <ip>192.168.14.2</ip> + <gateway>192.168.14.1</gateway> + <networkprefixlength>24</networkprefixlength> + <dns1>192.168.14.1</dns1> + <dns2>192.168.1.9</dns2> + </accesspoint> + <accesspoint> <ssid>botnet</ssid> <security>EAP</security> <eap>PEAP</eap> diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java index 863fbe6..21f1bfc 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java @@ -25,11 +25,18 @@ import org.xml.sax.helpers.DefaultHandler; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.AuthAlgorithm; +import android.net.wifi.WifiConfiguration.IpAssignment; import android.net.wifi.WifiConfiguration.KeyMgmt; - +import android.net.wifi.WifiConfiguration.ProxySettings; +import android.net.LinkAddress; +import android.net.LinkProperties; import android.util.Log; + import java.io.InputStream; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; @@ -38,7 +45,8 @@ import java.util.List; * The configurations of an access point is included in tag * <accesspoint></accesspoint>. The supported configuration includes: ssid, * security, eap, phase2, identity, password, anonymousidentity, cacert, usercert, - * in which each is included in the corresponding tags. All access points have to be + * in which each is included in the corresponding tags. Static IP setting is also supported. + * Tags that can be used include: ip, gateway, netmask, dns1, dns2. All access points have to be * enclosed in tags of <resources></resources>. * * The following is a sample configuration file for an access point using EAP-PEAP with MSCHAP2. @@ -52,6 +60,9 @@ import java.util.List; * <password>abcdefgh</password> * </accesspoint> * </resources> + * + * Note:ssid and security have to be the first two tags + * for static ip setting, tag "ip" should be listed before other fields: dns, gateway, netmask. */ public class AccessPointParserHelper { private static final String KEYSTORE_SPACE = "keystore://"; @@ -93,9 +104,11 @@ public class AccessPointParserHelper { boolean security = false; boolean password = false; boolean ip = false; - boolean subnetmask = false; boolean gateway = false; - boolean dns = false; + boolean networkprefix = false; + boolean netmask = false; + boolean dns1 = false; + boolean dns2 = false; boolean eap = false; boolean phase2 = false; boolean identity = false; @@ -104,6 +117,8 @@ public class AccessPointParserHelper { boolean usercert = false; WifiConfiguration config = null; int securityType = NONE; + LinkProperties mLinkProperties = null; + InetAddress mInetAddr = null; @Override public void startElement(String uri, String localName, String tagName, @@ -138,12 +153,37 @@ public class AccessPointParserHelper { if (tagName.equalsIgnoreCase("usercert")) { usercert = true; } + if (tagName.equalsIgnoreCase("ip")) { + mLinkProperties = new LinkProperties(); + ip = true; + } + if (tagName.equalsIgnoreCase("gateway")) { + gateway = true; + } + if (tagName.equalsIgnoreCase("networkprefixlength")) { + networkprefix = true; + } + if (tagName.equalsIgnoreCase("netmask")) { + netmask = true; + } + if (tagName.equalsIgnoreCase("dns1")) { + dns1 = true; + } + if (tagName.equalsIgnoreCase("dns2")) { + dns2 = true; + } } @Override public void endElement(String uri, String localName, String tagName) throws SAXException { - Log.v(TAG, "endElement: " + tagName); if (tagName.equalsIgnoreCase("accesspoint")) { + if (mLinkProperties != null) { + config.ipAssignment = IpAssignment.STATIC; + config.linkProperties = mLinkProperties; + } else { + config.ipAssignment = IpAssignment.DHCP; + } + config.proxySettings = ProxySettings.NONE; networks.add(config); } } @@ -152,14 +192,11 @@ public class AccessPointParserHelper { public void characters(char ch[], int start, int length) throws SAXException { if (ssid) { config.SSID = new String(ch, start, length); - Log.v(TAG, "ssid: " + config.SSID); ssid = false; } if (security) { String securityStr = (new String(ch, start, length)).toUpperCase(); - Log.v(TAG, "security: " + securityStr); securityType = getSecurityType(securityStr); - Log.v(TAG, "securityType = " + securityType); switch (securityType) { case NONE: config.allowedKeyManagement.set(KeyMgmt.NONE); @@ -175,6 +212,13 @@ public class AccessPointParserHelper { case EAP: config.allowedKeyManagement.set(KeyMgmt.WPA_EAP); config.allowedKeyManagement.set(KeyMgmt.IEEE8021X); + // Initialize other fields. + config.phase2.setValue(""); + config.ca_cert.setValue(""); + config.client_cert.setValue(""); + config.private_key.setValue(""); + config.identity.setValue(""); + config.anonymous_identity.setValue(""); break; default: throw new SAXException(); @@ -187,7 +231,6 @@ public class AccessPointParserHelper { if (len == 0) { throw new SAXException(); } - Log.v(TAG, "passwordStr:" + passwordStr); if (securityType == WEP) { if ((len == 10 || len == 26 || len == 58) && passwordStr.matches("[0-9A-Fa-f]*")) { @@ -242,21 +285,94 @@ public class AccessPointParserHelper { config.client_cert.setValue(KEYSTORE_SPACE); usercert = false; } + if (ip) { + try { + String ipAddr = new String(ch, start, length); + if (!InetAddress.isNumeric(ipAddr)) { + throw new SAXException(); + } + mInetAddr = InetAddress.getByName(ipAddr); + } catch (UnknownHostException e) { + throw new SAXException(); + } + ip = false; + } + if (gateway) { + try { + String gwAddr = new String(ch, start, length); + if (!InetAddress.isNumeric(gwAddr)) { + throw new SAXException(); + } + mLinkProperties.setGateway(InetAddress.getByName(gwAddr)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + gateway = false; + } + if (networkprefix) { + try { + int nwPrefixLength = Integer.parseInt(new String(ch, start, length)); + if ((nwPrefixLength < 0) || (nwPrefixLength > 32)) { + throw new SAXException(); + } + mLinkProperties.addLinkAddress(new LinkAddress(mInetAddr, nwPrefixLength)); + } catch (NumberFormatException e) { + throw new SAXException(); + } + networkprefix = false; + } + if (netmask) { + try { + String netMaskStr = new String(ch, start, length); + if (!InetAddress.isNumeric(netMaskStr)) { + throw new SAXException(); + } + InetAddress netMaskAddr = InetAddress.getByName(netMaskStr); + mLinkProperties.addLinkAddress(new LinkAddress(mInetAddr, netMaskAddr)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + netmask = false; + } + if (dns1) { + try { + String dnsAddr = new String(ch, start, length); + if (!InetAddress.isNumeric(dnsAddr)) { + throw new SAXException(); + } + mLinkProperties.addDns(InetAddress.getByName(dnsAddr)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + dns1 = false; + } + if (dns2) { + try { + String dnsAddr = new String(ch, start, length); + if (!InetAddress.isNumeric(dnsAddr)) { + throw new SAXException(); + } + mLinkProperties.addDns(InetAddress.getByName(dnsAddr)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + dns2 = false; + } } }; - public AccessPointParserHelper() { - } - /** - * Process the accesspoint.xml file - * @return List of WifiConfiguration - * @throws Exception when parsing the XML file + * Process the InputStream in + * @param in is the InputStream that can be used for XML parsing + * @throws Exception */ - public List<WifiConfiguration> processAccessPoint(InputStream in) throws Exception { + public AccessPointParserHelper(InputStream in) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse(in, mHandler); + } + + public List<WifiConfiguration> getNetworkConfigurations() throws Exception { return networks; } } diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java index 37b9f52..2888696 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java @@ -16,10 +16,8 @@ package com.android.connectivitymanagertest; -import com.android.connectivitymanagertest.R; import android.app.Activity; import android.content.Context; -import android.content.res.Resources; import android.content.BroadcastReceiver; import android.content.Intent; import android.content.IntentFilter; @@ -36,7 +34,6 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; -import android.net.wifi.SupplicantState; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; import android.net.wifi.WifiInfo; @@ -52,7 +49,7 @@ public class ConnectivityManagerTestActivity extends Activity { public static final String LOG_TAG = "ConnectivityManagerTestActivity"; public static final int WAIT_FOR_SCAN_RESULT = 10 * 1000; //10 seconds - public static final int WIFI_SCAN_TIMEOUT = 20 * 1000; + public static final int WIFI_SCAN_TIMEOUT = 50 * 1000; public static final int SHORT_TIMEOUT = 5 * 1000; public static final long LONG_TIMEOUT = 50 * 1000; public static final int SUCCESS = 0; // for Wifi tethering state change @@ -61,6 +58,7 @@ public class ConnectivityManagerTestActivity extends Activity { private static final String ACCESS_POINT_FILE = "accesspoints.xml"; public ConnectivityReceiver mConnectivityReceiver = null; public WifiReceiver mWifiReceiver = null; + private AccessPointParserHelper mParseHelper = null; /* * Track network connectivity information */ @@ -101,7 +99,7 @@ public class ConnectivityManagerTestActivity extends Activity { private class ConnectivityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - Log.v(LOG_TAG, "ConnectivityReceiver: onReceive() is called with " + intent); + log("ConnectivityReceiver: onReceive() is called with " + intent); String action = intent.getAction(); if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { Log.v("ConnectivityReceiver", "onReceive() called with " + intent); @@ -126,9 +124,9 @@ public class ConnectivityManagerTestActivity extends Activity { mReason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON); mIsFailOver = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false); - Log.v(LOG_TAG, "mNetworkInfo: " + mNetworkInfo.toString()); + log("mNetworkInfo: " + mNetworkInfo.toString()); if (mOtherNetworkInfo != null) { - Log.v(LOG_TAG, "mOtherNetworkInfo: " + mOtherNetworkInfo.toString()); + log("mOtherNetworkInfo: " + mOtherNetworkInfo.toString()); } recordNetworkState(mNetworkInfo.getType(), mNetworkInfo.getState()); if (mOtherNetworkInfo != null) { @@ -148,7 +146,7 @@ public class ConnectivityManagerTestActivity extends Activity { } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { mWifiNetworkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); - Log.v(LOG_TAG, "mWifiNetworkInfo: " + mWifiNetworkInfo.toString()); + log("mWifiNetworkInfo: " + mWifiNetworkInfo.toString()); if (mWifiNetworkInfo.getState() == State.CONNECTED) { mBssid = intent.getStringExtra(WifiManager.EXTRA_BSSID); } @@ -181,7 +179,7 @@ public class ConnectivityManagerTestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Log.v(LOG_TAG, "onCreate, inst=" + Integer.toHexString(hashCode())); + log("onCreate, inst=" + Integer.toHexString(hashCode())); // Create a simple layout LinearLayout contentView = new LinearLayout(this); @@ -212,7 +210,7 @@ public class ConnectivityManagerTestActivity extends Activity { initializeNetworkStates(); if (mWifiManager.isWifiEnabled()) { - Log.v(LOG_TAG, "Clear Wifi before we start the test."); + log("Clear Wifi before we start the test."); removeConfiguredNetworksAndDisableWifi(); } mWifiRegexs = mCM.getTetherableWifiRegexs(); @@ -220,32 +218,22 @@ public class ConnectivityManagerTestActivity extends Activity { public List<WifiConfiguration> loadNetworkConfigurations() throws Exception { InputStream in = getAssets().open(ACCESS_POINT_FILE); - AccessPointParserHelper parseHelper = new AccessPointParserHelper(); - return parseHelper.processAccessPoint(in); - } - - private void printNetConfig(String[] configuration) { - for (int i = 0; i < configuration.length; i++) { - if (i == 0) { - Log.v(LOG_TAG, "SSID: " + configuration[0]); - } else { - Log.v(LOG_TAG, " " + configuration[i]); - } - } + 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.v(LOG_TAG, "Initialize network state for " + networkType + ": " + + log("Initialize network state for " + networkType + ": " + connectivityState[networkType].toString()); } } // deposit a network state public void recordNetworkState(int networkType, State networkState) { - Log.v(LOG_TAG, "record network state for network " + networkType + + log("record network state for network " + networkType + ", state is " + networkState); connectivityState[networkType].recordState(networkState); } @@ -259,40 +247,40 @@ public class ConnectivityManagerTestActivity extends Activity { // Validate the states recorded public boolean validateNetworkStates(int networkType) { - Log.v(LOG_TAG, "validate network state for " + networkType + ": "); + log("validate network state for " + networkType + ": "); return connectivityState[networkType].validateStateTransition(); } // return result from network state validation public String getTransitionFailureReason(int networkType) { - Log.v(LOG_TAG, "get network state transition failure reason for " + networkType + ": " + + log("get network state transition failure reason for " + networkType + ": " + connectivityState[networkType].toString()); return connectivityState[networkType].getReason(); } private void notifyNetworkConnectivityChange() { synchronized(connectivityObject) { - Log.v(LOG_TAG, "notify network connectivity changed"); + log("notify network connectivity changed"); connectivityObject.notifyAll(); } } private void notifyScanResult() { synchronized (this) { - Log.v(LOG_TAG, "notify that scan results are available"); + log("notify that scan results are available"); this.notify(); } } private void notifyWifiState() { synchronized (wifiObject) { - Log.v(LOG_TAG, "notify wifi state changed"); + log("notify wifi state changed"); wifiObject.notify(); } } private void notifyWifiAPState() { synchronized (this) { - Log.v(LOG_TAG, "notify wifi AP state changed"); + log("notify wifi AP state changed"); this.notify(); } } @@ -306,7 +294,7 @@ public class ConnectivityManagerTestActivity extends Activity { for (Object obj: tethered) { String str = (String)obj; for (String tethRex: mWifiRegexs) { - Log.v(LOG_TAG, "str: " + str +"tethRex: " + tethRex); + log("str: " + str +"tethRex: " + tethRex); if (str.matches(tethRex)) { wifiTethered = true; } @@ -316,7 +304,7 @@ public class ConnectivityManagerTestActivity extends Activity { for (Object obj: errored) { String str = (String)obj; for (String tethRex: mWifiRegexs) { - Log.v(LOG_TAG, "error: str: " + str +"tethRex: " + tethRex); + log("error: str: " + str +"tethRex: " + tethRex); if (str.matches(tethRex)) { wifiErrored = true; } @@ -328,7 +316,7 @@ public class ConnectivityManagerTestActivity extends Activity { } else if (wifiErrored) { mWifiTetherResult = FAILURE; // wifi tethering failed } - Log.v(LOG_TAG, "mWifiTetherResult: " + mWifiTetherResult); + log("mWifiTetherResult: " + mWifiTetherResult); this.notify(); } } @@ -344,12 +332,12 @@ public class ConnectivityManagerTestActivity extends Activity { return false; } else { // the broadcast has been sent out. the state has been changed. - Log.v(LOG_TAG, "networktype: " + networkType + " state: " + + log("networktype: " + networkType + " state: " + mCM.getNetworkInfo(networkType)); return true; } } - Log.v(LOG_TAG, "Wait for the connectivity state for network: " + networkType + + log("Wait for the connectivity state for network: " + networkType + " to be " + expectedState.toString()); synchronized (connectivityObject) { try { @@ -359,7 +347,7 @@ public class ConnectivityManagerTestActivity extends Activity { } if ((mNetworkInfo.getType() != networkType) || (mNetworkInfo.getState() != expectedState)) { - Log.v(LOG_TAG, "network state for " + mNetworkInfo.getType() + + log("network state for " + mNetworkInfo.getType() + "is: " + mNetworkInfo.getState()); continue; } @@ -380,7 +368,7 @@ public class ConnectivityManagerTestActivity extends Activity { return true; } } - Log.v(LOG_TAG, "Wait for wifi state to be: " + expectedState); + log("Wait for wifi state to be: " + expectedState); synchronized (wifiObject) { try { wifiObject.wait(SHORT_TIMEOUT); @@ -388,7 +376,7 @@ public class ConnectivityManagerTestActivity extends Activity { e.printStackTrace(); } if (mWifiState != expectedState) { - Log.v(LOG_TAG, "Wifi state is: " + mWifiNetworkInfo.getState()); + log("Wifi state is: " + mWifiState); continue; } return true; @@ -408,7 +396,7 @@ public class ConnectivityManagerTestActivity extends Activity { return true; } } - Log.v(LOG_TAG, "Wait for wifi AP state to be: " + expectedState); + log("Wait for wifi AP state to be: " + expectedState); synchronized (wifiObject) { try { wifiObject.wait(SHORT_TIMEOUT); @@ -416,7 +404,7 @@ public class ConnectivityManagerTestActivity extends Activity { e.printStackTrace(); } if (mWifiManager.getWifiApState() != expectedState) { - Log.v(LOG_TAG, "Wifi state is: " + mWifiManager.getWifiApState()); + log("Wifi state is: " + mWifiManager.getWifiApState()); continue; } return true; @@ -436,7 +424,7 @@ public class ConnectivityManagerTestActivity extends Activity { if ((System.currentTimeMillis() - startTime) > timeout) { return mWifiTetherResult; } - Log.v(LOG_TAG, "Wait for wifi tethering result."); + log("Wait for wifi tethering result."); synchronized (this) { try { this.wait(SHORT_TIMEOUT); @@ -490,64 +478,61 @@ public class ConnectivityManagerTestActivity extends Activity { //If Wifi is not enabled, enable it if (!mWifiManager.isWifiEnabled()) { - Log.v(LOG_TAG, "Wifi is not enabled, enable it"); + 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)) { + log("wait for WIFI_STATE_ENABLED failed"); + return false; + } } - List<ScanResult> netList = mWifiManager.getScanResults(); - if (netList == null) { - Log.v(LOG_TAG, "scan results are null"); - // if no scan results are available, start active scan - mWifiManager.startScanActive(); - mScanResultIsAvailable = false; - long startTime = System.currentTimeMillis(); - while (!mScanResultIsAvailable) { - if ((System.currentTimeMillis() - startTime) > WIFI_SCAN_TIMEOUT) { - return false; + boolean foundApInScanResults = false; + for (int retry = 0; retry < 5; retry++) { + List<ScanResult> netList = mWifiManager.getScanResults(); + if (netList != null) { + log("size of scan result list: " + netList.size()); + for (int i = 0; i < netList.size(); i++) { + ScanResult sr= netList.get(i); + if (sr.SSID.equals(ssid)) { + log("found " + ssid + " in the scan result list"); + log("retry: " + retry); + foundApInScanResults = true; + mWifiManager.connectNetwork(config); + break; + } } - // wait for the scan results to be available - synchronized (this) { - // wait for the scan result to be available - try { - this.wait(WAIT_FOR_SCAN_RESULT); - } catch (InterruptedException e) { - e.printStackTrace(); + } + if (foundApInScanResults) { + return true; + } else { + // Start an active scan + mWifiManager.startScanActive(); + mScanResultIsAvailable = false; + long startTime = System.currentTimeMillis(); + while (!mScanResultIsAvailable) { + if ((System.currentTimeMillis() - startTime) > WIFI_SCAN_TIMEOUT) { + log("wait for scan results timeout"); + return false; } - if ((mWifiManager.getScanResults() == null) || - (mWifiManager.getScanResults().size() <= 0)) { - continue; + // wait for the scan results to be available + synchronized (this) { + // wait for the scan result to be available + try { + this.wait(WAIT_FOR_SCAN_RESULT); + } catch (InterruptedException e) { + e.printStackTrace(); + } + if ((mWifiManager.getScanResults() == null) || + (mWifiManager.getScanResults().size() <= 0)) { + continue; + } + mScanResultIsAvailable = true; } - mScanResultIsAvailable = true; } } } - - netList = mWifiManager.getScanResults(); - - for (int i = 0; i < netList.size(); i++) { - ScanResult sr= netList.get(i); - if (sr.SSID.equals(ssid)) { - Log.v(LOG_TAG, "found " + ssid + " in the scan result list"); - int networkId = mWifiManager.addNetwork(config); - // Connect to network by disabling others. - mWifiManager.enableNetwork(networkId, true); - mWifiManager.saveConfiguration(); - List<WifiConfiguration> wifiNetworks = mWifiManager.getConfiguredNetworks(); - for (WifiConfiguration netConfig : wifiNetworks) { - Log.v(LOG_TAG, netConfig.toString()); - } - - mWifiManager.reconnect(); - break; - } - } - - List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks(); - if (netConfList.size() <= 0) { - Log.v(LOG_TAG, ssid + " is not available"); - return false; - } - return true; + return false; } /* @@ -555,27 +540,13 @@ public class ConnectivityManagerTestActivity extends Activity { */ public boolean disconnectAP() { if (mWifiManager.isWifiEnabled()) { - //remove the current network Id - WifiInfo curWifi = mWifiManager.getConnectionInfo(); - if (curWifi == null) { - return false; - } - int curNetworkId = curWifi.getNetworkId(); - mWifiManager.removeNetwork(curNetworkId); - mWifiManager.saveConfiguration(); - - // remove other saved networks - List<WifiConfiguration> netConfList = mWifiManager.getConfiguredNetworks(); - if (netConfList != null) { - Log.v(LOG_TAG, "remove configured network ids"); - for (int i = 0; i < netConfList.size(); i++) { - WifiConfiguration conf = new WifiConfiguration(); - conf = netConfList.get(i); - mWifiManager.removeNetwork(conf.networkId); - } + // remove saved networks + List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks(); + for (WifiConfiguration wifiConfig: wifiConfigList) { + log("remove wifi configuration: " + wifiConfig.toString()); + mWifiManager.forgetNetwork(wifiConfig.networkId); } } - mWifiManager.saveConfiguration(); return true; } /** @@ -599,7 +570,7 @@ public class ConnectivityManagerTestActivity extends Activity { } // Wait for the actions to be completed try { - Thread.sleep(5*1000); + Thread.sleep(SHORT_TIMEOUT); } catch (InterruptedException e) {} return true; } @@ -632,7 +603,7 @@ public class ConnectivityManagerTestActivity extends Activity { if (mWifiReceiver != null) { unregisterReceiver(mWifiReceiver); } - Log.v(LOG_TAG, "onDestroy, inst=" + Integer.toHexString(hashCode())); + log("onDestroy, inst=" + Integer.toHexString(hashCode())); } @Override @@ -674,4 +645,8 @@ public class ConnectivityManagerTestActivity extends Activity { } return super.onKeyDown(keyCode, event); } + + private void log(String message) { + Log.v(LOG_TAG, message); + } } diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java index 69eb5db..9c72102 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java @@ -17,26 +17,33 @@ package com.android.connectivitymanagertest.functional; import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; -import com.android.connectivitymanagertest.NetworkState; +import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; import android.R; import android.app.Activity; +import android.content.ContentResolver; import android.content.Intent; import android.content.Context; import android.content.res.Resources; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiConfiguration.KeyMgmt; +import android.net.wifi.WifiConfiguration.Status; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.ConnectivityManager; +import android.net.DhcpInfo; import android.net.NetworkInfo; import android.net.NetworkInfo.State; +import android.provider.Settings; import android.test.suitebuilder.annotation.LargeTest; import android.test.ActivityInstrumentationTestCase2; import android.util.Log; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; /** * Test Wi-Fi connection with different configuration @@ -48,18 +55,25 @@ import java.util.List; public class WifiConnectionTest extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> { private static final String TAG = "WifiConnectionTest"; - private static final boolean DEBUG = true; - private static final String PKG_NAME = "com.android.connectivitymanagertests"; + private static final boolean DEBUG = false; private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>(); private ConnectivityManagerTestActivity mAct; + private ConnectivityManagerTestRunner mRunner; + private WifiManager mWifiManager = null; + private Set<WifiConfiguration> enabledNetworks = null; public WifiConnectionTest() { - super(PKG_NAME, ConnectivityManagerTestActivity.class); + super(ConnectivityManagerTestActivity.class); } @Override public void setUp() throws Exception { super.setUp(); + log("before we launch the test activity, we preserve all the configured networks."); + mRunner = ((ConnectivityManagerTestRunner)getInstrumentation()); + mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE); + enabledNetworks = getEnabledNetworks(mWifiManager.getConfiguredNetworks()); + mAct = getActivity(); networks = mAct.loadNetworkConfigurations(); if (DEBUG) { @@ -68,30 +82,61 @@ public class WifiConnectionTest // enable Wifi and verify wpa_supplicant is started assertTrue("enable Wifi failed", mAct.enableWifi()); - try { - Thread.sleep( 2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT); - } catch (Exception e) { - fail("interrupted while waiting for WPA_SUPPLICANT to start"); - } + sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT, + "interrupted while waiting for WPA_SUPPLICANT to start"); WifiInfo mConnection = mAct.mWifiManager.getConnectionInfo(); assertNotNull(mConnection); assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant()); } private void printNetworkConfigurations() { - Log.v(TAG, "==== print network configurations parsed from XML file ===="); - Log.v(TAG, "number of access points: " + networks.size()); + log("==== print network configurations parsed from XML file ===="); + log("number of access points: " + networks.size()); for (WifiConfiguration config : networks) { - Log.v(TAG, config.toString()); + log(config.toString()); } } @Override public void tearDown() throws Exception { + log("tearDown()"); mAct.removeConfiguredNetworksAndDisableWifi(); + reEnableNetworks(enabledNetworks); super.tearDown(); } + private Set<WifiConfiguration> getEnabledNetworks(List<WifiConfiguration> configuredNetworks) { + Set<WifiConfiguration> networks = new HashSet<WifiConfiguration>(); + for (WifiConfiguration wifiConfig : configuredNetworks) { + if (wifiConfig.status == Status.ENABLED || wifiConfig.status == Status.CURRENT) { + networks.add(wifiConfig); + log("remembering enabled network " + wifiConfig.SSID + + " status is " + wifiConfig.status); + } + } + return networks; + } + + private void reEnableNetworks(Set<WifiConfiguration> enabledNetworks) { + if (!mWifiManager.isWifiEnabled()) { + log("reEnableNetworks: enable Wifi"); + mWifiManager.setWifiEnabled(true); + sleep(ConnectivityManagerTestActivity.SHORT_TIMEOUT, + "interruped while waiting for wifi to be enabled"); + } + + for (WifiConfiguration config : enabledNetworks) { + if (DEBUG) { + log("recover wifi configuration: " + config.toString()); + } + config.SSID = "\"" + config.SSID + "\""; + config.networkId = -1; + mWifiManager.connectNetwork(config); + sleep(ConnectivityManagerTestActivity.SHORT_TIMEOUT, + "interruped while connecting to " + config.SSID); + } + } + /** * Connect to the provided Wi-Fi network * @param config is the network configuration @@ -103,32 +148,40 @@ public class WifiConnectionTest mAct.connectToWifiWithConfiguration(config)); // step 2: verify Wifi state and network state; - assertTrue(mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED, - ConnectivityManagerTestActivity.SHORT_TIMEOUT)); assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, - State.CONNECTED, ConnectivityManagerTestActivity.LONG_TIMEOUT)); + State.CONNECTED, 2 * ConnectivityManagerTestActivity.LONG_TIMEOUT)); // step 3: verify the current connected network is the given SSID + assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo()); if (DEBUG) { - Log.v(TAG, "config.SSID = " + config.SSID); - Log.v(TAG, "mAct.mWifiManager.getConnectionInfo.getSSID()" + + log("config.SSID = " + config.SSID); + log("mAct.mWifiManager.getConnectionInfo.getSSID()" + mAct.mWifiManager.getConnectionInfo().getSSID()); } assertTrue(config.SSID.contains(mAct.mWifiManager.getConnectionInfo().getSSID())); + } - // Maintain the connection for 50 seconds before switching + private void sleep(long sometime, String errorMsg) { try { - Thread.sleep(50*1000); - } catch (Exception e) { - fail("interrupted while waiting for WPA_SUPPLICANT to start"); + 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; + log("-- START Wi-Fi connection test to : " + ssid + " --"); connectToWifi(networks.get(i)); - mAct.removeConfiguredNetworksAndDisableWifi(); + sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT, + "interruped while waiting for wifi disabled."); + log("-- END Wi-Fi connection test to " + ssid + " -- "); } } } |