Skip to content

Commit c1c04ad

Browse files
Xia WangAndroid (Google) Code Review
authored andcommitted
Merge "Add Wi-Fi tests for static IP. DO NOT MERGE" into gingerbread
2 parents 93116f8 + 739d0ae commit c1c04ad

File tree

4 files changed

+197
-21
lines changed

4 files changed

+197
-21
lines changed

core/tests/ConnectivityManagerTest/assets/accesspoints.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@
1313
<security>PSK</security>
1414
<password>androidwifi</password>
1515
</accesspoint>
16+
<accesspoint>
17+
<ssid>securenetstatic</ssid>
18+
<security>PSK</security>
19+
<password>androidwifi</password>
20+
<ip>192.168.14.2</ip>
21+
<gateway>192.168.14.1</gateway>
22+
<netmask>255.255.255.0</netmask>
23+
<dns1>192.168.14.1</dns1>
24+
<dns2>192.168.1.9</dns2>
25+
</accesspoint>
1626
<accesspoint>
1727
<ssid>botnet</ssid>
1828
<security>EAP</security>

core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java

Lines changed: 98 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import android.net.wifi.WifiConfiguration;
2727
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
2828
import android.net.wifi.WifiConfiguration.KeyMgmt;
29+
import android.net.DhcpInfo;
2930

30-
import android.util.Log;
3131
import java.io.InputStream;
32+
import java.net.UnknownHostException;
3233
import java.util.ArrayList;
34+
import java.util.HashMap;
3335
import java.util.List;
3436

3537

@@ -38,7 +40,8 @@
3840
* The configurations of an access point is included in tag
3941
* <accesspoint></accesspoint>. The supported configuration includes: ssid,
4042
* security, eap, phase2, identity, password, anonymousidentity, cacert, usercert,
41-
* in which each is included in the corresponding tags. All access points have to be
43+
* in which each is included in the corresponding tags. Static IP setting is also supported.
44+
* Tags that can be used include: ip, gateway, netmask, dns1, dns2. All access points have to be
4245
* enclosed in tags of <resources></resources>.
4346
*
4447
* The following is a sample configuration file for an access point using EAP-PEAP with MSCHAP2.
@@ -62,6 +65,7 @@ public class AccessPointParserHelper {
6265
static final int EAP = 3;
6366

6467
List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
68+
HashMap<String, DhcpInfo> ssidToDhcpInfoHM = new HashMap<String, DhcpInfo>();
6569

6670
private int getSecurityType (String security) {
6771
if (security.equalsIgnoreCase("NONE")) {
@@ -87,15 +91,34 @@ private boolean validateEapValue(String value) {
8791
}
8892
}
8993

94+
private static int stringToIpAddr(String addrString) throws UnknownHostException {
95+
try {
96+
String[] parts = addrString.split("\\.");
97+
if (parts.length != 4) {
98+
throw new UnknownHostException(addrString);
99+
}
100+
101+
int a = Integer.parseInt(parts[0]) ;
102+
int b = Integer.parseInt(parts[1]) << 8;
103+
int c = Integer.parseInt(parts[2]) << 16;
104+
int d = Integer.parseInt(parts[3]) << 24;
105+
106+
return a | b | c | d;
107+
} catch (NumberFormatException ex) {
108+
throw new UnknownHostException(addrString);
109+
}
110+
}
111+
90112
DefaultHandler mHandler = new DefaultHandler() {
91113

92114
boolean ssid = false;
93115
boolean security = false;
94116
boolean password = false;
95117
boolean ip = false;
96-
boolean subnetmask = false;
118+
boolean netmask = false;
97119
boolean gateway = false;
98-
boolean dns = false;
120+
boolean dns1 = false;
121+
boolean dns2 = false;
99122
boolean eap = false;
100123
boolean phase2 = false;
101124
boolean identity = false;
@@ -104,6 +127,7 @@ private boolean validateEapValue(String value) {
104127
boolean usercert = false;
105128
WifiConfiguration config = null;
106129
int securityType = NONE;
130+
DhcpInfo mDhcpInfo = null;
107131

108132
@Override
109133
public void startElement(String uri, String localName, String tagName,
@@ -138,28 +162,43 @@ public void startElement(String uri, String localName, String tagName,
138162
if (tagName.equalsIgnoreCase("usercert")) {
139163
usercert = true;
140164
}
165+
if (tagName.equalsIgnoreCase("ip")) {
166+
ip = true;
167+
mDhcpInfo = new DhcpInfo();
168+
}
169+
if (tagName.equalsIgnoreCase("gateway")) {
170+
gateway = true;
171+
}
172+
if (tagName.equalsIgnoreCase("netmask")) {
173+
netmask = true;
174+
}
175+
if (tagName.equalsIgnoreCase("dns1")) {
176+
dns1 = true;
177+
}
178+
if (tagName.equalsIgnoreCase("dns2")) {
179+
dns2 = true;
180+
}
141181
}
142182

143183
@Override
144184
public void endElement(String uri, String localName, String tagName) throws SAXException {
145-
Log.v(TAG, "endElement: " + tagName);
146185
if (tagName.equalsIgnoreCase("accesspoint")) {
147186
networks.add(config);
187+
if (mDhcpInfo != null) {
188+
ssidToDhcpInfoHM.put(config.SSID, mDhcpInfo);
189+
}
148190
}
149191
}
150192

151193
@Override
152194
public void characters(char ch[], int start, int length) throws SAXException {
153195
if (ssid) {
154196
config.SSID = new String(ch, start, length);
155-
Log.v(TAG, "ssid: " + config.SSID);
156197
ssid = false;
157198
}
158199
if (security) {
159200
String securityStr = (new String(ch, start, length)).toUpperCase();
160-
Log.v(TAG, "security: " + securityStr);
161201
securityType = getSecurityType(securityStr);
162-
Log.v(TAG, "securityType = " + securityType);
163202
switch (securityType) {
164203
case NONE:
165204
config.allowedKeyManagement.set(KeyMgmt.NONE);
@@ -187,7 +226,6 @@ public void characters(char ch[], int start, int length) throws SAXException {
187226
if (len == 0) {
188227
throw new SAXException();
189228
}
190-
Log.v(TAG, "passwordStr:" + passwordStr);
191229
if (securityType == WEP) {
192230
if ((len == 10 || len == 26 || len == 58) &&
193231
passwordStr.matches("[0-9A-Fa-f]*")) {
@@ -242,21 +280,65 @@ public void characters(char ch[], int start, int length) throws SAXException {
242280
config.client_cert.setValue(KEYSTORE_SPACE);
243281
usercert = false;
244282
}
283+
if (ip) {
284+
try {
285+
mDhcpInfo.ipAddress = stringToIpAddr(new String(ch, start, length));
286+
} catch (UnknownHostException e) {
287+
throw new SAXException();
288+
}
289+
ip = false;
290+
}
291+
if (gateway) {
292+
try {
293+
mDhcpInfo.gateway = stringToIpAddr(new String(ch, start, length));
294+
} catch (UnknownHostException e) {
295+
throw new SAXException();
296+
}
297+
gateway = false;
298+
}
299+
if (netmask) {
300+
try {
301+
mDhcpInfo.netmask = stringToIpAddr(new String(ch, start, length));
302+
} catch (UnknownHostException e) {
303+
throw new SAXException();
304+
}
305+
netmask = false;
306+
}
307+
if (dns1) {
308+
try {
309+
mDhcpInfo.dns1 = stringToIpAddr(new String(ch, start, length));
310+
} catch (UnknownHostException e) {
311+
throw new SAXException();
312+
}
313+
dns1 = false;
314+
}
315+
if (dns2) {
316+
try {
317+
mDhcpInfo.dns2 = stringToIpAddr(new String(ch, start, length));
318+
} catch (UnknownHostException e) {
319+
throw new SAXException();
320+
}
321+
dns2 = false;
322+
}
245323
}
246324
};
247325

248-
public AccessPointParserHelper() {
249-
}
250-
251326
/**
252-
* Process the accesspoint.xml file
253-
* @return List of WifiConfiguration
254-
* @throws Exception when parsing the XML file
327+
* Process the InputStream in
328+
* @param in is the InputStream that can be used for XML parsing
329+
* @throws Exception
255330
*/
256-
public List<WifiConfiguration> processAccessPoint(InputStream in) throws Exception {
331+
public AccessPointParserHelper(InputStream in) throws Exception {
257332
SAXParserFactory factory = SAXParserFactory.newInstance();
258333
SAXParser saxParser = factory.newSAXParser();
259334
saxParser.parse(in, mHandler);
335+
}
336+
337+
public List<WifiConfiguration> getNetworkConfigurations() throws Exception {
260338
return networks;
261339
}
340+
341+
public HashMap<String, DhcpInfo> getSsidToDhcpInfoHashMap() {
342+
return ssidToDhcpInfoHM;
343+
}
262344
}

core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030

3131
import java.io.InputStream;
3232
import java.util.ArrayList;
33+
import java.util.HashMap;
3334
import java.util.List;
3435
import android.widget.LinearLayout;
3536
import android.net.ConnectivityManager;
37+
import android.net.DhcpInfo;
3638
import android.net.NetworkInfo;
3739
import android.net.NetworkInfo.State;
3840

@@ -61,6 +63,7 @@ public class ConnectivityManagerTestActivity extends Activity {
6163
private static final String ACCESS_POINT_FILE = "accesspoints.xml";
6264
public ConnectivityReceiver mConnectivityReceiver = null;
6365
public WifiReceiver mWifiReceiver = null;
66+
private AccessPointParserHelper mParseHelper = null;
6467
/*
6568
* Track network connectivity information
6669
*/
@@ -156,6 +159,7 @@ public void onReceive(Context context, Intent intent) {
156159
} else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
157160
mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
158161
WifiManager.WIFI_STATE_UNKNOWN);
162+
Log.v(LOG_TAG, "mWifiState: " + mWifiState);
159163
notifyWifiState();
160164
} else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) {
161165
notifyWifiAPState();
@@ -220,10 +224,19 @@ protected void onCreate(Bundle savedInstanceState) {
220224

221225
public List<WifiConfiguration> loadNetworkConfigurations() throws Exception {
222226
InputStream in = getAssets().open(ACCESS_POINT_FILE);
223-
AccessPointParserHelper parseHelper = new AccessPointParserHelper();
224-
return parseHelper.processAccessPoint(in);
227+
mParseHelper = new AccessPointParserHelper(in);
228+
return mParseHelper.getNetworkConfigurations();
225229
}
226230

231+
public HashMap<String, DhcpInfo> getDhcpInfo() throws Exception{
232+
if (mParseHelper == null) {
233+
InputStream in = getAssets().open(ACCESS_POINT_FILE);
234+
mParseHelper = new AccessPointParserHelper(in);
235+
}
236+
return mParseHelper.getSsidToDhcpInfoHashMap();
237+
}
238+
239+
227240
private void printNetConfig(String[] configuration) {
228241
for (int i = 0; i < configuration.length; i++) {
229242
if (i == 0) {
@@ -388,7 +401,7 @@ public boolean waitForWifiState(int expectedState, long timeout) {
388401
e.printStackTrace();
389402
}
390403
if (mWifiState != expectedState) {
391-
Log.v(LOG_TAG, "Wifi state is: " + mWifiNetworkInfo.getState());
404+
Log.v(LOG_TAG, "Wifi state is: " + mWifiState);
392405
continue;
393406
}
394407
return true;

0 commit comments

Comments
 (0)