1 /* 2 * Copyright (C) 2020 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; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertTrue; 23 24 import android.net.wifi.WifiConfiguration.AuthAlgorithm; 25 import android.net.wifi.WifiConfiguration.GroupCipher; 26 import android.net.wifi.WifiConfiguration.GroupMgmtCipher; 27 import android.net.wifi.WifiConfiguration.KeyMgmt; 28 import android.net.wifi.WifiConfiguration.PairwiseCipher; 29 import android.net.wifi.WifiConfiguration.Protocol; 30 import android.os.Parcel; 31 32 import androidx.test.filters.SmallTest; 33 34 import org.junit.Test; 35 36 import java.util.BitSet; 37 38 /** 39 * Unit tests for {@link android.net.wifi.WifiInfo}. 40 */ 41 @SmallTest 42 public class SecurityParamsTest { 43 verifySecurityParams(SecurityParams params, int expectedSecurityType, int[] expectedAllowedKeyManagement, int[] expectedAllowedProtocols, int[] expectedAllowedAuthAlgorithms, int[] expectedAllowedPairwiseCiphers, int[] expectedAllowedGroupCiphers, boolean expectedRequirePmf)44 private void verifySecurityParams(SecurityParams params, 45 int expectedSecurityType, 46 int[] expectedAllowedKeyManagement, 47 int[] expectedAllowedProtocols, 48 int[] expectedAllowedAuthAlgorithms, 49 int[] expectedAllowedPairwiseCiphers, 50 int[] expectedAllowedGroupCiphers, 51 boolean expectedRequirePmf) { 52 assertTrue(params.isSecurityType(expectedSecurityType)); 53 assertEquals(expectedSecurityType, params.getSecurityType()); 54 for (int b: expectedAllowedKeyManagement) { 55 assertTrue(params.getAllowedKeyManagement().get(b)); 56 } 57 for (int b: expectedAllowedProtocols) { 58 assertTrue(params.getAllowedProtocols().get(b)); 59 } 60 for (int b: expectedAllowedAuthAlgorithms) { 61 assertTrue(params.getAllowedAuthAlgorithms().get(b)); 62 } 63 for (int b: expectedAllowedPairwiseCiphers) { 64 assertTrue(params.getAllowedPairwiseCiphers().get(b)); 65 } 66 for (int b: expectedAllowedGroupCiphers) { 67 assertTrue(params.getAllowedGroupCiphers().get(b)); 68 } 69 assertEquals(expectedRequirePmf, params.isRequirePmf()); 70 } 71 72 /** Verify the security params created by security type. */ 73 @Test testSecurityTypeCreator()74 public void testSecurityTypeCreator() throws Exception { 75 int[] securityTypes = new int[] { 76 WifiConfiguration.SECURITY_TYPE_WAPI_CERT, 77 WifiConfiguration.SECURITY_TYPE_WAPI_PSK, 78 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT, 79 WifiConfiguration.SECURITY_TYPE_OWE, 80 WifiConfiguration.SECURITY_TYPE_SAE, 81 WifiConfiguration.SECURITY_TYPE_OSEN, 82 WifiConfiguration.SECURITY_TYPE_EAP, 83 WifiConfiguration.SECURITY_TYPE_PSK, 84 WifiConfiguration.SECURITY_TYPE_OPEN, 85 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2, 86 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3, 87 }; 88 89 for (int type: securityTypes) { 90 assertEquals(type, 91 SecurityParams.createSecurityParamsBySecurityType(type).getSecurityType()); 92 } 93 } 94 95 /** Verify EAP params creator. */ 96 @Test testEapCreator()97 public void testEapCreator() throws Exception { 98 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP; 99 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; 100 int[] expectedAllowedProtocols = new int[] {}; 101 int[] expectedAllowedAuthAlgorithms = new int[] {}; 102 int[] expectedAllowedPairwiseCiphers = new int[] { 103 PairwiseCipher.CCMP, PairwiseCipher.TKIP, PairwiseCipher.GCMP_256}; 104 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.TKIP, 105 GroupCipher.GCMP_256}; 106 boolean expectedRequirePmf = false; 107 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 108 expectedSecurityType); 109 verifySecurityParams(p, expectedSecurityType, 110 expectedAllowedKeyManagement, expectedAllowedProtocols, 111 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 112 expectedAllowedGroupCiphers, expectedRequirePmf); 113 } 114 115 /** Verify Passpoint R1/R2 params creator. */ 116 @Test testEapPasspointR1R2Creator()117 public void testEapPasspointR1R2Creator() throws Exception { 118 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2; 119 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; 120 int[] expectedAllowedProtocols = new int[] {}; 121 int[] expectedAllowedAuthAlgorithms = new int[] {}; 122 int[] expectedAllowedPairwiseCiphers = new int[] { 123 PairwiseCipher.CCMP, PairwiseCipher.GCMP_256}; 124 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.GCMP_256}; 125 boolean expectedRequirePmf = false; 126 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 127 expectedSecurityType); 128 verifySecurityParams(p, expectedSecurityType, 129 expectedAllowedKeyManagement, expectedAllowedProtocols, 130 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 131 expectedAllowedGroupCiphers, expectedRequirePmf); 132 } 133 134 /** Verify Passpoint R3 params creator. */ 135 @Test testEapPasspointR3Creator()136 public void testEapPasspointR3Creator() throws Exception { 137 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3; 138 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; 139 int[] expectedAllowedProtocols = new int[] {}; 140 int[] expectedAllowedAuthAlgorithms = new int[] {}; 141 int[] expectedAllowedPairwiseCiphers = new int[] { 142 PairwiseCipher.CCMP, PairwiseCipher.GCMP_256}; 143 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.GCMP_256}; 144 boolean expectedRequirePmf = true; 145 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 146 expectedSecurityType); 147 verifySecurityParams(p, expectedSecurityType, 148 expectedAllowedKeyManagement, expectedAllowedProtocols, 149 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 150 expectedAllowedGroupCiphers, expectedRequirePmf); 151 } 152 153 /** Verify Enhanced Open params creator. */ 154 @Test testEnhancedOpenCreator()155 public void testEnhancedOpenCreator() throws Exception { 156 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OWE; 157 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OWE}; 158 int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; 159 int[] expectedAllowedAuthAlgorithms = new int[] {}; 160 int[] expectedAllowedPairwiseCiphers = new int[] { 161 PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; 162 int[] expectedAllowedGroupCiphers = new int[] { 163 GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256}; 164 boolean expectedRequirePmf = true; 165 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 166 expectedSecurityType); 167 verifySecurityParams(p, expectedSecurityType, 168 expectedAllowedKeyManagement, expectedAllowedProtocols, 169 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 170 expectedAllowedGroupCiphers, expectedRequirePmf); 171 } 172 173 /** Verify Open params creator. */ 174 @Test testOpenCreator()175 public void testOpenCreator() throws Exception { 176 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OPEN; 177 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE}; 178 int[] expectedAllowedProtocols = new int[] {}; 179 int[] expectedAllowedAuthAlgorithms = new int[] {}; 180 int[] expectedAllowedPairwiseCiphers = new int[] {}; 181 int[] expectedAllowedGroupCiphers = new int[] {}; 182 boolean expectedRequirePmf = false; 183 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 184 expectedSecurityType); 185 verifySecurityParams(p, expectedSecurityType, 186 expectedAllowedKeyManagement, expectedAllowedProtocols, 187 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 188 expectedAllowedGroupCiphers, expectedRequirePmf); 189 } 190 191 /** Verify OSEN params creator. */ 192 @Test testOsenCreator()193 public void testOsenCreator() throws Exception { 194 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OSEN; 195 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OSEN}; 196 int[] expectedAllowedProtocols = new int[] {Protocol.OSEN}; 197 int[] expectedAllowedAuthAlgorithms = new int[] {}; 198 int[] expectedAllowedPairwiseCiphers = new int[] {}; 199 int[] expectedAllowedGroupCiphers = new int[] {}; 200 boolean expectedRequirePmf = false; 201 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 202 expectedSecurityType); 203 verifySecurityParams(p, expectedSecurityType, 204 expectedAllowedKeyManagement, expectedAllowedProtocols, 205 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 206 expectedAllowedGroupCiphers, expectedRequirePmf); 207 } 208 209 /** Verify WAPI CERT params creator. */ 210 @Test testWapiCertCreator()211 public void testWapiCertCreator() throws Exception { 212 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_CERT; 213 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_CERT}; 214 int[] expectedAllowedProtocols = new int[] {Protocol.WAPI}; 215 int[] expectedAllowedAuthAlgorithms = new int[] {}; 216 int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4}; 217 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4}; 218 boolean expectedRequirePmf = false; 219 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 220 expectedSecurityType); 221 verifySecurityParams(p, expectedSecurityType, 222 expectedAllowedKeyManagement, expectedAllowedProtocols, 223 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 224 expectedAllowedGroupCiphers, expectedRequirePmf); 225 } 226 227 /** Verify WAPI PSK params creator. */ 228 @Test testWapiPskCreator()229 public void testWapiPskCreator() throws Exception { 230 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_PSK; 231 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_PSK}; 232 int[] expectedAllowedProtocols = new int[] {Protocol.WAPI}; 233 int[] expectedAllowedAuthAlgorithms = new int[] {}; 234 int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4}; 235 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4}; 236 boolean expectedRequirePmf = false; 237 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 238 expectedSecurityType); 239 verifySecurityParams(p, expectedSecurityType, 240 expectedAllowedKeyManagement, expectedAllowedProtocols, 241 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 242 expectedAllowedGroupCiphers, expectedRequirePmf); 243 } 244 245 /** Verify WEP params creator. */ 246 @Test testWepCreator()247 public void testWepCreator() throws Exception { 248 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WEP; 249 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE}; 250 int[] expectedAllowedProtocols = new int[] {}; 251 int[] expectedAllowedAuthAlgorithms = new int[] {AuthAlgorithm.OPEN, AuthAlgorithm.SHARED}; 252 int[] expectedAllowedPairwiseCiphers = new int[] {}; 253 int[] expectedAllowedGroupCiphers = new int[] {}; 254 boolean expectedRequirePmf = false; 255 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 256 expectedSecurityType); 257 verifySecurityParams(p, expectedSecurityType, 258 expectedAllowedKeyManagement, expectedAllowedProtocols, 259 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 260 expectedAllowedGroupCiphers, expectedRequirePmf); 261 } 262 263 /** Verify WPA3 Enterprise 192-bit params creator. */ 264 @Test testWpa3Enterprise192BitCreator()265 public void testWpa3Enterprise192BitCreator() throws Exception { 266 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT; 267 int[] expectedAllowedKeyManagement = new int[] { 268 KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X, KeyMgmt.SUITE_B_192}; 269 int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; 270 int[] expectedAllowedAuthAlgorithms = new int[] {}; 271 int[] expectedAllowedPairwiseCiphers = new int[] { 272 PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; 273 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.GCMP_128, GroupCipher.GCMP_256}; 274 boolean expectedRequirePmf = true; 275 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 276 expectedSecurityType); 277 verifySecurityParams(p, expectedSecurityType, 278 expectedAllowedKeyManagement, expectedAllowedProtocols, 279 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 280 expectedAllowedGroupCiphers, expectedRequirePmf); 281 282 assertTrue(p.getAllowedGroupManagementCiphers().get(GroupMgmtCipher.BIP_GMAC_256)); 283 } 284 285 /** Verify WPA3 Enterprise params creator. */ 286 @Test testWpa3EnterpriseCreator()287 public void testWpa3EnterpriseCreator() throws Exception { 288 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE; 289 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X}; 290 int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; 291 int[] expectedAllowedAuthAlgorithms = new int[] {}; 292 int[] expectedAllowedPairwiseCiphers = new int[] { 293 PairwiseCipher.CCMP, PairwiseCipher.GCMP_256}; 294 int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.GCMP_256}; 295 boolean expectedRequirePmf = true; 296 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 297 expectedSecurityType); 298 verifySecurityParams(p, expectedSecurityType, 299 expectedAllowedKeyManagement, expectedAllowedProtocols, 300 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 301 expectedAllowedGroupCiphers, expectedRequirePmf); 302 } 303 304 /** Verify WPA3 Personal params creator. */ 305 @Test testWpa3PersonalCreator()306 public void testWpa3PersonalCreator() throws Exception { 307 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_SAE; 308 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.SAE}; 309 int[] expectedAllowedProtocols = new int[] {Protocol.RSN}; 310 int[] expectedAllowedAuthAlgorithms = new int[] {}; 311 int[] expectedAllowedPairwiseCiphers = new int[] { 312 PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256}; 313 int[] expectedAllowedGroupCiphers = new int[] { 314 GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256}; 315 boolean expectedRequirePmf = true; 316 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 317 expectedSecurityType); 318 verifySecurityParams(p, expectedSecurityType, 319 expectedAllowedKeyManagement, expectedAllowedProtocols, 320 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 321 expectedAllowedGroupCiphers, expectedRequirePmf); 322 } 323 324 /** Verify WPA2 Personal EAP params creator. */ 325 @Test testWpaWpa2PersonalCreator()326 public void testWpaWpa2PersonalCreator() throws Exception { 327 int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PSK; 328 int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_PSK}; 329 int[] expectedAllowedProtocols = new int[] {}; 330 int[] expectedAllowedAuthAlgorithms = new int[] {}; 331 int[] expectedAllowedPairwiseCiphers = new int[] {}; 332 int[] expectedAllowedGroupCiphers = new int[] {}; 333 boolean expectedRequirePmf = false; 334 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 335 expectedSecurityType); 336 verifySecurityParams(p, expectedSecurityType, 337 expectedAllowedKeyManagement, expectedAllowedProtocols, 338 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers, 339 expectedAllowedGroupCiphers, expectedRequirePmf); 340 } 341 342 /** Verify setter/getter methods */ 343 @Test testCommonSetterGetter()344 public void testCommonSetterGetter() throws Exception { 345 SecurityParams params = SecurityParams.createSecurityParamsBySecurityType( 346 WifiConfiguration.SECURITY_TYPE_PSK); 347 348 // PSK setting 349 BitSet allowedKeyManagement = new BitSet(); 350 allowedKeyManagement.set(KeyMgmt.WPA_PSK); 351 352 BitSet allowedProtocols = new BitSet(); 353 allowedProtocols.set(Protocol.RSN); 354 allowedProtocols.set(Protocol.WPA); 355 356 BitSet allowedPairwiseCiphers = new BitSet(); 357 allowedPairwiseCiphers.set(PairwiseCipher.CCMP); 358 allowedPairwiseCiphers.set(PairwiseCipher.TKIP); 359 360 BitSet allowedGroupCiphers = new BitSet(); 361 allowedGroupCiphers.set(GroupCipher.CCMP); 362 allowedGroupCiphers.set(GroupCipher.TKIP); 363 allowedGroupCiphers.set(GroupCipher.WEP40); 364 allowedGroupCiphers.set(GroupCipher.WEP104); 365 366 assertEquals(allowedKeyManagement, params.getAllowedKeyManagement()); 367 assertTrue(params.getAllowedKeyManagement().get(KeyMgmt.WPA_PSK)); 368 369 assertEquals(allowedProtocols, params.getAllowedProtocols()); 370 assertTrue(params.getAllowedProtocols().get(Protocol.RSN)); 371 assertTrue(params.getAllowedProtocols().get(Protocol.WPA)); 372 373 assertEquals(allowedPairwiseCiphers, params.getAllowedPairwiseCiphers()); 374 assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.CCMP)); 375 assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.TKIP)); 376 377 assertEquals(allowedGroupCiphers, params.getAllowedGroupCiphers()); 378 assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.CCMP)); 379 assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.TKIP)); 380 assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP40)); 381 assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP104)); 382 383 params.setEnabled(false); 384 assertFalse(params.isEnabled()); 385 } 386 387 /** Verify SAE-specific methods */ 388 @Test testSaeMethods()389 public void testSaeMethods() throws Exception { 390 SecurityParams p = SecurityParams.createSecurityParamsBySecurityType( 391 WifiConfiguration.SECURITY_TYPE_SAE); 392 393 assertFalse(p.isAddedByAutoUpgrade()); 394 p.setIsAddedByAutoUpgrade(true); 395 assertTrue(p.isAddedByAutoUpgrade()); 396 397 assertFalse(p.isSaeH2eOnlyMode()); 398 p.enableSaeH2eOnlyMode(true); 399 assertTrue(p.isSaeH2eOnlyMode()); 400 401 assertFalse(p.isSaePkOnlyMode()); 402 p.enableSaePkOnlyMode(true); 403 assertTrue(p.isSaePkOnlyMode()); 404 } 405 406 /** Verify copy constructor. */ 407 @Test testCopyConstructor()408 public void testCopyConstructor() throws Exception { 409 SecurityParams params = SecurityParams.createSecurityParamsBySecurityType( 410 WifiConfiguration.SECURITY_TYPE_PSK); 411 params.setEnabled(false); 412 params.setIsAddedByAutoUpgrade(true); 413 414 SecurityParams copiedParams = new SecurityParams(params); 415 416 assertTrue(params.isSameSecurityType(copiedParams)); 417 assertEquals(params.getAllowedKeyManagement(), copiedParams.getAllowedKeyManagement()); 418 assertEquals(params.getAllowedProtocols(), copiedParams.getAllowedProtocols()); 419 assertEquals(params.getAllowedAuthAlgorithms(), copiedParams.getAllowedAuthAlgorithms()); 420 assertEquals(params.getAllowedPairwiseCiphers(), copiedParams.getAllowedPairwiseCiphers()); 421 assertEquals(params.getAllowedGroupCiphers(), copiedParams.getAllowedGroupCiphers()); 422 assertEquals(params.getAllowedGroupManagementCiphers(), 423 copiedParams.getAllowedGroupManagementCiphers()); 424 assertEquals(params.getAllowedSuiteBCiphers(), copiedParams.getAllowedSuiteBCiphers()); 425 assertEquals(params.isRequirePmf(), copiedParams.isRequirePmf()); 426 assertEquals(params.isEnabled(), copiedParams.isEnabled()); 427 assertEquals(params.isSaeH2eOnlyMode(), copiedParams.isSaeH2eOnlyMode()); 428 assertEquals(params.isSaePkOnlyMode(), copiedParams.isSaePkOnlyMode()); 429 assertEquals(params.isAddedByAutoUpgrade(), copiedParams.isAddedByAutoUpgrade()); 430 } 431 432 /** Check that two params are equal if and only if their types are the same. */ 433 @Test testEquals()434 public void testEquals() { 435 SecurityParams saeParams1 = SecurityParams.createSecurityParamsBySecurityType( 436 WifiConfiguration.SECURITY_TYPE_SAE); 437 SecurityParams saeParams2 = SecurityParams.createSecurityParamsBySecurityType( 438 WifiConfiguration.SECURITY_TYPE_SAE); 439 SecurityParams pskParams = SecurityParams.createSecurityParamsBySecurityType( 440 WifiConfiguration.SECURITY_TYPE_PSK); 441 assertEquals(saeParams1, saeParams2); 442 assertNotEquals(saeParams1, pskParams); 443 } 444 445 /** Check that hash values are the same if and only if their types are the same. */ 446 @Test testHashCode()447 public void testHashCode() { 448 SecurityParams saeParams1 = SecurityParams.createSecurityParamsBySecurityType( 449 WifiConfiguration.SECURITY_TYPE_SAE); 450 SecurityParams saeParams2 = SecurityParams.createSecurityParamsBySecurityType( 451 WifiConfiguration.SECURITY_TYPE_SAE); 452 SecurityParams pskParams = SecurityParams.createSecurityParamsBySecurityType( 453 WifiConfiguration.SECURITY_TYPE_PSK); 454 assertEquals(saeParams1.hashCode(), saeParams2.hashCode()); 455 assertNotEquals(saeParams1.hashCode(), pskParams.hashCode()); 456 } 457 458 /** Verify open network check */ 459 @Test testIsOpenNetwork()460 public void testIsOpenNetwork() { 461 SecurityParams[] openSecurityParams = new SecurityParams[] { 462 SecurityParams.createSecurityParamsBySecurityType( 463 WifiConfiguration.SECURITY_TYPE_OWE), 464 SecurityParams.createSecurityParamsBySecurityType( 465 WifiConfiguration.SECURITY_TYPE_OPEN), 466 }; 467 for (SecurityParams p: openSecurityParams) { 468 assertTrue(p.isOpenSecurityType()); 469 } 470 471 SecurityParams[] nonOpenSecurityParams = new SecurityParams[] { 472 SecurityParams.createSecurityParamsBySecurityType( 473 WifiConfiguration.SECURITY_TYPE_EAP), 474 SecurityParams.createSecurityParamsBySecurityType( 475 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2), 476 SecurityParams.createSecurityParamsBySecurityType( 477 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3), 478 SecurityParams.createSecurityParamsBySecurityType( 479 WifiConfiguration.SECURITY_TYPE_OSEN), 480 SecurityParams.createSecurityParamsBySecurityType( 481 WifiConfiguration.SECURITY_TYPE_WAPI_PSK), 482 SecurityParams.createSecurityParamsBySecurityType( 483 WifiConfiguration.SECURITY_TYPE_WAPI_CERT), 484 SecurityParams.createSecurityParamsBySecurityType( 485 WifiConfiguration.SECURITY_TYPE_WEP), 486 SecurityParams.createSecurityParamsBySecurityType( 487 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT), 488 SecurityParams.createSecurityParamsBySecurityType( 489 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE), 490 SecurityParams.createSecurityParamsBySecurityType( 491 WifiConfiguration.SECURITY_TYPE_SAE), 492 SecurityParams.createSecurityParamsBySecurityType( 493 WifiConfiguration.SECURITY_TYPE_PSK), 494 }; 495 for (SecurityParams p: nonOpenSecurityParams) { 496 assertFalse(p.isOpenSecurityType()); 497 } 498 } 499 500 /** Verify enterprise network check */ 501 @Test testIsEnterpriseNetwork()502 public void testIsEnterpriseNetwork() { 503 SecurityParams[] enterpriseSecurityParams = new SecurityParams[] { 504 SecurityParams.createSecurityParamsBySecurityType( 505 WifiConfiguration.SECURITY_TYPE_EAP), 506 SecurityParams.createSecurityParamsBySecurityType( 507 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2), 508 SecurityParams.createSecurityParamsBySecurityType( 509 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3), 510 SecurityParams.createSecurityParamsBySecurityType( 511 WifiConfiguration.SECURITY_TYPE_WAPI_CERT), 512 SecurityParams.createSecurityParamsBySecurityType( 513 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT), 514 SecurityParams.createSecurityParamsBySecurityType( 515 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE), 516 }; 517 for (SecurityParams p: enterpriseSecurityParams) { 518 assertTrue(p.isEnterpriseSecurityType()); 519 } 520 521 SecurityParams[] nonEnterpriseSecurityParams = new SecurityParams[] { 522 SecurityParams.createSecurityParamsBySecurityType( 523 WifiConfiguration.SECURITY_TYPE_OWE), 524 SecurityParams.createSecurityParamsBySecurityType( 525 WifiConfiguration.SECURITY_TYPE_OPEN), 526 SecurityParams.createSecurityParamsBySecurityType( 527 WifiConfiguration.SECURITY_TYPE_OSEN), 528 SecurityParams.createSecurityParamsBySecurityType( 529 WifiConfiguration.SECURITY_TYPE_WAPI_PSK), 530 SecurityParams.createSecurityParamsBySecurityType( 531 WifiConfiguration.SECURITY_TYPE_WEP), 532 SecurityParams.createSecurityParamsBySecurityType( 533 WifiConfiguration.SECURITY_TYPE_SAE), 534 SecurityParams.createSecurityParamsBySecurityType( 535 WifiConfiguration.SECURITY_TYPE_PSK), 536 }; 537 for (SecurityParams p: nonEnterpriseSecurityParams) { 538 assertFalse(p.isEnterpriseSecurityType()); 539 } 540 } 541 542 /** Check that parcel marshalling/unmarshalling works */ 543 @Test testParcelMethods()544 public void testParcelMethods() { 545 SecurityParams params = SecurityParams.createSecurityParamsBySecurityType( 546 WifiConfiguration.SECURITY_TYPE_SAE); 547 548 Parcel parcelW = Parcel.obtain(); 549 params.writeToParcel(parcelW, 0); 550 byte[] bytes = parcelW.marshall(); 551 parcelW.recycle(); 552 553 Parcel parcelR = Parcel.obtain(); 554 parcelR.unmarshall(bytes, 0, bytes.length); 555 parcelR.setDataPosition(0); 556 557 SecurityParams reParams = SecurityParams.CREATOR.createFromParcel(parcelR); 558 assertEquals(params, reParams); 559 } 560 } 561