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 android.net.wifi.cts; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.net.wifi.WifiConfiguration; 22 import android.net.wifi.WifiEnterpriseConfig; 23 import android.net.wifi.WifiEnterpriseConfig.Eap; 24 import android.net.wifi.WifiEnterpriseConfig.Phase2; 25 import android.net.wifi.WifiManager; 26 import android.test.AndroidTestCase; 27 28 public class WifiEnterpriseConfigTest extends AndroidTestCase { 29 private WifiManager mWifiManager; 30 31 private static final String SSID = "\"TestSSID\""; 32 private static final String IDENTITY = "identity"; 33 private static final String PASSWORD = "password"; 34 private static final String SUBJECT_MATCH = "subjectmatch"; 35 private static final String ANON_IDENTITY = "anonidentity"; 36 private static final int ENABLE_DELAY = 10000; 37 38 private boolean hasWifi() { 39 return getContext().getPackageManager().hasSystemFeature( 40 PackageManager.FEATURE_WIFI); 41 } 42 43 @Override 44 protected void setUp() throws Exception { 45 super.setUp(); 46 mWifiManager = (WifiManager) mContext 47 .getSystemService(Context.WIFI_SERVICE); 48 assertNotNull(mWifiManager); 49 mWifiManager.setWifiEnabled(true); 50 Thread.sleep(ENABLE_DELAY); 51 if (hasWifi()) { 52 assertTrue(mWifiManager.isWifiEnabled()); 53 } 54 } 55 56 public void testSettersAndGetters() { 57 if (!hasWifi()) { 58 return; 59 } 60 61 WifiEnterpriseConfig config = new WifiEnterpriseConfig(); 62 assertTrue(config.getEapMethod() == Eap.NONE); 63 config.setEapMethod(Eap.PEAP); 64 assertTrue(config.getEapMethod() == Eap.PEAP); 65 config.setEapMethod(Eap.PWD); 66 assertTrue(config.getEapMethod() == Eap.PWD); 67 config.setEapMethod(Eap.TLS); 68 assertTrue(config.getEapMethod() == Eap.TLS); 69 config.setEapMethod(Eap.TTLS); 70 assertTrue(config.getEapMethod() == Eap.TTLS); 71 assertTrue(config.getPhase2Method() == Phase2.NONE); 72 config.setPhase2Method(Phase2.PAP); 73 assertTrue(config.getPhase2Method() == Phase2.PAP); 74 config.setPhase2Method(Phase2.MSCHAP); 75 assertTrue(config.getPhase2Method() == Phase2.MSCHAP); 76 config.setPhase2Method(Phase2.MSCHAPV2); 77 assertTrue(config.getPhase2Method() == Phase2.MSCHAPV2); 78 config.setPhase2Method(Phase2.GTC); 79 assertTrue(config.getPhase2Method() == Phase2.GTC); 80 config.setIdentity(IDENTITY); 81 assertTrue(config.getIdentity().equals(IDENTITY)); 82 config.setAnonymousIdentity(ANON_IDENTITY); 83 assertTrue(config.getAnonymousIdentity().equals(ANON_IDENTITY)); 84 config.setPassword(PASSWORD); 85 assertTrue(config.getPassword().equals(PASSWORD)); 86 config.setCaCertificate(null); 87 config.setClientKeyEntry(null, null); 88 config.setSubjectMatch(SUBJECT_MATCH); 89 assertTrue(config.getSubjectMatch().equals(SUBJECT_MATCH)); 90 } 91 92 public void testAddEapNetwork() { 93 if (!hasWifi()) { 94 return; 95 } 96 97 WifiConfiguration config = new WifiConfiguration(); 98 WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); 99 enterpriseConfig.setEapMethod(Eap.PWD); 100 enterpriseConfig.setIdentity(IDENTITY); 101 enterpriseConfig.setPassword(PASSWORD); 102 config.SSID = SSID; 103 config.enterpriseConfig = enterpriseConfig; 104 105 int netId = mWifiManager.addNetwork(config); 106 assertTrue(doesSsidExist(SSID)); 107 mWifiManager.removeNetwork(netId); 108 assertFalse(doesSsidExist(SSID)); 109 } 110 111 private boolean doesSsidExist(String ssid) { 112 for (final WifiConfiguration w : mWifiManager.getConfiguredNetworks()) { 113 if (w.SSID.equals(ssid)) 114 return true; 115 } 116 return false; 117 } 118 } 119