diff options
4 files changed, 197 insertions, 21 deletions
diff --git a/core/tests/ConnectivityManagerTest/assets/accesspoints.xml b/core/tests/ConnectivityManagerTest/assets/accesspoints.xml index 2b0e4af..b6bc0de 100755 --- a/core/tests/ConnectivityManagerTest/assets/accesspoints.xml +++ b/core/tests/ConnectivityManagerTest/assets/accesspoints.xml @@ -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> + <netmask>255.255.255.0</netmask> + <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..37c854a 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java @@ -26,10 +26,12 @@ import org.xml.sax.helpers.DefaultHandler; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.AuthAlgorithm; import android.net.wifi.WifiConfiguration.KeyMgmt; +import android.net.DhcpInfo; -import android.util.Log; import java.io.InputStream; +import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; @@ -38,7 +40,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. @@ -62,6 +65,7 @@ public class AccessPointParserHelper { static final int EAP = 3; List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>(); + HashMap<String, DhcpInfo> ssidToDhcpInfoHM = new HashMap<String, DhcpInfo>(); private int getSecurityType (String security) { if (security.equalsIgnoreCase("NONE")) { @@ -87,15 +91,34 @@ public class AccessPointParserHelper { } } + private static int stringToIpAddr(String addrString) throws UnknownHostException { + try { + String[] parts = addrString.split("\\."); + if (parts.length != 4) { + throw new UnknownHostException(addrString); + } + + int a = Integer.parseInt(parts[0]) ; + int b = Integer.parseInt(parts[1]) << 8; + int c = Integer.parseInt(parts[2]) << 16; + int d = Integer.parseInt(parts[3]) << 24; + + return a | b | c | d; + } catch (NumberFormatException ex) { + throw new UnknownHostException(addrString); + } + } + DefaultHandler mHandler = new DefaultHandler() { boolean ssid = false; boolean security = false; boolean password = false; boolean ip = false; - boolean subnetmask = false; + boolean netmask = false; boolean gateway = false; - boolean dns = false; + boolean dns1 = false; + boolean dns2 = false; boolean eap = false; boolean phase2 = false; boolean identity = false; @@ -104,6 +127,7 @@ public class AccessPointParserHelper { boolean usercert = false; WifiConfiguration config = null; int securityType = NONE; + DhcpInfo mDhcpInfo = null; @Override public void startElement(String uri, String localName, String tagName, @@ -138,13 +162,31 @@ public class AccessPointParserHelper { if (tagName.equalsIgnoreCase("usercert")) { usercert = true; } + if (tagName.equalsIgnoreCase("ip")) { + ip = true; + mDhcpInfo = new DhcpInfo(); + } + if (tagName.equalsIgnoreCase("gateway")) { + gateway = 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")) { networks.add(config); + if (mDhcpInfo != null) { + ssidToDhcpInfoHM.put(config.SSID, mDhcpInfo); + } } } @@ -152,14 +194,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); @@ -187,7 +226,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 +280,65 @@ public class AccessPointParserHelper { config.client_cert.setValue(KEYSTORE_SPACE); usercert = false; } + if (ip) { + try { + mDhcpInfo.ipAddress = stringToIpAddr(new String(ch, start, length)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + ip = false; + } + if (gateway) { + try { + mDhcpInfo.gateway = stringToIpAddr(new String(ch, start, length)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + gateway = false; + } + if (netmask) { + try { + mDhcpInfo.netmask = stringToIpAddr(new String(ch, start, length)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + netmask = false; + } + if (dns1) { + try { + mDhcpInfo.dns1 = stringToIpAddr(new String(ch, start, length)); + } catch (UnknownHostException e) { + throw new SAXException(); + } + dns1 = false; + } + if (dns2) { + try { + mDhcpInfo.dns2 = stringToIpAddr(new String(ch, start, length)); + } 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; } + + public HashMap<String, DhcpInfo> getSsidToDhcpInfoHashMap() { + return ssidToDhcpInfoHM; + } } diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java index 37b9f52..3d62c61 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java @@ -30,9 +30,11 @@ import android.view.KeyEvent; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import android.widget.LinearLayout; import android.net.ConnectivityManager; +import android.net.DhcpInfo; import android.net.NetworkInfo; import android.net.NetworkInfo.State; @@ -61,6 +63,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 */ @@ -156,6 +159,7 @@ public class ConnectivityManagerTestActivity extends Activity { } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); + Log.v(LOG_TAG, "mWifiState: " + mWifiState); notifyWifiState(); } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { notifyWifiAPState(); @@ -220,10 +224,19 @@ 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); + mParseHelper = new AccessPointParserHelper(in); + return mParseHelper.getNetworkConfigurations(); } + public HashMap<String, DhcpInfo> getDhcpInfo() throws Exception{ + if (mParseHelper == null) { + InputStream in = getAssets().open(ACCESS_POINT_FILE); + mParseHelper = new AccessPointParserHelper(in); + } + return mParseHelper.getSsidToDhcpInfoHashMap(); + } + + private void printNetConfig(String[] configuration) { for (int i = 0; i < configuration.length; i++) { if (i == 0) { @@ -388,7 +401,7 @@ public class ConnectivityManagerTestActivity extends Activity { e.printStackTrace(); } if (mWifiState != expectedState) { - Log.v(LOG_TAG, "Wifi state is: " + mWifiNetworkInfo.getState()); + Log.v(LOG_TAG, "Wifi state is: " + mWifiState); continue; } return true; 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..4140570 100644 --- a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java +++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java @@ -16,11 +16,14 @@ package com.android.connectivitymanagertest.functional; +import com.android.connectivitymanagertest.ConnectivityManagerStressTestRunner; import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; +import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; import com.android.connectivitymanagertest.NetworkState; import android.R; import android.app.Activity; +import android.content.ContentResolver; import android.content.Intent; import android.content.Context; import android.content.res.Resources; @@ -28,15 +31,20 @@ import android.net.wifi.WifiConfiguration; 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.HashMap; import java.util.List; +import java.util.Map.Entry; +import java.util.Set; /** * Test Wi-Fi connection with different configuration @@ -52,18 +60,25 @@ public class WifiConnectionTest private static final String PKG_NAME = "com.android.connectivitymanagertests"; private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>(); private ConnectivityManagerTestActivity mAct; + private HashMap<String, DhcpInfo> hm = null; + private ConnectivityManagerTestRunner mRunner; + private ContentResolver cr; public WifiConnectionTest() { - super(PKG_NAME, ConnectivityManagerTestActivity.class); + super(ConnectivityManagerTestActivity.class); } @Override public void setUp() throws Exception { super.setUp(); mAct = getActivity(); + mRunner = ((ConnectivityManagerTestRunner)getInstrumentation()); + cr = mRunner.getContext().getContentResolver(); networks = mAct.loadNetworkConfigurations(); + hm = mAct.getDhcpInfo(); if (DEBUG) { printNetworkConfigurations(); + printDhcpInfo(); } // enable Wifi and verify wpa_supplicant is started @@ -86,12 +101,36 @@ public class WifiConnectionTest } } + private void printDhcpInfo() { + if (hm == null) { + return; + } else { + Set<Entry<String, DhcpInfo>> set = hm.entrySet(); + for (Entry<String, DhcpInfo> me: set) { + Log.v(TAG, "SSID: " + me.getKey()); + DhcpInfo dhcp = me.getValue(); + Log.v(TAG, " dhcp: " + dhcp.toString()); + Log.v(TAG, "IP: " + intToIpString(dhcp.ipAddress)); + Log.v(TAG, "gateway: " + intToIpString(dhcp.gateway)); + Log.v(TAG, "Netmask: " + intToIpString(dhcp.netmask)); + Log.v(TAG, "DNS1: " + intToIpString(dhcp.dns1)); + Log.v(TAG, "DNS2: " + intToIpString(dhcp.dns2)); + } + } + } + @Override public void tearDown() throws Exception { mAct.removeConfiguredNetworksAndDisableWifi(); super.tearDown(); } + private String intToIpString(int i) { + return ((i & 0xFF) + "." + + ((i >> 8) & 0xFF) + "." + + ((i >> 16) & 0xFF) + "." + + ((i >> 24) & 0xFF)); + } /** * Connect to the provided Wi-Fi network * @param config is the network configuration @@ -99,6 +138,26 @@ public class WifiConnectionTest */ private void connectToWifi(WifiConfiguration config) { // step 1: connect to the test access point + boolean isStaticIP = false; + if (hm.containsKey(config.SSID)) { + DhcpInfo dhcpInfo = hm.get(config.SSID); + if (dhcpInfo != null) { + isStaticIP = true; + // set the system settings: + Settings.System.putInt(cr,Settings.System.WIFI_USE_STATIC_IP, 1); + Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, + intToIpString(dhcpInfo.ipAddress)); + Settings.System.putString(cr, Settings.System.WIFI_STATIC_GATEWAY, + intToIpString(dhcpInfo.gateway)); + Settings.System.putString(cr, Settings.System.WIFI_STATIC_NETMASK, + intToIpString(dhcpInfo.netmask)); + Settings.System.putString(cr, Settings.System.WIFI_STATIC_DNS1, + intToIpString(dhcpInfo.dns1)); + Settings.System.putString(cr, Settings.System.WIFI_STATIC_DNS2, + intToIpString(dhcpInfo.dns2)); + } + } + assertTrue("failed to connect to " + config.SSID, mAct.connectToWifiWithConfiguration(config)); @@ -118,17 +177,29 @@ public class WifiConnectionTest // Maintain the connection for 50 seconds before switching try { - Thread.sleep(50*1000); + Thread.sleep(mAct.LONG_TIMEOUT); } catch (Exception e) { fail("interrupted while waiting for WPA_SUPPLICANT to start"); } + + if (isStaticIP) { + Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 0); + } } @LargeTest public void testWifiConnections() { for (int i = 0; i < networks.size(); i++) { + String ssid = networks.get(i).SSID; + Log.v(TAG, "-- start Wi-Fi connection test for SSID: " + ssid + " --"); connectToWifi(networks.get(i)); mAct.removeConfiguredNetworksAndDisableWifi(); + try { + Thread.sleep(4 * mAct.SHORT_TIMEOUT); + } catch (Exception e) { + fail("Interrupted while disabling wifi"); + } + Log.v(TAG, "-- END Wi-Fi connection test for SSID: " + ssid + " --"); } } } |