WifiAssociationTest.java revision 58a5db37b3de268aea18b160234f9fd4d70a970d
1/* 2 * Copyright (C) 2013, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.connectivitymanagertest.functional; 18 19import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; 20 21import android.content.Context; 22import android.os.Bundle; 23import android.net.wifi.WifiInfo; 24import android.net.wifi.WifiConfiguration; 25import android.net.wifi.WifiConfiguration.KeyMgmt; 26import android.net.wifi.WifiConfiguration.AuthAlgorithm; 27import android.net.wifi.WifiConfiguration.GroupCipher; 28import android.net.wifi.WifiConfiguration.PairwiseCipher; 29import android.net.wifi.WifiConfiguration.Protocol; 30import android.net.wifi.WifiConfiguration.Status; 31import android.net.wifi.WifiManager; 32import android.net.ConnectivityManager; 33import android.net.NetworkInfo; 34import android.net.NetworkInfo.State; 35import android.test.suitebuilder.annotation.LargeTest; 36import android.test.ActivityInstrumentationTestCase2; 37import android.test.InstrumentationTestRunner; 38import android.util.Log; 39 40/** 41 * Test Wi-Fi connection with different configuration 42 * To run this tests: 43 * adb shell am instrument -e ssid <ssid> -e password <password> 44 * -e security-type <security-type> 45 * -w com.android.connectivitymanagertest/android.test.InstrumentationTestRunner 46 */ 47public class WifiAssociationTest 48 extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> { 49 private static final String TAG = "WifiAssociationTest"; 50 private ConnectivityManagerTestActivity mAct; 51 private String mSsid = null; 52 private String mPassword = null; 53 private String mSecurityType = null; 54 private WifiManager mWifiManager = null; 55 56 enum SECURITY_TYPE { 57 OPEN, WEP64, WEP128, WPA_TKIP, WPA2_AES 58 }; 59 60 public WifiAssociationTest() { 61 super(ConnectivityManagerTestActivity.class); 62 } 63 64 @Override 65 public void setUp() throws Exception { 66 super.setUp(); 67 InstrumentationTestRunner mRunner = (InstrumentationTestRunner)getInstrumentation(); 68 mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE); 69 mAct = getActivity(); 70 Bundle arguments = mRunner.getArguments(); 71 mSecurityType = arguments.getString("security-type"); 72 mSsid = arguments.getString("ssid"); 73 mPassword = arguments.getString("password"); 74 assertNotNull("Security type is empty", mSecurityType); 75 assertNotNull("Ssid is empty", mSsid); 76 // enable Wifi and verify wpa_supplicant is started 77 assertTrue("enable Wifi failed", mAct.enableWifi()); 78 sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT, 79 "interrupted while waiting for WPA_SUPPLICANT to start"); 80 WifiInfo mConnection = mAct.mWifiManager.getConnectionInfo(); 81 assertNotNull(mConnection); 82 assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant()); 83 } 84 85 @Override 86 public void tearDown() throws Exception { 87 log("tearDown()"); 88 super.tearDown(); 89 } 90 91 /** 92 * Connect to the provided Wi-Fi network 93 * @param config is the network configuration 94 * @return true if the connection is successful. 95 */ 96 private void connectToWifi(WifiConfiguration config) { 97 // step 1: connect to the test access point 98 assertTrue("failed to associate with " + config.SSID, 99 mAct.connectToWifiWithConfiguration(config)); 100 101 // step 2: verify Wifi state and network state; 102 assertTrue("failed to connect with " + config.SSID, 103 mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, 104 State.CONNECTED, ConnectivityManagerTestActivity.WIFI_CONNECTION_TIMEOUT)); 105 106 // step 3: verify the current connected network is the given SSID 107 assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo()); 108 assertTrue(config.SSID.contains(mAct.mWifiManager.getConnectionInfo().getSSID())); 109 } 110 111 private void sleep(long sometime, String errorMsg) { 112 try { 113 Thread.sleep(sometime); 114 } catch (InterruptedException e) { 115 fail(errorMsg); 116 } 117 } 118 119 private void log(String message) { 120 Log.v(TAG, message); 121 } 122 123 @LargeTest 124 public void testWifiAssociation() { 125 assertNotNull("no test ssid", mSsid); 126 WifiConfiguration config = new WifiConfiguration(); 127 config.SSID = mSsid; 128 SECURITY_TYPE security = SECURITY_TYPE.valueOf(mSecurityType); 129 log("Security type is " + security.toString()); 130 switch (security) { 131 // set network configurations 132 case OPEN: 133 config.allowedKeyManagement.set(KeyMgmt.NONE); 134 break; 135 case WEP64: 136 // always use hex pair for WEP-40 137 assertTrue("not a WEP64 security type?", mPassword.length() == 10); 138 config.allowedKeyManagement.set(KeyMgmt.NONE); 139 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 140 config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); 141 config.allowedGroupCiphers.set(GroupCipher.WEP40); 142 if (mPassword != null) { 143 int length = mPassword.length(); 144 // WEP-40 145 if (mPassword.matches("[0-9A-Fa-f]*")) { 146 config.wepKeys[0] = mPassword; 147 } else { 148 fail("Please type hex pair for the password"); 149 } 150 } 151 break; 152 case WEP128: 153 assertNotNull("password is empty", mPassword); 154 // always use hex pair for WEP-104 155 assertTrue("not a WEP128 security type?", mPassword.length() == 26); 156 config.allowedKeyManagement.set(KeyMgmt.NONE); 157 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 158 config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED); 159 config.allowedGroupCiphers.set(GroupCipher.WEP104); 160 if (mPassword != null) { 161 int length = mPassword.length(); 162 // WEP-40 163 if (mPassword.matches("[0-9A-Fa-f]*")) { 164 config.wepKeys[0] = mPassword; 165 } else { 166 fail("Please type hex pair for the password"); 167 } 168 } 169 break; 170 case WPA_TKIP: 171 assertNotNull("missing password", mPassword); 172 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK); 173 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 174 config.allowedProtocols.set(Protocol.WPA); 175 config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP); 176 config.allowedGroupCiphers.set(GroupCipher.TKIP); 177 if (mPassword.matches("[0-9A-Fa-f]{64}")) { 178 config.preSharedKey = mPassword; 179 } else { 180 config.preSharedKey = '"' + mPassword + '"'; 181 } 182 break; 183 case WPA2_AES: 184 assertNotNull("missing password", mPassword); 185 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK); 186 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 187 config.allowedProtocols.set(Protocol.RSN); 188 config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP); 189 config.allowedGroupCiphers.set(GroupCipher.CCMP); 190 config.allowedProtocols.set(Protocol.RSN); 191 if (mPassword.matches("[0-9A-Fa-f]{64}")) { 192 config.preSharedKey = mPassword; 193 } else { 194 config.preSharedKey = '"' + mPassword + '"'; 195 } 196 break; 197 default: 198 fail("Not a valid security type: " + mSecurityType); 199 break; 200 } 201 Log.v(TAG, "network config: " + config.toString()); 202 connectToWifi(config); 203 } 204} 205