Home | History | Annotate | Download | only in functional
      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         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 
    110         WifiConfiguration config = null;
    111         switch (securityType) {
    112             case OPEN:
    113                 config = WifiConfigurationHelper.createOpenConfig(ssid);
    114                 break;
    115             case WEP64:
    116                 assertNotNull("password is empty", password);
    117                 // always use hex pair for WEP-40
    118                 assertTrue(WifiConfigurationHelper.isHex(password, 10));
    119                 config = WifiConfigurationHelper.createWepConfig(ssid, password);
    120                 config.allowedGroupCiphers.set(GroupCipher.WEP40);
    121                 break;
    122             case WEP128:
    123                 assertNotNull("password is empty", password);
    124                 // always use hex pair for WEP-104
    125                 assertTrue(WifiConfigurationHelper.isHex(password, 26));
    126                 config = WifiConfigurationHelper.createWepConfig(ssid, password);
    127                 config.allowedGroupCiphers.set(GroupCipher.WEP104);
    128                 break;
    129             case WPA_TKIP:
    130                 assertNotNull("password is empty", password);
    131                 config = WifiConfigurationHelper.createPskConfig(ssid, password);
    132                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
    133                 config.allowedProtocols.set(Protocol.WPA);
    134                 config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
    135                 config.allowedGroupCiphers.set(GroupCipher.TKIP);
    136                 break;
    137             case WPA2_AES:
    138                 assertNotNull("password is empty", password);
    139                 config = WifiConfigurationHelper.createPskConfig(ssid, password);
    140                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
    141                 config.allowedProtocols.set(Protocol.RSN);
    142                 config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
    143                 config.allowedGroupCiphers.set(GroupCipher.CCMP);
    144                 break;
    145             default:
    146                 fail("Not a valid security type: " + securityType);
    147                 break;
    148         }
    149         return config;
    150     }
    151 }
    152