Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 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 | |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 19 | import android.net.wifi.WifiConfiguration; |
| 20 | import android.net.wifi.WifiConfiguration.AuthAlgorithm; |
| 21 | import android.net.wifi.WifiConfiguration.GroupCipher; |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 22 | import android.net.wifi.WifiConfiguration.PairwiseCipher; |
| 23 | import android.net.wifi.WifiConfiguration.Protocol; |
| 24 | import android.net.wifi.WifiInfo; |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 25 | import android.net.wifi.WifiManager; |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 27 | import android.test.suitebuilder.annotation.LargeTest; |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 28 | |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 29 | import com.android.connectivitymanagertest.ConnectivityManagerTestBase; |
| 30 | import com.android.connectivitymanagertest.WifiAssociationTestRunner; |
Eric Rowe | 2591e00 | 2014-09-03 09:49:18 -0700 | [diff] [blame] | 31 | import com.android.connectivitymanagertest.WifiConfigurationHelper; |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 32 | |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Test Wi-Fi connection with different configuration |
| 35 | * To run this tests: |
Xia Wang | 1c6911a | 2013-05-21 14:18:56 -0700 | [diff] [blame] | 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" |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 39 | */ |
Guang Zhu | d21f1c1 | 2014-08-18 16:18:00 -0700 | [diff] [blame] | 40 | public class WifiAssociationTest extends ConnectivityManagerTestBase { |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 41 | private enum SecurityType { |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 42 | OPEN, WEP64, WEP128, WPA_TKIP, WPA2_AES |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Eric Rowe | 2591e00 | 2014-09-03 09:49:18 -0700 | [diff] [blame] | 45 | public WifiAssociationTest() { |
| 46 | super(WifiAssociationTest.class.getSimpleName()); |
| 47 | } |
| 48 | |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Test that the wifi can associate with a given access point. |
| 51 | */ |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 52 | @LargeTest |
| 53 | public void testWifiAssociation() { |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 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 | String freqStr = arguments.getString("frequency-band"); |
| 67 | if (freqStr != null) { |
| 68 | setFrequencyBand(freqStr); |
| 69 | } |
| 70 | |
| 71 | assertTrue("enable Wifi failed", enableWifi()); |
| 72 | WifiInfo wi = mWifiManager.getConnectionInfo(); |
| 73 | logv("%s", wi); |
| 74 | assertNotNull("no active wifi info", wi); |
| 75 | |
| 76 | WifiConfiguration config = getConfig(ssid, securityType, password); |
| 77 | |
| 78 | logv("Network config: %s", config.toString()); |
| 79 | connectToWifi(config); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the frequency band and verify that it has been set. |
| 84 | */ |
| 85 | private void setFrequencyBand(String frequencyBandStr) { |
| 86 | int frequencyBand = -1; |
| 87 | if ("2.4".equals(frequencyBandStr)) { |
| 88 | frequencyBand = WifiManager.WIFI_FREQUENCY_BAND_2GHZ; |
| 89 | } else if ("5.0".equals(frequencyBandStr)) { |
| 90 | frequencyBand = WifiManager.WIFI_FREQUENCY_BAND_5GHZ; |
| 91 | } else if ("auto".equals(frequencyBandStr)) { |
| 92 | frequencyBand = WifiManager.WIFI_FREQUENCY_BAND_AUTO; |
| 93 | } else { |
| 94 | fail("Invalid frequency-band"); |
| 95 | } |
| 96 | if (mWifiManager.getFrequencyBand() != frequencyBand) { |
| 97 | logv("Set frequency band to %s", frequencyBandStr); |
| 98 | mWifiManager.setFrequencyBand(frequencyBand, true); |
| 99 | } |
| 100 | assertEquals("Specified frequency band does not match operational band", |
| 101 | frequencyBand, mWifiManager.getFrequencyBand()); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the {@link WifiConfiguration} based on ssid, security, and password. |
| 106 | */ |
| 107 | private WifiConfiguration getConfig(String ssid, SecurityType securityType, String password) { |
| 108 | logv("Security type is %s", securityType.toString()); |
| 109 | |
Eric Rowe | 2591e00 | 2014-09-03 09:49:18 -0700 | [diff] [blame] | 110 | WifiConfiguration config = null; |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 111 | switch (securityType) { |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 112 | case OPEN: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 113 | config = WifiConfigurationHelper.createOpenConfig(ssid); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 114 | break; |
| 115 | case WEP64: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 116 | assertNotNull("password is empty", password); |
Eric Rowe | 2591e00 | 2014-09-03 09:49:18 -0700 | [diff] [blame] | 117 | // always use hex pair for WEP-40 |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 118 | assertTrue(WifiConfigurationHelper.isHex(password, 10)); |
| 119 | config = WifiConfigurationHelper.createWepConfig(ssid, password); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 120 | config.allowedGroupCiphers.set(GroupCipher.WEP40); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 121 | break; |
| 122 | case WEP128: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 123 | assertNotNull("password is empty", password); |
Eric Rowe | 2591e00 | 2014-09-03 09:49:18 -0700 | [diff] [blame] | 124 | // always use hex pair for WEP-104 |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 125 | assertTrue(WifiConfigurationHelper.isHex(password, 26)); |
| 126 | config = WifiConfigurationHelper.createWepConfig(ssid, password); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 127 | config.allowedGroupCiphers.set(GroupCipher.WEP104); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 128 | break; |
| 129 | case WPA_TKIP: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 130 | assertNotNull("password is empty", password); |
| 131 | config = WifiConfigurationHelper.createPskConfig(ssid, password); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 132 | config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); |
| 133 | config.allowedProtocols.set(Protocol.WPA); |
| 134 | config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP); |
| 135 | config.allowedGroupCiphers.set(GroupCipher.TKIP); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 136 | break; |
| 137 | case WPA2_AES: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 138 | assertNotNull("password is empty", password); |
| 139 | config = WifiConfigurationHelper.createPskConfig(ssid, password); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 140 | config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); |
| 141 | config.allowedProtocols.set(Protocol.RSN); |
| 142 | config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP); |
| 143 | config.allowedGroupCiphers.set(GroupCipher.CCMP); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 144 | break; |
| 145 | default: |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 146 | fail("Not a valid security type: " + securityType); |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
Eric Rowe | 485ceb8 | 2014-09-11 14:14:18 -0700 | [diff] [blame] | 149 | return config; |
Xia Wang | 58a5db3 | 2013-05-02 21:19:40 -0700 | [diff] [blame] | 150 | } |
| 151 | } |