summaryrefslogtreecommitdiffstats
path: root/core/tests/ConnectivityManagerTest
diff options
context:
space:
mode:
authorXia Wang <xiaw@google.com>2013-05-02 21:19:40 -0700
committerXia Wang <xiaw@google.com>2013-05-08 11:27:27 -0700
commit58a5db37b3de268aea18b160234f9fd4d70a970d (patch)
tree8f578755036ae696f60507fb985d5f1954b30b51 /core/tests/ConnectivityManagerTest
parent2b662e8ef96c645e38a3debc29afa7454664f6cb (diff)
downloadframeworks_base-58a5db37b3de268aea18b160234f9fd4d70a970d.zip
frameworks_base-58a5db37b3de268aea18b160234f9fd4d70a970d.tar.gz
frameworks_base-58a5db37b3de268aea18b160234f9fd4d70a970d.tar.bz2
Add wifi association test
Change-Id: I1f3f49a1857835522c9c5e02ed4f530b83df1d02
Diffstat (limited to 'core/tests/ConnectivityManagerTest')
-rw-r--r--core/tests/ConnectivityManagerTest/AndroidManifest.xml5
-rw-r--r--core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiAssociationTest.java204
2 files changed, 209 insertions, 0 deletions
diff --git a/core/tests/ConnectivityManagerTest/AndroidManifest.xml b/core/tests/ConnectivityManagerTest/AndroidManifest.xml
index 2c34d47..a63a453 100644
--- a/core/tests/ConnectivityManagerTest/AndroidManifest.xml
+++ b/core/tests/ConnectivityManagerTest/AndroidManifest.xml
@@ -32,6 +32,11 @@
</intent-filter>
</activity>
</application>
+ <!-- default test runner -->
+ <instrumentation android:name="android.test.InstrumentationTestRunner"
+ android:targetPackage="com.android.connectivitymanagertest"
+ android:label="default instrumentation test runner"
+ />
<!--
This declares that this app uses the instrumentation test runner targeting
the package of connectivitymanagertest. To run the tests use the command:
diff --git a/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiAssociationTest.java b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiAssociationTest.java
new file mode 100644
index 0000000..87a98bf
--- /dev/null
+++ b/core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiAssociationTest.java
@@ -0,0 +1,204 @@
+/*
+ * Copyright (C) 2013, 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.functional;
+
+import com.android.connectivitymanagertest.ConnectivityManagerTestActivity;
+
+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.WifiConfiguration.Status;
+import android.net.wifi.WifiManager;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.net.NetworkInfo.State;
+import android.test.suitebuilder.annotation.LargeTest;
+import android.test.ActivityInstrumentationTestCase2;
+import android.test.InstrumentationTestRunner;
+import android.util.Log;
+
+/**
+ * Test Wi-Fi connection with different configuration
+ * To run this tests:
+ * adb shell am instrument -e ssid <ssid> -e password <password>
+ * -e security-type <security-type>
+ * -w com.android.connectivitymanagertest/android.test.InstrumentationTestRunner
+ */
+public class WifiAssociationTest
+ extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> {
+ private static final String TAG = "WifiAssociationTest";
+ private ConnectivityManagerTestActivity mAct;
+ private String mSsid = null;
+ private String mPassword = null;
+ private String mSecurityType = null;
+ private WifiManager mWifiManager = null;
+
+ enum SECURITY_TYPE {
+ OPEN, WEP64, WEP128, WPA_TKIP, WPA2_AES
+ };
+
+ public WifiAssociationTest() {
+ super(ConnectivityManagerTestActivity.class);
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ InstrumentationTestRunner mRunner = (InstrumentationTestRunner)getInstrumentation();
+ mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE);
+ mAct = getActivity();
+ Bundle arguments = mRunner.getArguments();
+ mSecurityType = arguments.getString("security-type");
+ mSsid = arguments.getString("ssid");
+ mPassword = arguments.getString("password");
+ assertNotNull("Security type is empty", mSecurityType);
+ assertNotNull("Ssid is empty", mSsid);
+ // enable Wifi and verify wpa_supplicant is started
+ assertTrue("enable Wifi failed", mAct.enableWifi());
+ 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());
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ log("tearDown()");
+ 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 associate with " + config.SSID,
+ mAct.connectToWifiWithConfiguration(config));
+
+ // step 2: verify Wifi state and network state;
+ assertTrue("failed to connect with " + config.SSID,
+ mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI,
+ State.CONNECTED, ConnectivityManagerTestActivity.WIFI_CONNECTION_TIMEOUT));
+
+ // step 3: verify the current connected network is the given SSID
+ assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo());
+ assertTrue(config.SSID.contains(mAct.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 testWifiAssociation() {
+ assertNotNull("no test ssid", mSsid);
+ WifiConfiguration config = new WifiConfiguration();
+ config.SSID = mSsid;
+ SECURITY_TYPE security = SECURITY_TYPE.valueOf(mSecurityType);
+ log("Security type is " + security.toString());
+ switch (security) {
+ // set network configurations
+ case OPEN:
+ config.allowedKeyManagement.set(KeyMgmt.NONE);
+ break;
+ case WEP64:
+ // always use hex pair for WEP-40
+ assertTrue("not a WEP64 security type?", mPassword.length() == 10);
+ 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]*")) {
+ config.wepKeys[0] = mPassword;
+ } else {
+ fail("Please type hex pair for the password");
+ }
+ }
+ 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]*")) {
+ config.wepKeys[0] = mPassword;
+ } else {
+ fail("Please type hex pair for the password");
+ }
+ }
+ break;
+ case WPA_TKIP:
+ assertNotNull("missing password", mPassword);
+ config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
+ config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
+ config.allowedProtocols.set(Protocol.WPA);
+ config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
+ config.allowedGroupCiphers.set(GroupCipher.TKIP);
+ if (mPassword.matches("[0-9A-Fa-f]{64}")) {
+ config.preSharedKey = mPassword;
+ } else {
+ config.preSharedKey = '"' + mPassword + '"';
+ }
+ break;
+ case WPA2_AES:
+ assertNotNull("missing password", mPassword);
+ config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
+ config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
+ config.allowedProtocols.set(Protocol.RSN);
+ config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
+ config.allowedGroupCiphers.set(GroupCipher.CCMP);
+ config.allowedProtocols.set(Protocol.RSN);
+ if (mPassword.matches("[0-9A-Fa-f]{64}")) {
+ config.preSharedKey = mPassword;
+ } else {
+ config.preSharedKey = '"' + mPassword + '"';
+ }
+ break;
+ default:
+ fail("Not a valid security type: " + mSecurityType);
+ break;
+ }
+ Log.v(TAG, "network config: " + config.toString());
+ connectToWifi(config);
+ }
+}