1 /* 2 * Copyright (C) 2016 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.server.wifi; 18 19 import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID; 20 import static android.net.wifi.WifiEnterpriseConfig.OCSP_NONE; 21 import static android.net.wifi.WifiEnterpriseConfig.OCSP_REQUIRE_CERT_STATUS; 22 23 import static com.android.server.wifi.WifiConfigurationUtil.addSecurityTypeToNetworkId; 24 import static com.android.server.wifi.WifiConfigurationUtil.convertWifiInfoSecurityTypeToWifiConfiguration; 25 26 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertFalse; 28 import static org.junit.Assert.assertTrue; 29 30 import android.content.pm.UserInfo; 31 import android.net.IpConfiguration; 32 import android.net.MacAddress; 33 import android.net.wifi.ScanResult; 34 import android.net.wifi.SecurityParams; 35 import android.net.wifi.WifiConfiguration; 36 import android.net.wifi.WifiEnterpriseConfig; 37 import android.net.wifi.WifiInfo; 38 import android.net.wifi.WifiManager; 39 import android.net.wifi.WifiNetworkSpecifier; 40 import android.net.wifi.WifiScanner; 41 import android.os.PatternMatcher; 42 import android.util.Pair; 43 44 import androidx.test.filters.SmallTest; 45 46 import com.android.modules.utils.build.SdkLevel; 47 48 import org.junit.Test; 49 50 import java.security.cert.X509Certificate; 51 import java.util.ArrayList; 52 import java.util.Arrays; 53 import java.util.Collections; 54 import java.util.List; 55 56 /** 57 * Unit tests for {@link com.android.server.wifi.WifiConfigurationUtil}. 58 */ 59 @SmallTest 60 public class WifiConfigurationUtilTest extends WifiBaseTest { 61 static final int CURRENT_USER_ID = 0; 62 static final int CURRENT_USER_MANAGED_PROFILE_USER_ID = 10; 63 static final int OTHER_USER_ID = 11; 64 static final int TEST_UID = 10000; 65 static final String TEST_PACKAGE = "com.test"; 66 static final String TEST_SSID = "\"test_ssid\""; 67 static final String TEST_SSID_1 = "\"test_ssid_1\""; 68 static final String TEST_BSSID = "aa:aa:11:22:cc:dd"; 69 static final String TEST_BSSID_1 = "11:22:11:22:cc:dd"; 70 static final List<UserInfo> PROFILES = Arrays.asList( 71 new UserInfo(CURRENT_USER_ID, "owner", 0), 72 new UserInfo(CURRENT_USER_MANAGED_PROFILE_USER_ID, "managed profile", 0)); 73 74 /** 75 * Verify that new WifiEnterpriseConfig is detected. 76 */ 77 @Test testEnterpriseConfigAdded()78 public void testEnterpriseConfigAdded() { 79 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 80 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 81 .setIdentity("username", "password") 82 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 83 84 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged( 85 null, eapConfig.enterpriseConfig)); 86 } 87 88 /** 89 * Verify WifiEnterpriseConfig eap change is detected. 90 */ 91 @Test testEnterpriseConfigEapChangeDetected()92 public void testEnterpriseConfigEapChangeDetected() { 93 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS); 94 EnterpriseConfig peapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.PEAP); 95 96 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 97 peapConfig.enterpriseConfig)); 98 } 99 100 /** 101 * Verify WifiEnterpriseConfig phase2 method change is detected. 102 */ 103 @Test testEnterpriseConfigPhase2ChangeDetected()104 public void testEnterpriseConfigPhase2ChangeDetected() { 105 EnterpriseConfig eapConfig = 106 new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 107 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2); 108 EnterpriseConfig papConfig = 109 new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 110 .setPhase2(WifiEnterpriseConfig.Phase2.PAP); 111 112 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 113 papConfig.enterpriseConfig)); 114 } 115 116 /** 117 * Verify WifiEnterpriseConfig added Certificate is detected. 118 */ 119 @Test testCaCertificateAddedDetected()120 public void testCaCertificateAddedDetected() { 121 EnterpriseConfig eapConfigNoCerts = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 122 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 123 .setIdentity("username", "password"); 124 125 EnterpriseConfig eapConfig1Cert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 126 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 127 .setIdentity("username", "password") 128 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 129 130 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged( 131 eapConfigNoCerts.enterpriseConfig, eapConfig1Cert.enterpriseConfig)); 132 } 133 134 /** 135 * Verify WifiEnterpriseConfig Certificate change is detected. 136 */ 137 @Test testDifferentCaCertificateDetected()138 public void testDifferentCaCertificateDetected() { 139 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 140 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 141 .setIdentity("username", "password") 142 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 143 144 EnterpriseConfig eapConfigNewCert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 145 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 146 .setIdentity("username", "password") 147 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT1}); 148 149 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 150 eapConfigNewCert.enterpriseConfig)); 151 } 152 153 /** 154 * Verify WifiEnterpriseConfig added Certificate changes are detected. 155 */ 156 @Test testCaCertificateChangesDetected()157 public void testCaCertificateChangesDetected() { 158 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 159 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 160 .setIdentity("username", "password") 161 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 162 163 EnterpriseConfig eapConfigAddedCert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 164 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 165 .setIdentity("username", "password") 166 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 167 168 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 169 eapConfigAddedCert.enterpriseConfig)); 170 } 171 172 /** 173 * Verify that WifiEnterpriseConfig does not detect changes for identical configs. 174 */ 175 @Test testWifiEnterpriseConfigNoChanges()176 public void testWifiEnterpriseConfigNoChanges() { 177 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 178 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 179 .setIdentity("username", "password") 180 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 181 182 // Just to be clear that check is not against the same object 183 EnterpriseConfig eapConfigSame = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 184 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 185 .setIdentity("username", "password") 186 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 187 188 assertFalse(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 189 eapConfigSame.enterpriseConfig)); 190 } 191 192 /** 193 * Verify that the validate method successfully validates good WifiConfigurations with ASCII 194 * values. 195 */ 196 @Test testValidatePositiveCases_AsciiSsidString()197 public void testValidatePositiveCases_AsciiSsidString() { 198 assertTrue(WifiConfigurationUtil.validate( 199 WifiConfigurationTestUtil.createOpenNetwork(), 200 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 201 assertTrue(WifiConfigurationUtil.validate( 202 WifiConfigurationTestUtil.createPskNetwork(), 203 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 204 assertTrue(WifiConfigurationUtil.validate( 205 WifiConfigurationTestUtil.createWepNetwork(), 206 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 207 assertTrue(WifiConfigurationUtil.validate( 208 WifiConfigurationTestUtil.createEapNetwork(), 209 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 210 assertTrue(WifiConfigurationUtil.validate( 211 WifiConfigurationTestUtil.createOweNetwork(), 212 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 213 assertTrue(WifiConfigurationUtil.validate( 214 WifiConfigurationTestUtil.createSaeNetwork(), 215 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 216 assertTrue(WifiConfigurationUtil.validate( 217 WifiConfigurationTestUtil.createEapSuiteBNetwork(), 218 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 219 } 220 221 /** 222 * Verify that the validate method successfully validates good WifiConfigurations with hex 223 * values. 224 */ 225 @Test testValidatePositiveCases_HexSsidString()226 public void testValidatePositiveCases_HexSsidString() { 227 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 228 config.SSID = "abcd1234555a"; 229 config.preSharedKey = "abcd123455151234556788990034556667332345667322344556676743233445"; 230 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 231 } 232 233 /** 234 * Verify that the validate method validates WifiConfiguration with "any" in the BSSID field. 235 */ 236 @Test testValidatePositiveCases_AnyBssidString()237 public void testValidatePositiveCases_AnyBssidString() { 238 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 239 config.BSSID = "any"; 240 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 241 assertTrue(WifiConfigurationUtil.validate( 242 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 243 } 244 245 /** 246 * Verify that the validate method validates WifiConfiguration with masked psk string only for 247 * an update. 248 */ 249 @Test testValidatePositiveCases_MaskedPskString()250 public void testValidatePositiveCases_MaskedPskString() { 251 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 252 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 253 254 config.preSharedKey = WifiConfigurationUtil.PASSWORD_MASK; 255 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 256 assertTrue(WifiConfigurationUtil.validate( 257 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 258 } 259 260 /** 261 * Verify that the validate method validates WifiConfiguration with null ssid only for an 262 * update. 263 */ 264 @Test testValidatePositiveCases_OnlyUpdateIgnoresNullSsid()265 public void testValidatePositiveCases_OnlyUpdateIgnoresNullSsid() { 266 WifiConfiguration config = new WifiConfiguration(); 267 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 268 assertTrue(WifiConfigurationUtil.validate( 269 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 270 } 271 272 /** 273 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 274 */ 275 @Test testValidateNegativeCases_BadAsciiSsidLength()276 public void testValidateNegativeCases_BadAsciiSsidLength() { 277 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 278 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 279 280 config.SSID = "\"abcdfefeeretretyetretetetetetrertertrsreqwrwe\""; 281 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 282 config.SSID = "\"\""; 283 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 284 } 285 286 /** 287 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 288 */ 289 @Test testValidateNegativeCases_BadUtf8SsidLength()290 public void testValidateNegativeCases_BadUtf8SsidLength() { 291 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 292 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 293 294 config.SSID = "\"가하아너너ㅓ저저ㅓ어아아다자저ㅓ더타아어어러두어\""; 295 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 296 config.SSID = "\"\""; 297 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 298 } 299 300 /** 301 * Verify that the validate method fails to validate WifiConfiguration with malformed ssid 302 * string. 303 */ 304 @Test testValidateNegativeCases_MalformedAsciiSsidString()305 public void testValidateNegativeCases_MalformedAsciiSsidString() { 306 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 307 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 308 309 config.SSID = "\"ab"; 310 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 311 } 312 313 /** 314 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 315 */ 316 @Test testValidateNegativeCases_BadHexSsidLength()317 public void testValidateNegativeCases_BadHexSsidLength() { 318 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 319 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 320 321 config.SSID = "abcdfe012345632423343543453456464545656464545646454ace34534545634535"; 322 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 323 config.SSID = ""; 324 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 325 } 326 327 /** 328 * Verify that the validate method fails to validate WifiConfiguration with malformed ssid 329 * string. 330 */ 331 @Test testValidateNegativeCases_MalformedHexSsidString()332 public void testValidateNegativeCases_MalformedHexSsidString() { 333 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 334 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 335 336 config.SSID = "hello"; 337 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 338 } 339 340 /** 341 * Verify that the validate method fails to validate WifiConfiguration with bad psk length. 342 */ 343 @Test testValidateNegativeCases_BadAsciiPskLength()344 public void testValidateNegativeCases_BadAsciiPskLength() { 345 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 346 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 347 348 config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq" 349 + "weqe\""; 350 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 351 config.preSharedKey = "\"454\""; 352 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 353 } 354 355 /** 356 * Verify that the validate method fails to validate WifiConfiguration with bad sae length. 357 */ 358 @Test testValidateNegativeCases_BadAsciiSaeLength()359 public void testValidateNegativeCases_BadAsciiSaeLength() { 360 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 361 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 362 363 config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq" 364 + "weqe\""; 365 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 366 config.preSharedKey = "\"\""; 367 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 368 } 369 370 /** 371 * Verify that the validate method fails to validate WifiConfiguration with malformed psk 372 * string. 373 */ 374 @Test testValidateNegativeCases_MalformedAsciiPskString()375 public void testValidateNegativeCases_MalformedAsciiPskString() { 376 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 377 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 378 379 config.preSharedKey = "\"abcdfefeeretrety"; 380 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 381 } 382 383 /** 384 * Verify that the validate method fails to validate WifiConfiguration with malformed sae 385 * string. 386 */ 387 @Test testValidateNegativeCases_MalformedAsciiSaeString()388 public void testValidateNegativeCases_MalformedAsciiSaeString() { 389 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 390 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 391 392 config.preSharedKey = "\"abcdfefeeretrety"; 393 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 394 } 395 396 /** 397 * Verify that the validate method fails to validate WifiConfiguration with bad psk length. 398 */ 399 @Test testValidateNegativeCases_BadHexPskLength()400 public void testValidateNegativeCases_BadHexPskLength() { 401 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 402 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 403 404 config.preSharedKey = "abcd123456788990013453445345465465476546"; 405 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 406 config.preSharedKey = ""; 407 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 408 } 409 410 /** 411 * Verify that the validate method fails to validate WifiConfiguration with malformed psk 412 * string. 413 */ 414 @Test testValidateNegativeCases_MalformedHexPskString()415 public void testValidateNegativeCases_MalformedHexPskString() { 416 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 417 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 418 419 config.preSharedKey = "adbdfgretrtyrtyrty"; 420 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 421 } 422 423 /** 424 * Verify that the validate method fails to validate WifiConfiguration with malformed sae 425 * string. 426 */ 427 @Test testValidateNegativeCases_MalformedHexSaeString()428 public void testValidateNegativeCases_MalformedHexSaeString() { 429 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 430 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 431 432 config.preSharedKey = "adbdfgretrtyrtyrty"; 433 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 434 } 435 436 /** 437 * Verify that the validate method validates WifiConfiguration with masked wep key only for 438 * an update. 439 */ 440 @Test testValidatePositiveCases_MaskedWepKeysString()441 public void testValidatePositiveCases_MaskedWepKeysString() { 442 WifiConfiguration config = WifiConfigurationTestUtil.createWepNetwork(); 443 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 444 445 config.wepKeys = new String[]{ WifiConfigurationUtil.PASSWORD_MASK, 446 WifiConfigurationUtil.PASSWORD_MASK, 447 WifiConfigurationUtil.PASSWORD_MASK, 448 WifiConfigurationUtil.PASSWORD_MASK}; 449 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 450 assertTrue(WifiConfigurationUtil.validate( 451 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 452 } 453 454 /** 455 * Verify that the validate method fails to validate WifiConfiguration with bad wep length. 456 */ 457 @Test testValidateNegativeCases_BadWepKeysLength()458 public void testValidateNegativeCases_BadWepKeysLength() { 459 WifiConfiguration config = WifiConfigurationTestUtil.createWepNetwork(); 460 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 461 462 config.wepKeys = new String[] {"\"abcd\""}; 463 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 464 config.wepKeys = new String[] {"456"}; 465 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 466 // Error scenario in b/169638868. 467 config.wepKeys = new String[] {""}; 468 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 469 } 470 471 /** 472 * Verify that the validate method fails to validate WifiConfiguration with bad wep tx key idx. 473 */ 474 @Test testValidateNegativeCases_BadWepTxKeysIndex()475 public void testValidateNegativeCases_BadWepTxKeysIndex() { 476 WifiConfiguration config = WifiConfigurationTestUtil.createWepNetwork(); 477 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 478 // Should be < wepKeys.length 479 config.wepTxKeyIndex = config.wepKeys.length; 480 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 481 } 482 483 /** 484 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 485 */ 486 @Test testValidateNegativeCases_BadKeyMgmtPskEap()487 public void testValidateNegativeCases_BadKeyMgmtPskEap() { 488 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 489 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 490 491 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 492 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 493 } 494 495 /** 496 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 497 */ 498 @Test testValidateNegativeCases_BadKeyMgmtOpenPsk()499 public void testValidateNegativeCases_BadKeyMgmtOpenPsk() { 500 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 501 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 502 503 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 504 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 505 } 506 507 /** 508 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 509 */ 510 @Test testValidateNegativeCases_BadKeyMgmt()511 public void testValidateNegativeCases_BadKeyMgmt() { 512 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 513 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 514 515 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 516 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 517 } 518 519 /** 520 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 521 */ 522 @Test testValidateNegativeCases_BadSuiteBKeyMgmt()523 public void testValidateNegativeCases_BadSuiteBKeyMgmt() { 524 WifiConfiguration config = WifiConfigurationTestUtil.createEapSuiteBNetwork(); 525 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 526 527 config.allowedKeyManagement.clear(WifiConfiguration.KeyMgmt.IEEE8021X); 528 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK); 529 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 530 } 531 532 /** 533 * Verify that the validate method fails to validate WifiConfiguration with bad ipconfiguration 534 * values. 535 */ 536 @Test testValidateNegativeCases_BadIpconfiguration()537 public void testValidateNegativeCases_BadIpconfiguration() { 538 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 539 IpConfiguration ipConfig = 540 WifiConfigurationTestUtil.createStaticIpConfigurationWithPacProxy(); 541 config.setIpConfiguration(ipConfig); 542 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 543 544 ipConfig.setStaticIpConfiguration(null); 545 config.setIpConfiguration(ipConfig); 546 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 547 } 548 549 /** 550 * Verify that the validate method fails to validate WifiConfiguration with bad KeyMgmt value. 551 */ 552 @Test testValidateNegativeCases_InvalidKeyMgmt()553 public void testValidateNegativeCases_InvalidKeyMgmt() { 554 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 555 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 556 557 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.FT_EAP + 1); 558 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 559 } 560 561 /** 562 * Verify that the validate method fails to validate WifiConfiguration with bad KeyMgmt value. 563 */ 564 @Test testValidateNegativeCases_InvalidKeyMgmtWithPreSharedKey()565 public void testValidateNegativeCases_InvalidKeyMgmtWithPreSharedKey() { 566 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 567 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 568 569 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OSEN); 570 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 571 // Verify we reset the KeyMgmt 572 assertTrue(config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)); 573 assertFalse(config.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.OSEN)); 574 } 575 576 /** 577 * Verify that the validate method fails to validate WifiConfiguration with bad Protocol value. 578 */ 579 @Test testValidateNegativeCases_InvalidProtocol()580 public void testValidateNegativeCases_InvalidProtocol() { 581 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 582 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 583 584 config.allowedProtocols.set(4); 585 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 586 } 587 588 /** 589 * Verify that the validate method fails to validate WifiConfiguration with bad AuthAlgorithm 590 * value. 591 */ 592 @Test testValidateNegativeCases_InvalidAuthAlgorithm()593 public void testValidateNegativeCases_InvalidAuthAlgorithm() { 594 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 595 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 596 597 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP + 3); 598 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 599 } 600 601 /** 602 * Verify that the validate method fails to validate WifiConfiguration with bad GroupCipher 603 * value. 604 */ 605 @Test testValidateNegativeCases_InvalidGroupCipher()606 public void testValidateNegativeCases_InvalidGroupCipher() { 607 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 608 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 609 610 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.GTK_NOT_USED + 4); 611 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 612 } 613 614 /** 615 * Verify that the validate method fails to validate WifiConfiguration with bad PairwiseCipher 616 * value. 617 */ 618 @Test testValidateNegativeCases_InvalidPairwiseCipher()619 public void testValidateNegativeCases_InvalidPairwiseCipher() { 620 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 621 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 622 623 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP + 4); 624 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 625 } 626 627 /** 628 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 629 * only ssid pattern set. 630 */ 631 @Test testValidateNetworkSpecifierPositiveCases_SsidPattern()632 public void testValidateNetworkSpecifierPositiveCases_SsidPattern() { 633 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 634 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 635 Pair.create(WifiManager.ALL_ZEROS_MAC_ADDRESS, WifiManager.ALL_ZEROS_MAC_ADDRESS), 636 ScanResult.UNSPECIFIED, 637 WifiConfigurationTestUtil.createOpenNetwork()); 638 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 639 } 640 641 /** 642 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 643 * only bssid pattern set. 644 */ 645 @Test testValidateNetworkSpecifierPositiveCases_BssidPattern()646 public void testValidateNetworkSpecifierPositiveCases_BssidPattern() { 647 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 648 new PatternMatcher(".*", PatternMatcher.PATTERN_SIMPLE_GLOB), 649 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 650 ScanResult.UNSPECIFIED, 651 WifiConfigurationTestUtil.createOpenNetwork()); 652 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 653 } 654 655 /** 656 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 657 * both ssid & bssid patterns set. 658 */ 659 @Test testValidateNetworkSpecifierPositiveCases_BothSsidPatternAndBssidPattern()660 public void testValidateNetworkSpecifierPositiveCases_BothSsidPatternAndBssidPattern() { 661 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 662 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 663 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 664 ScanResult.UNSPECIFIED, 665 WifiConfigurationTestUtil.createOpenNetwork()); 666 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 667 } 668 669 /** 670 * Verify that the validate method validates a WifiNetworkSpecifier that specifies ssid, bssid, 671 * and band. Note that such requests will currently still be rejected by WifiNetworkFactory, but 672 * requesting specific bands may be supported in future releases. 673 */ 674 @Test testValidateNetworkSpecifierPositiveCases_SsidPatternAndBssidPatternAndBand()675 public void testValidateNetworkSpecifierPositiveCases_SsidPatternAndBssidPatternAndBand() { 676 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 677 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 678 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 679 ScanResult.WIFI_BAND_5_GHZ, 680 WifiConfigurationTestUtil.createOpenNetwork()); 681 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 682 } 683 684 685 /** 686 * Verify that the validate method fails to validate WifiNetworkSpecifier with no 687 * ssid/bssid info. 688 */ 689 @Test testValidateNetworkSpecifierNegativeCases_NoSsidBssid()690 public void testValidateNetworkSpecifierNegativeCases_NoSsidBssid() { 691 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 692 new PatternMatcher(".*", PatternMatcher.PATTERN_SIMPLE_GLOB), 693 Pair.create(WifiManager.ALL_ZEROS_MAC_ADDRESS, WifiManager.ALL_ZEROS_MAC_ADDRESS), 694 ScanResult.UNSPECIFIED, 695 WifiConfigurationTestUtil.createOpenNetwork()); 696 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 697 } 698 699 /** 700 * Verify that the validate method fails to validate WifiNetworkSpecifier with invalid SSID 701 * match pattern. 702 */ 703 @Test testValidateNetworkSpecifierNegativeCases_MatchNoneSsidPattern()704 public void testValidateNetworkSpecifierNegativeCases_MatchNoneSsidPattern() { 705 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 706 new PatternMatcher("", PatternMatcher.PATTERN_LITERAL), 707 Pair.create(WifiManager.ALL_ZEROS_MAC_ADDRESS, WifiManager.ALL_ZEROS_MAC_ADDRESS), 708 ScanResult.UNSPECIFIED, 709 WifiConfigurationTestUtil.createOpenNetwork()); 710 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 711 } 712 713 /** 714 * Verify that the validate method fails to validate WifiNetworkSpecifier with illegal 715 * pattern. 716 */ 717 @Test testValidateNetworkSpecifierNegativeCases_MatchNoneBssidPattern()718 public void testValidateNetworkSpecifierNegativeCases_MatchNoneBssidPattern() { 719 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 720 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 721 Pair.create(MacAddress.BROADCAST_ADDRESS, MacAddress.BROADCAST_ADDRESS), 722 ScanResult.UNSPECIFIED, 723 WifiConfigurationTestUtil.createOpenNetwork()); 724 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 725 } 726 727 /** 728 * Verify that the validate method fails to validate WifiNetworkSpecifier with illegal 729 * pattern. 730 */ 731 @Test testValidateNetworkSpecifierNegativeCases_InvalidBssidPattern()732 public void testValidateNetworkSpecifierNegativeCases_InvalidBssidPattern() { 733 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 734 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 735 Pair.create(MacAddress.fromString(TEST_BSSID), WifiManager.ALL_ZEROS_MAC_ADDRESS), 736 ScanResult.UNSPECIFIED, 737 WifiConfigurationTestUtil.createOpenNetwork()); 738 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 739 } 740 741 /** 742 * Verify that the validate method fails to validate WifiNetworkSpecifier with SSID pattern 743 * for hidden network. 744 */ 745 @Test testValidateNetworkSpecifierNegativeCases_NoSsidPatternForHiddenNetwork()746 public void testValidateNetworkSpecifierNegativeCases_NoSsidPatternForHiddenNetwork() { 747 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 748 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_PREFIX), 749 Pair.create(WifiManager.ALL_ZEROS_MAC_ADDRESS, WifiManager.ALL_ZEROS_MAC_ADDRESS), 750 ScanResult.UNSPECIFIED, 751 WifiConfigurationTestUtil.createOpenHiddenNetwork()); 752 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 753 } 754 755 /** 756 * Verify that the validate method fails to validate WifiNetworkSpecifier with an invalid band. 757 */ 758 @Test testValidateNetworkSpecifierNegativeCases_InvalidBand()759 public void testValidateNetworkSpecifierNegativeCases_InvalidBand() { 760 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 761 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 762 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 763 42, // invalid 764 WifiConfigurationTestUtil.createOpenNetwork()); 765 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 766 } 767 768 /** 769 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 770 * for an open network using {@link WifiConfigurationUtil#createPnoNetwork( 771 * WifiConfiguration)}. 772 */ 773 @Test testCreatePnoNetworkWithOpenNetwork()774 public void testCreatePnoNetworkWithOpenNetwork() { 775 WifiConfiguration network = WifiConfigurationTestUtil.createOpenNetwork(); 776 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 777 WifiConfigurationUtil.createPnoNetwork(network); 778 assertEquals(network.SSID, pnoNetwork.ssid); 779 assertEquals( 780 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 781 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 782 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_OPEN, pnoNetwork.authBitField); 783 } 784 785 /** 786 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 787 * for an open hidden network using {@link WifiConfigurationUtil#createPnoNetwork( 788 * WifiConfiguration)}. 789 */ 790 @Test testCreatePnoNetworkWithOpenHiddenNetwork()791 public void testCreatePnoNetworkWithOpenHiddenNetwork() { 792 WifiConfiguration network = WifiConfigurationTestUtil.createOpenHiddenNetwork(); 793 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 794 WifiConfigurationUtil.createPnoNetwork(network); 795 assertEquals(network.SSID, pnoNetwork.ssid); 796 assertEquals( 797 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 798 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND 799 | WifiScanner.PnoSettings.PnoNetwork.FLAG_DIRECTED_SCAN, pnoNetwork.flags); 800 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_OPEN, pnoNetwork.authBitField); 801 } 802 803 /** 804 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 805 * for a PSK network using {@link WifiConfigurationUtil#createPnoNetwork(WifiConfiguration) 806 * }. 807 */ 808 @Test testCreatePnoNetworkWithPskNetwork()809 public void testCreatePnoNetworkWithPskNetwork() { 810 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(); 811 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 812 WifiConfigurationUtil.createPnoNetwork(network); 813 assertEquals(network.SSID, pnoNetwork.ssid); 814 assertEquals( 815 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 816 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 817 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_PSK, pnoNetwork.authBitField); 818 } 819 820 /** 821 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 822 * objects have the same parameters. 823 */ 824 @Test testIsSameNetworkReturnsTrueOnSameNetwork()825 public void testIsSameNetworkReturnsTrueOnSameNetwork() { 826 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 827 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 828 assertTrue(WifiConfigurationUtil.isSameNetwork(network, network1)); 829 } 830 831 /** 832 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 833 * objects have the same parameters but different network selection BSSID's. 834 */ 835 @Test testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentBSSID()836 public void testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentBSSID() { 837 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 838 network.getNetworkSelectionStatus().setNetworkSelectionBSSID(TEST_BSSID); 839 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 840 network1.getNetworkSelectionStatus().setNetworkSelectionBSSID(TEST_BSSID_1); 841 assertTrue(WifiConfigurationUtil.isSameNetwork(network, network1)); 842 } 843 844 /** 845 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 846 * objects have the different SSIDs. 847 */ 848 @Test testIsSameNetworkReturnsFalseOnDifferentSSID()849 public void testIsSameNetworkReturnsFalseOnDifferentSSID() { 850 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 851 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID_1); 852 assertFalse(WifiConfigurationUtil.isSameNetwork(network, network1)); 853 } 854 855 /** 856 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 857 * objects have the different security type. 858 */ 859 @Test testIsSameNetworkReturnsFalseOnDifferentSecurityType()860 public void testIsSameNetworkReturnsFalseOnDifferentSecurityType() { 861 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 862 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 863 assertFalse(WifiConfigurationUtil.isSameNetwork(network, network1)); 864 } 865 866 /** 867 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 868 * objects have the different EAP identity. 869 */ 870 @Test testIsSameNetworkReturnsFalseOnDifferentEapIdentity()871 public void testIsSameNetworkReturnsFalseOnDifferentEapIdentity() { 872 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 873 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 874 network1.enterpriseConfig.setIdentity("Identity1"); 875 network2.enterpriseConfig.setIdentity("Identity2"); 876 assertFalse(WifiConfigurationUtil.isSameNetwork(network1, network2)); 877 } 878 879 /** 880 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 881 * objects have the different EAP anonymous identity. 882 */ 883 @Test testIsSameNetworkReturnsFalseOnDifferentEapAnonymousIdentity()884 public void testIsSameNetworkReturnsFalseOnDifferentEapAnonymousIdentity() { 885 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 886 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 887 network1.enterpriseConfig.setAnonymousIdentity("Identity1"); 888 network2.enterpriseConfig.setAnonymousIdentity("Identity2"); 889 assertFalse(WifiConfigurationUtil.isSameNetwork(network1, network2)); 890 } 891 892 /** 893 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 894 * objects have the different EAP anonymous(pseudonym) identity in EAP-SIM. 895 */ 896 @Test testIsSameNetworkReturnsTrueOnDifferentEapAnonymousIdentityInEapSim()897 public void testIsSameNetworkReturnsTrueOnDifferentEapAnonymousIdentityInEapSim() { 898 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 899 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 900 network1.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); 901 network2.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); 902 network1.enterpriseConfig.setAnonymousIdentity("Identity1"); 903 network2.enterpriseConfig.setAnonymousIdentity("Identity2"); 904 assertTrue(WifiConfigurationUtil.isSameNetwork(network1, network2)); 905 } 906 907 /** 908 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 909 * objects have the same candidate security params. 910 */ 911 @Test testIsSameNetworkReturnsTrueOnSameNetworkWithSameCandidateSecurityParams()912 public void testIsSameNetworkReturnsTrueOnSameNetworkWithSameCandidateSecurityParams() { 913 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 914 network.getNetworkSelectionStatus().setCandidateSecurityParams( 915 SecurityParams.createSecurityParamsBySecurityType( 916 WifiConfiguration.SECURITY_TYPE_PSK)); 917 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 918 network1.getNetworkSelectionStatus().setCandidateSecurityParams( 919 SecurityParams.createSecurityParamsBySecurityType( 920 WifiConfiguration.SECURITY_TYPE_PSK)); 921 assertTrue(WifiConfigurationUtil.isSameNetwork(network, network1)); 922 } 923 924 /** 925 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 926 * objects have the different candidate security params. 927 */ 928 @Test testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentCandidateSecurityParams()929 public void testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentCandidateSecurityParams() { 930 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 931 network.getNetworkSelectionStatus().setCandidateSecurityParams( 932 SecurityParams.createSecurityParamsBySecurityType( 933 WifiConfiguration.SECURITY_TYPE_PSK)); 934 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 935 network1.getNetworkSelectionStatus().setCandidateSecurityParams( 936 SecurityParams.createSecurityParamsBySecurityType( 937 WifiConfiguration.SECURITY_TYPE_SAE)); 938 assertFalse(WifiConfigurationUtil.isSameNetwork(network, network1)); 939 } 940 941 /** 942 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 943 * for a EAP network using {@link WifiConfigurationUtil#createPnoNetwork(WifiConfiguration) 944 * }. 945 */ 946 @Test testCreatePnoNetworkWithEapNetwork()947 public void testCreatePnoNetworkWithEapNetwork() { 948 WifiConfiguration network = WifiConfigurationTestUtil.createEapNetwork(); 949 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 950 WifiConfigurationUtil.createPnoNetwork(network); 951 assertEquals(network.SSID, pnoNetwork.ssid); 952 assertEquals( 953 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 954 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 955 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_EAPOL, pnoNetwork.authBitField); 956 } 957 958 /** 959 * Verify that the generalized 960 * {@link com.android.server.wifi.WifiConfigurationUtil.WifiConfigurationComparator} 961 * can be used to sort a List given a 'compareNetworkWithSameStatus' method. 962 */ 963 @Test testPnoListComparator()964 public void testPnoListComparator() { 965 List<WifiConfiguration> networks = new ArrayList<>(); 966 final WifiConfiguration enabledNetwork1 = WifiConfigurationTestUtil.createEapNetwork(); 967 enabledNetwork1.getNetworkSelectionStatus().setNetworkSelectionStatus( 968 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED); 969 final WifiConfiguration enabledNetwork2 = WifiConfigurationTestUtil.createEapNetwork(); 970 enabledNetwork2.getNetworkSelectionStatus().setNetworkSelectionStatus( 971 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED); 972 final WifiConfiguration tempDisabledNetwork1 = WifiConfigurationTestUtil.createEapNetwork(); 973 tempDisabledNetwork1.getNetworkSelectionStatus().setNetworkSelectionStatus( 974 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); 975 final WifiConfiguration tempDisabledNetwork2 = WifiConfigurationTestUtil.createEapNetwork(); 976 tempDisabledNetwork2.getNetworkSelectionStatus().setNetworkSelectionStatus( 977 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); 978 WifiConfiguration permDisabledNetwork = WifiConfigurationTestUtil.createEapNetwork(); 979 permDisabledNetwork.getNetworkSelectionStatus().setNetworkSelectionStatus( 980 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED); 981 982 // Add all the networks to the list. 983 networks.add(tempDisabledNetwork1); 984 networks.add(enabledNetwork1); 985 networks.add(permDisabledNetwork); 986 networks.add(tempDisabledNetwork2); 987 networks.add(enabledNetwork2); 988 989 // Prefer |enabledNetwork1| over |enabledNetwork2| and |tempDisabledNetwork1| over 990 // |tempDisabledNetwork2|. 991 WifiConfigurationUtil.WifiConfigurationComparator comparator = 992 new WifiConfigurationUtil.WifiConfigurationComparator() { 993 @Override 994 public int compareNetworksWithSameStatus( 995 WifiConfiguration a, WifiConfiguration b) { 996 if (a == enabledNetwork1 && b == enabledNetwork2) { 997 return -1; 998 } else if (b == enabledNetwork1 && a == enabledNetwork2) { 999 return 1; 1000 } else if (a == tempDisabledNetwork1 && b == tempDisabledNetwork2) { 1001 return -1; 1002 } else if (b == tempDisabledNetwork1 && a == tempDisabledNetwork2) { 1003 return 1; 1004 } 1005 return 0; 1006 } 1007 }; 1008 Collections.sort(networks, comparator); 1009 1010 // Now ensure that the networks were sorted correctly. 1011 assertEquals(enabledNetwork1, networks.get(0)); 1012 assertEquals(enabledNetwork2, networks.get(1)); 1013 assertEquals(tempDisabledNetwork1, networks.get(2)); 1014 assertEquals(tempDisabledNetwork2, networks.get(3)); 1015 assertEquals(permDisabledNetwork, networks.get(4)); 1016 } 1017 1018 /** 1019 * Verifies that when the existing configuration is null and macRandomizationSetting in the 1020 * newConfig is the default value, then hasMacRandomizationSettingsChanged returns false. 1021 */ 1022 @Test testHasMacRandomizationSettingsChangedNullExistingConfigDefaultNewConfig()1023 public void testHasMacRandomizationSettingsChangedNullExistingConfigDefaultNewConfig() { 1024 WifiConfiguration newConfig = new WifiConfiguration(); 1025 assertFalse(WifiConfigurationUtil.hasMacRandomizationSettingsChanged(null, newConfig)); 1026 } 1027 1028 /** 1029 * Verifies that when the existing configuration is null and macRandomizationSetting in the 1030 * newConfig is not the default value, then hasMacRandomizationSettingsChanged returns true. 1031 */ 1032 @Test testHasMacRandomizationSettingsChangedNullExistingConfigModifiedNewConfig()1033 public void testHasMacRandomizationSettingsChangedNullExistingConfigModifiedNewConfig() { 1034 WifiConfiguration newConfig = new WifiConfiguration(); 1035 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 1036 assertTrue(WifiConfigurationUtil.hasMacRandomizationSettingsChanged(null, newConfig)); 1037 } 1038 1039 /** 1040 * Verifies that when macRandomizationSetting in the newConfig is different from existingConfig 1041 * hasMacRandomizationSettingsChanged returns true. 1042 */ 1043 @Test testHasMacRandomizationSettingsChangedFieldsDifferent()1044 public void testHasMacRandomizationSettingsChangedFieldsDifferent() { 1045 WifiConfiguration existingConfig = new WifiConfiguration(); 1046 WifiConfiguration newConfig = new WifiConfiguration(); 1047 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 1048 assertTrue(WifiConfigurationUtil.hasMacRandomizationSettingsChanged( 1049 existingConfig, newConfig)); 1050 } 1051 1052 /** 1053 * Verifies that when macRandomizationSetting in the newConfig is the same as existingConfig 1054 * hasMacRandomizationSettingsChanged returns false. 1055 */ 1056 @Test testHasMacRandomizationSettingsChangedFieldsSame()1057 public void testHasMacRandomizationSettingsChangedFieldsSame() { 1058 WifiConfiguration existingConfig = new WifiConfiguration(); 1059 existingConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 1060 WifiConfiguration newConfig = new WifiConfiguration(); 1061 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 1062 assertFalse(WifiConfigurationUtil.hasMacRandomizationSettingsChanged( 1063 existingConfig, newConfig)); 1064 } 1065 1066 private static class EnterpriseConfig { 1067 public String eap; 1068 public String phase2; 1069 public String identity; 1070 public String password; 1071 public X509Certificate[] caCerts; 1072 public WifiEnterpriseConfig enterpriseConfig; 1073 public String wapiCertSuite; 1074 EnterpriseConfig(int eapMethod)1075 EnterpriseConfig(int eapMethod) { 1076 enterpriseConfig = new WifiEnterpriseConfig(); 1077 enterpriseConfig.setEapMethod(eapMethod); 1078 eap = WifiEnterpriseConfig.Eap.strings[eapMethod]; 1079 } 1080 setPhase2(int phase2Method)1081 public EnterpriseConfig setPhase2(int phase2Method) { 1082 enterpriseConfig.setPhase2Method(phase2Method); 1083 phase2 = "auth=" + WifiEnterpriseConfig.Phase2.strings[phase2Method]; 1084 return this; 1085 } 1086 setIdentity(String identity, String password)1087 public EnterpriseConfig setIdentity(String identity, String password) { 1088 enterpriseConfig.setIdentity(identity); 1089 enterpriseConfig.setPassword(password); 1090 this.identity = identity; 1091 this.password = password; 1092 return this; 1093 } 1094 setCaCerts(X509Certificate[] certs)1095 public EnterpriseConfig setCaCerts(X509Certificate[] certs) { 1096 enterpriseConfig.setCaCertificates(certs); 1097 caCerts = certs; 1098 return this; 1099 } 1100 setWapiCertSuite(String certSuite)1101 public EnterpriseConfig setWapiCertSuite(String certSuite) { 1102 enterpriseConfig.setWapiCertSuite(certSuite); 1103 wapiCertSuite = certSuite; 1104 return this; 1105 } 1106 } 1107 1108 /** 1109 * Verify WifiEnterpriseConfig CA Certificate alias changes are detected. 1110 */ 1111 @Test testCaCertificateAliasChangesDetected()1112 public void testCaCertificateAliasChangesDetected() { 1113 EnterpriseConfig eapConfig1 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1114 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1115 .setIdentity("username", "password"); 1116 eapConfig1.enterpriseConfig.setCaCertificateAlias("ALIAS_1"); 1117 1118 EnterpriseConfig eapConfig2 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1119 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1120 .setIdentity("username", "password"); 1121 eapConfig2.enterpriseConfig.setCaCertificateAlias("ALIAS_2"); 1122 1123 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig1.enterpriseConfig, 1124 eapConfig2.enterpriseConfig)); 1125 } 1126 1127 /** 1128 * Verify WifiEnterpriseConfig Client Certificate alias changes are detected. 1129 */ 1130 @Test testClientCertificateAliasChangesDetected()1131 public void testClientCertificateAliasChangesDetected() { 1132 EnterpriseConfig eapConfig1 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TLS); 1133 eapConfig1.enterpriseConfig.setCaCertificateAlias("ALIAS_1"); 1134 eapConfig1.enterpriseConfig.setClientCertificateAlias("CLIENT_ALIAS_1"); 1135 1136 EnterpriseConfig eapConfig2 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS); 1137 eapConfig2.enterpriseConfig.setCaCertificateAlias("ALIAS_1"); 1138 eapConfig2.enterpriseConfig.setClientCertificateAlias("CLIENT_ALIAS_2"); 1139 1140 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig1.enterpriseConfig, 1141 eapConfig2.enterpriseConfig)); 1142 } 1143 1144 /** 1145 * Verify WifiEnterpriseConfig OCSP changes are detected. 1146 */ 1147 @Test testOcspChangesDetected()1148 public void testOcspChangesDetected() { 1149 EnterpriseConfig eapConfig1 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1150 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1151 .setIdentity("username", "password") 1152 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 1153 eapConfig1.enterpriseConfig.setOcsp(OCSP_NONE); 1154 1155 EnterpriseConfig eapConfig2 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1156 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1157 .setIdentity("username", "password") 1158 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 1159 eapConfig2.enterpriseConfig.setOcsp(OCSP_REQUIRE_CERT_STATUS); 1160 1161 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig1.enterpriseConfig, 1162 eapConfig2.enterpriseConfig)); 1163 } 1164 1165 /** 1166 * Verify WifiEnterpriseConfig subject match changes are detected. 1167 */ 1168 @Test testSubjectMatchChangesDetected()1169 public void testSubjectMatchChangesDetected() { 1170 EnterpriseConfig eapConfig1 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1171 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1172 .setIdentity("username", "password") 1173 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 1174 eapConfig1.enterpriseConfig.setAltSubjectMatch("domain1.com"); 1175 1176 EnterpriseConfig eapConfig2 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 1177 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 1178 .setIdentity("username", "password") 1179 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 1180 eapConfig1.enterpriseConfig.setAltSubjectMatch("domain2.com"); 1181 1182 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig1.enterpriseConfig, 1183 eapConfig2.enterpriseConfig)); 1184 } 1185 1186 /** 1187 * Verify that new WifiEnterpriseConfig is detected. 1188 */ 1189 @Test testEnterpriseConfigWapiCertChanged()1190 public void testEnterpriseConfigWapiCertChanged() { 1191 EnterpriseConfig eapConfig1 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.WAPI_CERT) 1192 .setWapiCertSuite("WapiCertSuite1"); 1193 EnterpriseConfig eapConfig2 = new EnterpriseConfig(WifiEnterpriseConfig.Eap.WAPI_CERT) 1194 .setWapiCertSuite("WapiCertSuite2"); 1195 1196 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged( 1197 eapConfig1.enterpriseConfig, eapConfig2.enterpriseConfig)); 1198 } 1199 1200 /** 1201 * Verify that a WAPI config is not considered an OPEN config. 1202 */ 1203 @Test testWapiConfigNotOpenConfig()1204 public void testWapiConfigNotOpenConfig() { 1205 WifiConfiguration wapiPskConfig = new WifiConfiguration(); 1206 wapiPskConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WAPI_CERT); 1207 assertFalse(WifiConfigurationUtil.isConfigForOpenNetwork(wapiPskConfig)); 1208 1209 WifiConfiguration wapiCertConfig = new WifiConfiguration(); 1210 wapiCertConfig.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WAPI_CERT); 1211 assertFalse(WifiConfigurationUtil.isConfigForOpenNetwork(wapiCertConfig)); 1212 } 1213 1214 1215 /** 1216 * Verify that the validate method fails to validate WifiConfiguration with malformed 1217 * enterprise configuration 1218 */ 1219 @Test testValidateNegativeCases_MalformedEnterpriseConfig()1220 public void testValidateNegativeCases_MalformedEnterpriseConfig() { 1221 WifiConfiguration config = new WifiConfiguration(); 1222 config.SSID = "\"someNetwork\""; 1223 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP); 1224 // EAP method is kept as Eap.NONE - should not crash, but return invalid ID 1225 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1226 1227 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE); 1228 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1229 1230 config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT); 1231 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1232 } 1233 1234 /** 1235 * Verify that the validate method fails to validate WifiConfiguration with enterprise 1236 * configuration that is missing the identity and/or password. 1237 */ 1238 @Test testValidateNegativeCases_NoIdentityOrPasswordEnterpriseConfig()1239 public void testValidateNegativeCases_NoIdentityOrPasswordEnterpriseConfig() { 1240 WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork(); 1241 config.enterpriseConfig.setIdentity(null); 1242 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1243 assertFalse(WifiConfigurationUtil.validate(config, 1244 WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 1245 1246 config = WifiConfigurationTestUtil.createEapNetwork(); 1247 config.enterpriseConfig.setPassword(null); 1248 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1249 assertTrue(WifiConfigurationUtil.validate(config, 1250 WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 1251 1252 config = WifiConfigurationTestUtil.createWpa3EnterpriseNetwork(TEST_SSID); 1253 config.enterpriseConfig.setIdentity(null); 1254 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1255 assertFalse(WifiConfigurationUtil.validate(config, 1256 WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 1257 1258 config = WifiConfigurationTestUtil.createWpa3EnterpriseNetwork(TEST_SSID); 1259 config.enterpriseConfig.setPassword(null); 1260 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 1261 assertTrue(WifiConfigurationUtil.validate(config, 1262 WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 1263 } 1264 1265 /** 1266 * Verify the behavior of convertWifiInfoSecurityTypeToWifiConfiguration 1267 */ 1268 @Test testConvertWifiInfoSecurityTypeToWifiConfiguration()1269 public void testConvertWifiInfoSecurityTypeToWifiConfiguration() { 1270 assertEquals(WifiConfiguration.SECURITY_TYPE_OPEN, 1271 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_OPEN)); 1272 assertEquals(WifiConfiguration.SECURITY_TYPE_WEP, 1273 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_WEP)); 1274 assertEquals(WifiConfiguration.SECURITY_TYPE_PSK, 1275 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_PSK)); 1276 assertEquals(WifiConfiguration.SECURITY_TYPE_EAP, 1277 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_EAP)); 1278 assertEquals(WifiConfiguration.SECURITY_TYPE_SAE, 1279 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_SAE)); 1280 assertEquals(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT, 1281 convertWifiInfoSecurityTypeToWifiConfiguration( 1282 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT)); 1283 assertEquals(WifiConfiguration.SECURITY_TYPE_OWE, 1284 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_OWE)); 1285 assertEquals(WifiConfiguration.SECURITY_TYPE_WAPI_PSK, 1286 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_WAPI_PSK)); 1287 assertEquals(WifiConfiguration.SECURITY_TYPE_WAPI_CERT, 1288 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_WAPI_CERT)); 1289 assertEquals(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE, 1290 convertWifiInfoSecurityTypeToWifiConfiguration( 1291 WifiInfo.SECURITY_TYPE_EAP_WPA3_ENTERPRISE)); 1292 assertEquals(WifiConfiguration.SECURITY_TYPE_OSEN, 1293 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_OSEN)); 1294 assertEquals(WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2, 1295 convertWifiInfoSecurityTypeToWifiConfiguration( 1296 WifiInfo.SECURITY_TYPE_PASSPOINT_R1_R2)); 1297 assertEquals(WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3, 1298 convertWifiInfoSecurityTypeToWifiConfiguration( 1299 WifiInfo.SECURITY_TYPE_PASSPOINT_R3)); 1300 assertEquals(-1, convertWifiInfoSecurityTypeToWifiConfiguration(13)); 1301 assertEquals(-1, 1302 convertWifiInfoSecurityTypeToWifiConfiguration(WifiInfo.SECURITY_TYPE_UNKNOWN)); 1303 } 1304 1305 /** 1306 * Verify that adding and removing the security type for network ID behaves correctly 1307 */ 1308 @Test testAddAndRemoveSecurityTypeForNetworkId()1309 public void testAddAndRemoveSecurityTypeForNetworkId() { 1310 List<Integer> securityList = Arrays.asList( 1311 WifiConfiguration.SECURITY_TYPE_OPEN, 1312 WifiConfiguration.SECURITY_TYPE_WEP, 1313 WifiConfiguration.SECURITY_TYPE_PSK, 1314 WifiConfiguration.SECURITY_TYPE_EAP, 1315 WifiConfiguration.SECURITY_TYPE_SAE, 1316 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT, 1317 WifiConfiguration.SECURITY_TYPE_OWE, 1318 WifiConfiguration.SECURITY_TYPE_WAPI_PSK, 1319 WifiConfiguration.SECURITY_TYPE_WAPI_CERT, 1320 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE, 1321 WifiConfiguration.SECURITY_TYPE_OSEN, 1322 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2, 1323 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3 1324 ); 1325 1326 final int netId = 1; 1327 if (!SdkLevel.isAtLeastS()) { 1328 // INVALID_NET_ID should remain the same from either adding or removing 1329 assertEquals(INVALID_NETWORK_ID, WifiConfigurationUtil.addSecurityTypeToNetworkId( 1330 INVALID_NETWORK_ID, WifiConfiguration.SECURITY_TYPE_OPEN)); 1331 assertEquals(INVALID_NETWORK_ID, WifiConfigurationUtil.removeSecurityTypeFromNetworkId( 1332 INVALID_NETWORK_ID)); 1333 // Add and then remove should result in the original netId 1334 for (@WifiConfiguration.SecurityType int securityType : securityList) { 1335 assertEquals(netId, WifiConfigurationUtil.removeSecurityTypeFromNetworkId( 1336 WifiConfigurationUtil.addSecurityTypeToNetworkId( 1337 netId, securityType))); 1338 } 1339 // Multiple removes should result in the same netId as a single remove 1340 for (@WifiConfiguration.SecurityType int securityType : securityList) { 1341 assertEquals(WifiConfigurationUtil.removeSecurityTypeFromNetworkId(netId), 1342 WifiConfigurationUtil.removeSecurityTypeFromNetworkId( 1343 WifiConfigurationUtil.removeSecurityTypeFromNetworkId( 1344 WifiConfigurationUtil.addSecurityTypeToNetworkId( 1345 netId, securityType)))); 1346 } 1347 // A unique net id should be created for each security type added 1348 assertEquals(securityList.size(), securityList.stream() 1349 .map(security -> addSecurityTypeToNetworkId(netId, security)) 1350 .distinct() 1351 .count()); 1352 } else { 1353 // Add should do nothing for SDK level S and above. 1354 for (@WifiConfiguration.SecurityType int securityType : securityList) { 1355 assertEquals(netId, addSecurityTypeToNetworkId(netId, securityType)); 1356 } 1357 } 1358 } 1359 } 1360