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 17 package com.android.connectivitymanagertest.functional; 18 19 import android.net.wifi.WifiConfiguration; 20 import android.net.wifi.WifiConfiguration.AuthAlgorithm; 21 import android.net.wifi.WifiConfiguration.GroupCipher; 22 import android.net.wifi.WifiConfiguration.PairwiseCipher; 23 import android.net.wifi.WifiConfiguration.Protocol; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.os.Bundle; 27 import android.test.suitebuilder.annotation.LargeTest; 28 29 import com.android.connectivitymanagertest.ConnectivityManagerTestBase; 30 import com.android.connectivitymanagertest.WifiAssociationTestRunner; 31 import com.android.connectivitymanagertest.WifiConfigurationHelper; 32 33 /** 34 * Test Wi-Fi connection with different configuration 35 * To run this tests: 36 * * adb shell am instrument -e ssid <ssid> -e password <password> \ 37 * -e security-type [OPEN|WEP64|WEP128|WPA_TKIP|WPA2_AES] -e frequency-band [2.4|5.0|auto] 38 * -w com.android.connectivitymanagertest/.WifiAssociationTestRunner" 39 */ 40 public class WifiAssociationTest extends ConnectivityManagerTestBase { 41 private enum SecurityType { 42 OPEN, WEP64, WEP128, WPA_TKIP, WPA2_AES 43 } 44 45 public WifiAssociationTest() { 46 super(WifiAssociationTest.class.getSimpleName()); 47 } 48 49 /** 50 * Test that the wifi can associate with a given access point. 51 */ 52 @LargeTest 53 public void testWifiAssociation() { 54 WifiAssociationTestRunner runner = (WifiAssociationTestRunner) getInstrumentation(); 55 Bundle arguments = runner.getArguments(); 56 57 String ssid = arguments.getString("ssid"); 58 assertNotNull("ssid is empty", ssid); 59 60 String securityTypeStr = arguments.getString("security-type"); 61 assertNotNull("security-type is empty", securityTypeStr); 62 SecurityType securityType = SecurityType.valueOf(securityTypeStr); 63 64 String password = arguments.getString("password"); 65 66 assertTrue("enable Wifi failed", enableWifi()); 67 WifiInfo wi = mWifiManager.getConnectionInfo(); 68 logv("%s", wi); 69 assertNotNull("no active wifi info", wi); 70 71 WifiConfiguration config = getConfig(ssid, securityType, password); 72 73 logv("Network config: %s", config.toString()); 74 connectToWifi(config); 75 } 76 77 /** 78 * Get the {@link WifiConfiguration} based on ssid, security, and password. 79 */ 80 private WifiConfiguration getConfig(String ssid, SecurityType securityType, String password) { 81 logv("Security type is %s", securityType.toString()); 82 83 WifiConfiguration config = null; 84 switch (securityType) { 85 case OPEN: 86 config = WifiConfigurationHelper.createOpenConfig(ssid); 87 break; 88 case WEP64: 89 assertNotNull("password is empty", password); 90 // always use hex pair for WEP-40 91 assertTrue(WifiConfigurationHelper.isHex(password, 10)); 92 config = WifiConfigurationHelper.createWepConfig(ssid, password); 93 config.allowedGroupCiphers.set(GroupCipher.WEP40); 94 break; 95 case WEP128: 96 assertNotNull("password is empty", password); 97 // always use hex pair for WEP-104 98 assertTrue(WifiConfigurationHelper.isHex(password, 26)); 99 config = WifiConfigurationHelper.createWepConfig(ssid, password); 100 config.allowedGroupCiphers.set(GroupCipher.WEP104); 101 break; 102 case WPA_TKIP: 103 assertNotNull("password is empty", password); 104 config = WifiConfigurationHelper.createPskConfig(ssid, password); 105 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 106 config.allowedProtocols.set(Protocol.WPA); 107 config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP); 108 config.allowedGroupCiphers.set(GroupCipher.TKIP); 109 break; 110 case WPA2_AES: 111 assertNotNull("password is empty", password); 112 config = WifiConfigurationHelper.createPskConfig(ssid, password); 113 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 114 config.allowedProtocols.set(Protocol.RSN); 115 config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP); 116 config.allowedGroupCiphers.set(GroupCipher.CCMP); 117 break; 118 default: 119 fail("Not a valid security type: " + securityType); 120 break; 121 } 122 return config; 123 } 124 } 125