blob: 68f3179f96c127fc2ed6feea9d3d21e8f6b30ccc [file] [log] [blame]
Xia Wang58a5db32013-05-02 21:19:40 -07001/*
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
Guang Zhud21f1c12014-08-18 16:18:00 -070019import android.net.wifi.WifiConfiguration;
20import android.net.wifi.WifiConfiguration.AuthAlgorithm;
21import android.net.wifi.WifiConfiguration.GroupCipher;
Guang Zhud21f1c12014-08-18 16:18:00 -070022import android.net.wifi.WifiConfiguration.PairwiseCipher;
23import android.net.wifi.WifiConfiguration.Protocol;
24import android.net.wifi.WifiInfo;
Eric Rowe485ceb82014-09-11 14:14:18 -070025import android.net.wifi.WifiManager;
Guang Zhud21f1c12014-08-18 16:18:00 -070026import android.os.Bundle;
Xia Wang58a5db32013-05-02 21:19:40 -070027import android.test.suitebuilder.annotation.LargeTest;
Xia Wang58a5db32013-05-02 21:19:40 -070028
Guang Zhud21f1c12014-08-18 16:18:00 -070029import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
30import com.android.connectivitymanagertest.WifiAssociationTestRunner;
Eric Rowe2591e002014-09-03 09:49:18 -070031import com.android.connectivitymanagertest.WifiConfigurationHelper;
Guang Zhud21f1c12014-08-18 16:18:00 -070032
Xia Wang58a5db32013-05-02 21:19:40 -070033/**
34 * Test Wi-Fi connection with different configuration
35 * To run this tests:
Xia Wang1c6911a2013-05-21 14:18:56 -070036 * * 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 Wang58a5db32013-05-02 21:19:40 -070039 */
Guang Zhud21f1c12014-08-18 16:18:00 -070040public class WifiAssociationTest extends ConnectivityManagerTestBase {
Eric Rowe485ceb82014-09-11 14:14:18 -070041 private enum SecurityType {
Xia Wang58a5db32013-05-02 21:19:40 -070042 OPEN, WEP64, WEP128, WPA_TKIP, WPA2_AES
Xia Wang58a5db32013-05-02 21:19:40 -070043 }
44
Eric Rowe2591e002014-09-03 09:49:18 -070045 public WifiAssociationTest() {
46 super(WifiAssociationTest.class.getSimpleName());
47 }
48
Eric Rowe485ceb82014-09-11 14:14:18 -070049 /**
50 * Test that the wifi can associate with a given access point.
51 */
Xia Wang58a5db32013-05-02 21:19:40 -070052 @LargeTest
53 public void testWifiAssociation() {
Eric Rowe485ceb82014-09-11 14:14:18 -070054 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 Rowe2591e002014-09-03 09:49:18 -0700110 WifiConfiguration config = null;
Eric Rowe485ceb82014-09-11 14:14:18 -0700111 switch (securityType) {
Xia Wang58a5db32013-05-02 21:19:40 -0700112 case OPEN:
Eric Rowe485ceb82014-09-11 14:14:18 -0700113 config = WifiConfigurationHelper.createOpenConfig(ssid);
Xia Wang58a5db32013-05-02 21:19:40 -0700114 break;
115 case WEP64:
Eric Rowe485ceb82014-09-11 14:14:18 -0700116 assertNotNull("password is empty", password);
Eric Rowe2591e002014-09-03 09:49:18 -0700117 // always use hex pair for WEP-40
Eric Rowe485ceb82014-09-11 14:14:18 -0700118 assertTrue(WifiConfigurationHelper.isHex(password, 10));
119 config = WifiConfigurationHelper.createWepConfig(ssid, password);
Xia Wang58a5db32013-05-02 21:19:40 -0700120 config.allowedGroupCiphers.set(GroupCipher.WEP40);
Xia Wang58a5db32013-05-02 21:19:40 -0700121 break;
122 case WEP128:
Eric Rowe485ceb82014-09-11 14:14:18 -0700123 assertNotNull("password is empty", password);
Eric Rowe2591e002014-09-03 09:49:18 -0700124 // always use hex pair for WEP-104
Eric Rowe485ceb82014-09-11 14:14:18 -0700125 assertTrue(WifiConfigurationHelper.isHex(password, 26));
126 config = WifiConfigurationHelper.createWepConfig(ssid, password);
Xia Wang58a5db32013-05-02 21:19:40 -0700127 config.allowedGroupCiphers.set(GroupCipher.WEP104);
Xia Wang58a5db32013-05-02 21:19:40 -0700128 break;
129 case WPA_TKIP:
Eric Rowe485ceb82014-09-11 14:14:18 -0700130 assertNotNull("password is empty", password);
131 config = WifiConfigurationHelper.createPskConfig(ssid, password);
Xia Wang58a5db32013-05-02 21:19:40 -0700132 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
133 config.allowedProtocols.set(Protocol.WPA);
134 config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
135 config.allowedGroupCiphers.set(GroupCipher.TKIP);
Xia Wang58a5db32013-05-02 21:19:40 -0700136 break;
137 case WPA2_AES:
Eric Rowe485ceb82014-09-11 14:14:18 -0700138 assertNotNull("password is empty", password);
139 config = WifiConfigurationHelper.createPskConfig(ssid, password);
Xia Wang58a5db32013-05-02 21:19:40 -0700140 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
141 config.allowedProtocols.set(Protocol.RSN);
142 config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
143 config.allowedGroupCiphers.set(GroupCipher.CCMP);
Xia Wang58a5db32013-05-02 21:19:40 -0700144 break;
145 default:
Eric Rowe485ceb82014-09-11 14:14:18 -0700146 fail("Not a valid security type: " + securityType);
Xia Wang58a5db32013-05-02 21:19:40 -0700147 break;
148 }
Eric Rowe485ceb82014-09-11 14:14:18 -0700149 return config;
Xia Wang58a5db32013-05-02 21:19:40 -0700150 }
151}