Loading core/tests/ConnectivityManagerTest/assets/accesspoints.xml +11 −1 Original line number Diff line number Diff line Loading @@ -13,6 +13,16 @@ <security>PSK</security> <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> Loading core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java +132 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. Loading @@ -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://"; Loading Loading @@ -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; Loading @@ -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, Loading Loading @@ -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); } } Loading @@ -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); Loading @@ -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(); Loading @@ -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]*")) { Loading Loading @@ -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(); } }; public AccessPointParserHelper() { 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; } } }; /** * 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; } } core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java +86 −111 File changed.Preview size limit exceeded, changes collapsed. Show changes core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java +75 −22 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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 Loading @@ -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 + " -- "); } } } Loading
core/tests/ConnectivityManagerTest/assets/accesspoints.xml +11 −1 Original line number Diff line number Diff line Loading @@ -13,6 +13,16 @@ <security>PSK</security> <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> Loading
core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java +132 −16 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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. Loading @@ -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://"; Loading Loading @@ -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; Loading @@ -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, Loading Loading @@ -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); } } Loading @@ -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); Loading @@ -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(); Loading @@ -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]*")) { Loading Loading @@ -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(); } }; public AccessPointParserHelper() { 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; } } }; /** * 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; } }
core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java +86 −111 File changed.Preview size limit exceeded, changes collapsed. Show changes
core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java +75 −22 Original line number Diff line number Diff line Loading @@ -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 Loading @@ -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) { Loading @@ -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 Loading @@ -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 + " -- "); } } }