diff options
author | Jimmy Chen <jimmycmchen@google.com> | 2020-10-13 02:31:22 +0800 |
---|---|---|
committer | Jimmy Chen <jimmycmchen@google.com> | 2020-12-10 16:02:37 +0800 |
commit | 6157d1abdf06bb42604b7b654424191d0cb6dca6 (patch) | |
tree | 671a624fe8afcc45c81680c3bd5b73f18068b164 /wifi/tests | |
parent | 26ca287da728d88709f7523113637104fb31bf3e (diff) |
wifi: add security params class for known security types
Bug: 162685856
Test: atest FrameworksWifiApiTests
Change-Id: I020d338b1829f19f6dde8b1969f64cc065f8d796
Diffstat (limited to 'wifi/tests')
-rw-r--r-- | wifi/tests/src/android/net/wifi/SecurityParamsTest.java | 471 | ||||
-rw-r--r-- | wifi/tests/src/android/net/wifi/WifiConfigurationTest.java | 272 |
2 files changed, 734 insertions, 9 deletions
diff --git a/wifi/tests/src/android/net/wifi/SecurityParamsTest.java b/wifi/tests/src/android/net/wifi/SecurityParamsTest.java new file mode 100644 index 000000000000..e581b77c14ad --- /dev/null +++ b/wifi/tests/src/android/net/wifi/SecurityParamsTest.java @@ -0,0 +1,471 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net.wifi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +import android.net.wifi.WifiConfiguration.AuthAlgorithm; +import android.net.wifi.WifiConfiguration.GroupCipher; +import android.net.wifi.WifiConfiguration.GroupMgmtCipher; +import android.net.wifi.WifiConfiguration.KeyMgmt; +import android.net.wifi.WifiConfiguration.PairwiseCipher; +import android.net.wifi.WifiConfiguration.Protocol; +import android.os.Parcel; + +import androidx.test.filters.SmallTest; + +import org.junit.Test; + +import java.util.BitSet; + +/** + * Unit tests for {@link android.net.wifi.WifiInfo}. + */ +@SmallTest +public class SecurityParamsTest { + + private void verifySecurityParams(SecurityParams params, + int expectedSecurityType, + int[] expectedAllowedKeyManagement, + int[] expectedAllowedProtocols, + int[] expectedAllowedAuthAlgorithms, + int[] expectedAllowedPairwiseCiphers, + int[] expectedAllowedGroupCiphers, + boolean expectedRequirePmf) { + assertTrue(params.isSecurityType(expectedSecurityType)); + for (int b: expectedAllowedKeyManagement) { + assertTrue(params.getAllowedKeyManagement().get(b)); + } + for (int b: expectedAllowedProtocols) { + assertTrue(params.getAllowedProtocols().get(b)); + } + for (int b: expectedAllowedAuthAlgorithms) { + assertTrue(params.getAllowedAuthAlgorithms().get(b)); + } + for (int b: expectedAllowedPairwiseCiphers) { + assertTrue(params.getAllowedPairwiseCiphers().get(b)); + } + for (int b: expectedAllowedGroupCiphers) { + assertTrue(params.getAllowedGroupCiphers().get(b)); + } + assertEquals(expectedRequirePmf, params.isRequirePmf()); + } + + /** Verify EAP params creator. */ + @Test + public void testEapCreator() throws Exception { + SecurityParams p = SecurityParams.createWpaWpa2EnterpriseParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; + int[] expectedAllowedProtocols = new int[] {}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify EAP Passpoint params creator. */ + @Test + public void testEapPasspointCreator() throws Exception { + SecurityParams p = SecurityParams.createPasspointParams(false); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; + int[] expectedAllowedProtocols = new int[] {}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + + p = SecurityParams.createPasspointParams(true); + expectedRequirePmf = true; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify Enhanced Open params creator. */ + @Test + public void testEnhancedOpenCreator() throws Exception { + SecurityParams p = SecurityParams.createEnhancedOpenParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OWE; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OWE}; + int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] { + PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; + int[] expectedAllowedGroupCiphers = new int[] { + GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256}; + boolean expectedRequirePmf = true; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify Open params creator. */ + @Test + public void testOpenCreator() throws Exception { + SecurityParams p = SecurityParams.createOpenParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OPEN; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE}; + int[] expectedAllowedProtocols = new int[] {}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify OSEN params creator. */ + @Test + public void testOsenCreator() throws Exception { + SecurityParams p = SecurityParams.createOsenParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OSEN; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OSEN}; + int[] expectedAllowedProtocols = new int[] {Protocol.OSEN}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WAPI CERT params creator. */ + @Test + public void testWapiCertCreator() throws Exception { + SecurityParams p = SecurityParams.createWapiCertParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_CERT; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_CERT}; + int[] expectedAllowedProtocols = new int[] {Protocol.WAPI}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4}; + int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WAPI PSK params creator. */ + @Test + public void testWapiPskCreator() throws Exception { + SecurityParams p = SecurityParams.createWapiPskParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_PSK; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_PSK}; + int[] expectedAllowedProtocols = new int[] {Protocol.WAPI}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4}; + int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WEP params creator. */ + @Test + public void testWepCreator() throws Exception { + SecurityParams p = SecurityParams.createWepParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WEP; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE}; + int[] expectedAllowedProtocols = new int[] {}; + int[] expectedAllowedAuthAlgorithms = new int[] {AuthAlgorithm.OPEN, AuthAlgorithm.SHARED}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WPA3 Enterprise 192-bit params creator. */ + @Test + public void testWpa3Enterprise192BitCreator() throws Exception { + SecurityParams p = SecurityParams.createWpa3Enterprise192BitParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT; + int[] expectedAllowedKeyManagement = new int[] { + KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X, KeyMgmt.SUITE_B_192}; + int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] { + PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; + int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.GCMP_128, GroupCipher.GCMP_256}; + boolean expectedRequirePmf = true; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + + assertTrue(p.getAllowedGroupManagementCiphers().get(GroupMgmtCipher.BIP_GMAC_256)); + } + + /** Verify WPA3 Enterprise params creator. */ + @Test + public void testWpa3EnterpriseCreator() throws Exception { + SecurityParams p = SecurityParams.createWpa3EnterpriseParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; + int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] { + PairwiseCipher.CCMP, PairwiseCipher.GCMP_256}; + int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.GCMP_256}; + boolean expectedRequirePmf = true; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WPA3 Personal params creator. */ + @Test + public void testWpa3PersonalCreator() throws Exception { + SecurityParams p = SecurityParams.createWpa3PersonalParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_SAE; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.SAE}; + int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] { + PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; + int[] expectedAllowedGroupCiphers = new int[] { + GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256}; + boolean expectedRequirePmf = true; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify WPA2 Personal EAP params creator. */ + @Test + public void testWpaWpa2PersonalCreator() throws Exception { + SecurityParams p = SecurityParams.createWpaWpa2PersonalParams(); + int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PSK; + int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_PSK}; + int[] expectedAllowedProtocols = new int[] {}; + int[] expectedAllowedAuthAlgorithms = new int[] {}; + int[] expectedAllowedPairwiseCiphers = new int[] {}; + int[] expectedAllowedGroupCiphers = new int[] {}; + boolean expectedRequirePmf = false; + verifySecurityParams(p, expectedSecurityType, + expectedAllowedKeyManagement, expectedAllowedProtocols, + expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, + expectedAllowedGroupCiphers, expectedRequirePmf); + } + + /** Verify setter/getter methods */ + @Test + public void testCommonSetterGetter() throws Exception { + SecurityParams params = SecurityParams.createWpaWpa2PersonalParams(); + + // PSK setting + BitSet allowedKeyManagement = new BitSet(); + allowedKeyManagement.set(KeyMgmt.WPA_PSK); + + BitSet allowedProtocols = new BitSet(); + allowedProtocols.set(Protocol.RSN); + allowedProtocols.set(Protocol.WPA); + + BitSet allowedPairwiseCiphers = new BitSet(); + allowedPairwiseCiphers.set(PairwiseCipher.CCMP); + allowedPairwiseCiphers.set(PairwiseCipher.TKIP); + + BitSet allowedGroupCiphers = new BitSet(); + allowedGroupCiphers.set(GroupCipher.CCMP); + allowedGroupCiphers.set(GroupCipher.TKIP); + allowedGroupCiphers.set(GroupCipher.WEP40); + allowedGroupCiphers.set(GroupCipher.WEP104); + + assertEquals(allowedKeyManagement, params.getAllowedKeyManagement()); + assertTrue(params.getAllowedKeyManagement().get(KeyMgmt.WPA_PSK)); + + assertEquals(allowedProtocols, params.getAllowedProtocols()); + assertTrue(params.getAllowedProtocols().get(Protocol.RSN)); + assertTrue(params.getAllowedProtocols().get(Protocol.WPA)); + + assertEquals(allowedPairwiseCiphers, params.getAllowedPairwiseCiphers()); + assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.CCMP)); + assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.TKIP)); + + assertEquals(allowedGroupCiphers, params.getAllowedGroupCiphers()); + assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.CCMP)); + assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.TKIP)); + assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP40)); + assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP104)); + + params.setEnabled(false); + assertFalse(params.isEnabled()); + } + + /** Verify SAE-specific methods */ + @Test + public void testSaeMethods() throws Exception { + SecurityParams p = SecurityParams.createWpa3PersonalParams(); + + assertFalse(p.isAddedByAutoUpgrade()); + p.setIsAddedByAutoUpgrade(true); + assertTrue(p.isAddedByAutoUpgrade()); + + assertFalse(p.isSaeH2eOnlyMode()); + p.enableSaeH2eOnlyMode(true); + assertTrue(p.isSaeH2eOnlyMode()); + + assertFalse(p.isSaePkOnlyMode()); + p.enableSaePkOnlyMode(true); + assertTrue(p.isSaePkOnlyMode()); + } + + /** Verify copy constructor. */ + @Test + public void testCopyConstructor() throws Exception { + SecurityParams params = SecurityParams.createWpaWpa2PersonalParams(); + params.setEnabled(false); + params.setIsAddedByAutoUpgrade(true); + + SecurityParams copiedParams = new SecurityParams(params); + + assertTrue(params.isSameSecurityType(copiedParams)); + assertEquals(params.getAllowedKeyManagement(), copiedParams.getAllowedKeyManagement()); + assertEquals(params.getAllowedProtocols(), copiedParams.getAllowedProtocols()); + assertEquals(params.getAllowedAuthAlgorithms(), copiedParams.getAllowedAuthAlgorithms()); + assertEquals(params.getAllowedPairwiseCiphers(), copiedParams.getAllowedPairwiseCiphers()); + assertEquals(params.getAllowedGroupCiphers(), copiedParams.getAllowedGroupCiphers()); + assertEquals(params.getAllowedGroupManagementCiphers(), + copiedParams.getAllowedGroupManagementCiphers()); + assertEquals(params.getAllowedSuiteBCiphers(), copiedParams.getAllowedSuiteBCiphers()); + assertEquals(params.isRequirePmf(), copiedParams.isRequirePmf()); + assertEquals(params.isEnabled(), copiedParams.isEnabled()); + assertEquals(params.isSaeH2eOnlyMode(), copiedParams.isSaeH2eOnlyMode()); + assertEquals(params.isSaePkOnlyMode(), copiedParams.isSaePkOnlyMode()); + assertEquals(params.isAddedByAutoUpgrade(), copiedParams.isAddedByAutoUpgrade()); + } + + /** Check that two params are equal if and only if their types are the same. */ + @Test + public void testEquals() { + SecurityParams saeParams1 = SecurityParams.createWpa3PersonalParams(); + SecurityParams saeParams2 = SecurityParams.createWpa3PersonalParams(); + SecurityParams pskParams = SecurityParams.createWpaWpa2PersonalParams(); + assertEquals(saeParams1, saeParams2); + assertNotEquals(saeParams1, pskParams); + } + + /** Check that hash values are the same if and only if their types are the same. */ + @Test + public void testHashCode() { + SecurityParams saeParams1 = SecurityParams.createWpa3PersonalParams(); + SecurityParams saeParams2 = SecurityParams.createWpa3PersonalParams(); + SecurityParams pskParams = SecurityParams.createWpaWpa2PersonalParams(); + assertEquals(saeParams1.hashCode(), saeParams2.hashCode()); + assertNotEquals(saeParams1.hashCode(), pskParams.hashCode()); + } + + /** Verify open network check */ + @Test + public void testIsOpenNetwork() { + SecurityParams[] openSecurityParams = new SecurityParams[] { + SecurityParams.createEnhancedOpenParams(), + SecurityParams.createOpenParams(), + }; + for (SecurityParams p: openSecurityParams) { + assertTrue(p.isOpenSecurityType()); + } + + SecurityParams[] nonOpenSecurityParams = new SecurityParams[] { + SecurityParams.createWpaWpa2EnterpriseParams(), + SecurityParams.createPasspointParams(false), + SecurityParams.createOsenParams(), + SecurityParams.createWapiCertParams(), + SecurityParams.createWapiPskParams(), + SecurityParams.createWepParams(), + SecurityParams.createWpa3Enterprise192BitParams(), + SecurityParams.createWpa3EnterpriseParams(), + SecurityParams.createWpa3PersonalParams(), + SecurityParams.createWpaWpa2PersonalParams(), + }; + for (SecurityParams p: nonOpenSecurityParams) { + assertFalse(p.isOpenSecurityType()); + } + } + + /** Verify enterprise network check */ + @Test + public void testIsEnterpriseNetwork() { + SecurityParams[] enterpriseSecurityParams = new SecurityParams[] { + SecurityParams.createWpaWpa2EnterpriseParams(), + SecurityParams.createPasspointParams(false), + SecurityParams.createWapiCertParams(), + SecurityParams.createWpa3Enterprise192BitParams(), + SecurityParams.createWpa3EnterpriseParams(), + }; + for (SecurityParams p: enterpriseSecurityParams) { + assertTrue(p.isEnterpriseSecurityType()); + } + + SecurityParams[] nonEnterpriseSecurityParams = new SecurityParams[] { + SecurityParams.createEnhancedOpenParams(), + SecurityParams.createOpenParams(), + SecurityParams.createOsenParams(), + SecurityParams.createWapiPskParams(), + SecurityParams.createWepParams(), + SecurityParams.createWpa3PersonalParams(), + SecurityParams.createWpaWpa2PersonalParams(), + }; + for (SecurityParams p: nonEnterpriseSecurityParams) { + assertFalse(p.isEnterpriseSecurityType()); + } + } + + /** Check that parcel marshalling/unmarshalling works */ + @Test + public void testParcelMethods() { + SecurityParams params = SecurityParams.createWpa3PersonalParams(); + + Parcel parcelW = Parcel.obtain(); + params.writeToParcel(parcelW, 0); + byte[] bytes = parcelW.marshall(); + parcelW.recycle(); + + Parcel parcelR = Parcel.obtain(); + parcelR.unmarshall(bytes, 0, bytes.length); + parcelR.setDataPosition(0); + + SecurityParams reParams = SecurityParams.createFromParcel(parcelR); + assertEquals(params, reParams); + } +} diff --git a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java index c25374695463..f351e61b5eb8 100644 --- a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java +++ b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java @@ -20,11 +20,13 @@ import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_EAP; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_OPEN; +import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_OSEN; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_OWE; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_PSK; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_SAE; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_WAPI_CERT; import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_WAPI_PSK; +import static android.net.wifi.WifiConfiguration.SECURITY_TYPE_WEP; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -34,9 +36,13 @@ import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertTrue; import android.net.MacAddress; +import android.net.wifi.WifiConfiguration.GroupCipher; import android.net.wifi.WifiConfiguration.KeyMgmt; import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; +import android.net.wifi.WifiConfiguration.PairwiseCipher; +import android.net.wifi.WifiConfiguration.Protocol; import android.os.Parcel; +import android.util.Pair; import androidx.test.filters.SmallTest; @@ -45,6 +51,8 @@ import com.android.net.module.util.MacAddressUtils; import org.junit.Before; import org.junit.Test; +import java.util.List; + /** * Unit tests for {@link android.net.wifi.WifiConfiguration}. */ @@ -187,18 +195,24 @@ public class WifiConfigurationTest { @Test public void testIsOpenNetwork_NotOpen_HasAuthType() { - for (int keyMgmt = 0; keyMgmt < WifiConfiguration.KeyMgmt.strings.length; keyMgmt++) { - if (keyMgmt == WifiConfiguration.KeyMgmt.NONE - || keyMgmt == WifiConfiguration.KeyMgmt.OWE) { - continue; - } + int[] securityTypes = new int [] { + SECURITY_TYPE_WEP, + SECURITY_TYPE_PSK, + SECURITY_TYPE_EAP, + SECURITY_TYPE_SAE, + SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT, + SECURITY_TYPE_WAPI_PSK, + SECURITY_TYPE_WAPI_CERT, + SECURITY_TYPE_EAP_WPA3_ENTERPRISE, + SECURITY_TYPE_OSEN, + }; + for (int type: securityTypes) { WifiConfiguration config = new WifiConfiguration(); - config.allowedKeyManagement.clear(); - config.allowedKeyManagement.set(keyMgmt); + config.setSecurityParams(type); config.wepKeys = null; - assertFalse("Open network reported when key mgmt was set to " - + WifiConfiguration.KeyMgmt.strings[keyMgmt], config.isOpenNetwork()); + assertFalse("Open network reported when security type was set to " + + type, config.isOpenNetwork()); } } @@ -208,6 +222,7 @@ public class WifiConfigurationTest { config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); config.wepKeys = null; + config.convertLegacyFieldsToSecurityParamsIfNeeded(); assertFalse(config.isOpenNetwork()); } @@ -865,4 +880,243 @@ public class WifiConfigurationTest { } return sb.toString(); } + + private void verifyAllowedKeyManagement(WifiConfiguration config, int[] akms) { + for (int akm: akms) { + assertTrue(config.getSecurityParamsList().stream() + .anyMatch(params -> params.getAllowedKeyManagement().get(akm))); + } + } + + private void verifyAllowedProtocols(WifiConfiguration config, int[] aps) { + for (int ap: aps) { + assertTrue(config.getSecurityParamsList().stream() + .anyMatch(params -> params.getAllowedProtocols().get(ap))); + } + } + + private void verifyAllowedPairwiseCiphers(WifiConfiguration config, int[] apcs) { + for (int apc: apcs) { + assertTrue(config.getSecurityParamsList().stream() + .anyMatch(params -> params.getAllowedPairwiseCiphers().get(apc))); + } + } + + private void verifyAllowedGroupCiphers(WifiConfiguration config, int[] agcs) { + for (int agc: agcs) { + assertTrue(config.getSecurityParamsList().stream() + .anyMatch(params -> params.getAllowedGroupCiphers().get(agc))); + } + } + + /** Verify that adding security types works as expected. */ + @Test + public void testAddSecurityTypes() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + config.addSecurityParams(SecurityParams.createWapiPskParams()); + List<SecurityParams> paramsList = config.getSecurityParamsList(); + assertEquals(3, paramsList.size()); + + verifyAllowedKeyManagement(config, new int[] { + KeyMgmt.WPA_PSK, KeyMgmt.SAE, KeyMgmt.WAPI_PSK}); + verifyAllowedProtocols(config, new int[] {Protocol.WPA, Protocol.RSN, Protocol.WAPI}); + verifyAllowedPairwiseCiphers(config, new int[] { + PairwiseCipher.CCMP, PairwiseCipher.TKIP, + PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256, + PairwiseCipher.SMS4}); + verifyAllowedGroupCiphers(config, new int[] { + GroupCipher.CCMP, GroupCipher.TKIP, + GroupCipher.GCMP_128, GroupCipher.GCMP_256, + GroupCipher.SMS4}); + } + + /** Check that a personal security type can be added to a personal configuration. */ + @Test + public void testAddPersonalTypeToPersonalConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + } + + /** Check that an enterprise security type can be added to an enterprise configuration. */ + @Test + public void testAddEnterpriseTypeToEnterpriseConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); + config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); + config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE); + } + + /** Verify that adding an enterprise type to a personal configuration. */ + @Test (expected = IllegalArgumentException.class) + public void testAddEnterpriseTypeToPersonalConfig() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); + } + + /** Verify that adding a personal type to an enterprise configuration. */ + @Test (expected = IllegalArgumentException.class) + public void testAddPersonalTypeToEnterpriseConfig() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); + config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); + config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + } + + /** Check that an open security cannot be added to a non-open configuration. */ + @Test(expected = IllegalArgumentException.class) + public void testAddOpenTypeToNonOpenConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN); + } + + /** Check that a non-open security cannot be added to an open configuration. */ + @Test(expected = IllegalArgumentException.class) + public void testAddNonOpenTypeToOpenConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + } + + /** Check that a OSEN security cannot be added as additional type. */ + @Test(expected = IllegalArgumentException.class) + public void testAddOsenTypeToConfiguration() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_OSEN); + } + + /** Verify that adding duplicate security types raises the exception. */ + @Test (expected = IllegalArgumentException.class) + public void testAddDuplicateSecurityTypes() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + } + + /** Verify that adding duplicate security params raises the exception. */ + @Test (expected = IllegalArgumentException.class) + public void testAddDuplicateSecurityParams() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(SecurityParams.createWpaWpa2PersonalParams()); + config.addSecurityParams(SecurityParams.createWpaWpa2PersonalParams()); + } + + /** Verify that Suite-B type works as expected. */ + @Test + public void testAddSuiteBSecurityType() { + WifiConfiguration config = new WifiConfiguration(); + config.addSecurityParams(SecurityParams.createWpa3EnterpriseParams()); + config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); + config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE); + config.addSecurityParams(SecurityParams.createWpa3Enterprise192BitParams()); + + assertFalse(config.isSuiteBCipherEcdheRsaEnabled()); + config.enableSuiteBCiphers(false, true); + assertTrue(config.isSuiteBCipherEcdheRsaEnabled()); + } + + /** Verify that FILS bit can be set correctly. */ + @Test + public void testFilsKeyMgmt() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + + config.enableFils(false, true); + assertFalse(config.isFilsSha256Enabled()); + assertTrue(config.isFilsSha384Enabled()); + } + + /** Verify that SAE mode can be configured correctly. */ + @Test + public void testSaeTypeMethods() { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK); + config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + + SecurityParams saeParams = config.getSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + assertNotNull(saeParams); + assertFalse(saeParams.isSaeH2eOnlyMode()); + assertFalse(saeParams.isSaePkOnlyMode()); + + config.enableSaeH2eOnlyMode(true); + config.enableSaePkOnlyMode(true); + + saeParams = config.getSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE); + assertNotNull(saeParams); + assertTrue(saeParams.isSaeH2eOnlyMode()); + assertTrue(saeParams.isSaePkOnlyMode()); + } + + /** Verify the legacy configuration conversion */ + @Test + public void testLegacyConfigurationConversion() { + Pair[] keyMgmtSecurityTypePairs = new Pair[] { + new Pair<>(KeyMgmt.WAPI_CERT, SECURITY_TYPE_WAPI_CERT), + new Pair<>(KeyMgmt.WAPI_PSK, SECURITY_TYPE_WAPI_PSK), + new Pair<>(KeyMgmt.SUITE_B_192, SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT), + new Pair<>(KeyMgmt.OWE, SECURITY_TYPE_OWE), + new Pair<>(KeyMgmt.SAE, SECURITY_TYPE_SAE), + new Pair<>(KeyMgmt.OSEN, SECURITY_TYPE_OSEN), + new Pair<>(KeyMgmt.WPA2_PSK, SECURITY_TYPE_PSK), + new Pair<>(KeyMgmt.WPA_EAP, SECURITY_TYPE_EAP), + new Pair<>(KeyMgmt.WPA_PSK, SECURITY_TYPE_PSK), + new Pair<>(KeyMgmt.NONE, SECURITY_TYPE_OPEN), + }; + + for (Pair pair: keyMgmtSecurityTypePairs) { + WifiConfiguration config = new WifiConfiguration(); + config.allowedKeyManagement.set((int) pair.first); + config.convertLegacyFieldsToSecurityParamsIfNeeded(); + assertNotNull(config.getSecurityParams((int) pair.second)); + } + + // If none of key management is set, it should be open. + WifiConfiguration emptyConfig = new WifiConfiguration(); + emptyConfig.convertLegacyFieldsToSecurityParamsIfNeeded(); + assertNotNull(emptyConfig.getSecurityParams(SECURITY_TYPE_OPEN)); + + // If EAP key management is set and requirePmf is true, it is WPA3 Enterprise. + WifiConfiguration wpa3EnterpriseConfig = new WifiConfiguration(); + wpa3EnterpriseConfig.allowedKeyManagement.set(KeyMgmt.WPA_EAP); + wpa3EnterpriseConfig.requirePmf = true; + wpa3EnterpriseConfig.convertLegacyFieldsToSecurityParamsIfNeeded(); + assertNotNull(wpa3EnterpriseConfig.getSecurityParams(SECURITY_TYPE_EAP_WPA3_ENTERPRISE)); + + // If key management is NONE and wep key is set, it is WEP type. + WifiConfiguration wepConfig = new WifiConfiguration(); + wepConfig.allowedKeyManagement.set(KeyMgmt.NONE); + wepConfig.wepKeys = new String[] {"\"abcdef\""}; + wepConfig.convertLegacyFieldsToSecurityParamsIfNeeded(); + assertNotNull(wepConfig.getSecurityParams(SECURITY_TYPE_WEP)); + } + + /** Verify the set security params by SecurityParams objects. */ + @Test + public void testSetBySecurityParamsObject() { + Pair[] securityParamsSecurityTypePairs = new Pair[] { + new Pair<>(SecurityParams.createWapiCertParams(), SECURITY_TYPE_WAPI_CERT), + new Pair<>(SecurityParams.createWapiPskParams(), SECURITY_TYPE_WAPI_PSK), + new Pair<>(SecurityParams.createWpa3Enterprise192BitParams(), + SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT), + new Pair<>(SecurityParams.createEnhancedOpenParams(), SECURITY_TYPE_OWE), + new Pair<>(SecurityParams.createWpa3PersonalParams(), SECURITY_TYPE_SAE), + new Pair<>(SecurityParams.createOsenParams(), SECURITY_TYPE_OSEN), + new Pair<>(SecurityParams.createWpaWpa2EnterpriseParams(), SECURITY_TYPE_EAP), + new Pair<>(SecurityParams.createWpaWpa2PersonalParams(), SECURITY_TYPE_PSK), + new Pair<>(SecurityParams.createOpenParams(), SECURITY_TYPE_OPEN), + }; + for (Pair pair: securityParamsSecurityTypePairs) { + WifiConfiguration config = new WifiConfiguration(); + config.setSecurityParams((SecurityParams) pair.first); + assertNotNull(config.getSecurityParams((int) pair.second)); + } + } } |