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 org.junit.Assert.*; 20 21 import android.content.pm.UserInfo; 22 import android.net.IpConfiguration; 23 import android.net.MacAddress; 24 import android.net.wifi.WifiConfiguration; 25 import android.net.wifi.WifiEnterpriseConfig; 26 import android.net.wifi.WifiNetworkSpecifier; 27 import android.net.wifi.WifiScanner; 28 import android.os.PatternMatcher; 29 import android.os.UserHandle; 30 import android.util.Pair; 31 32 import androidx.test.filters.SmallTest; 33 34 import org.junit.Test; 35 36 import java.security.cert.X509Certificate; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Collections; 40 import java.util.List; 41 42 /** 43 * Unit tests for {@link com.android.server.wifi.WifiConfigurationUtil}. 44 */ 45 @SmallTest 46 public class WifiConfigurationUtilTest { 47 static final int CURRENT_USER_ID = 0; 48 static final int CURRENT_USER_MANAGED_PROFILE_USER_ID = 10; 49 static final int OTHER_USER_ID = 11; 50 static final int TEST_UID = 10000; 51 static final String TEST_PACKAGE = "com.test"; 52 static final String TEST_SSID = "test_ssid"; 53 static final String TEST_SSID_1 = "test_ssid_1"; 54 static final String TEST_BSSID = "aa:aa:11:22:cc:dd"; 55 static final String TEST_BSSID_1 = "11:22:11:22:cc:dd"; 56 static final List<UserInfo> PROFILES = Arrays.asList( 57 new UserInfo(CURRENT_USER_ID, "owner", 0), 58 new UserInfo(CURRENT_USER_MANAGED_PROFILE_USER_ID, "managed profile", 0)); 59 60 /** 61 * Test for {@link WifiConfigurationUtil.isVisibleToAnyProfile}. 62 */ 63 @Test isVisibleToAnyProfile()64 public void isVisibleToAnyProfile() { 65 // Shared network configuration created by another user. 66 final WifiConfiguration configuration = new WifiConfiguration(); 67 configuration.creatorUid = UserHandle.getUid(OTHER_USER_ID, 0); 68 assertTrue(WifiConfigurationUtil.isVisibleToAnyProfile(configuration, PROFILES)); 69 70 // Private network configuration created by another user. 71 configuration.shared = false; 72 assertFalse(WifiConfigurationUtil.isVisibleToAnyProfile(configuration, PROFILES)); 73 74 // Private network configuration created by the current user. 75 configuration.creatorUid = UserHandle.getUid(CURRENT_USER_ID, 0); 76 assertTrue(WifiConfigurationUtil.isVisibleToAnyProfile(configuration, PROFILES)); 77 78 // Private network configuration created by the current user's managed profile. 79 configuration.creatorUid = UserHandle.getUid(CURRENT_USER_MANAGED_PROFILE_USER_ID, 0); 80 assertTrue(WifiConfigurationUtil.isVisibleToAnyProfile(configuration, PROFILES)); 81 } 82 83 /** 84 * Verify that new WifiEnterpriseConfig is detected. 85 */ 86 @Test testEnterpriseConfigAdded()87 public void testEnterpriseConfigAdded() { 88 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 89 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 90 .setIdentity("username", "password") 91 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 92 93 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged( 94 null, eapConfig.enterpriseConfig)); 95 } 96 97 /** 98 * Verify WifiEnterpriseConfig eap change is detected. 99 */ 100 @Test testEnterpriseConfigEapChangeDetected()101 public void testEnterpriseConfigEapChangeDetected() { 102 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS); 103 EnterpriseConfig peapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.PEAP); 104 105 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 106 peapConfig.enterpriseConfig)); 107 } 108 109 /** 110 * Verify WifiEnterpriseConfig phase2 method change is detected. 111 */ 112 @Test testEnterpriseConfigPhase2ChangeDetected()113 public void testEnterpriseConfigPhase2ChangeDetected() { 114 EnterpriseConfig eapConfig = 115 new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 116 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2); 117 EnterpriseConfig papConfig = 118 new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 119 .setPhase2(WifiEnterpriseConfig.Phase2.PAP); 120 121 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 122 papConfig.enterpriseConfig)); 123 } 124 125 /** 126 * Verify WifiEnterpriseConfig added Certificate is detected. 127 */ 128 @Test testCaCertificateAddedDetected()129 public void testCaCertificateAddedDetected() { 130 EnterpriseConfig eapConfigNoCerts = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 131 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 132 .setIdentity("username", "password"); 133 134 EnterpriseConfig eapConfig1Cert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 135 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 136 .setIdentity("username", "password") 137 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 138 139 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged( 140 eapConfigNoCerts.enterpriseConfig, eapConfig1Cert.enterpriseConfig)); 141 } 142 143 /** 144 * Verify WifiEnterpriseConfig Certificate change is detected. 145 */ 146 @Test testDifferentCaCertificateDetected()147 public void testDifferentCaCertificateDetected() { 148 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 149 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 150 .setIdentity("username", "password") 151 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 152 153 EnterpriseConfig eapConfigNewCert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 154 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 155 .setIdentity("username", "password") 156 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT1}); 157 158 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 159 eapConfigNewCert.enterpriseConfig)); 160 } 161 162 /** 163 * Verify WifiEnterpriseConfig added Certificate changes are detected. 164 */ 165 @Test testCaCertificateChangesDetected()166 public void testCaCertificateChangesDetected() { 167 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 168 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 169 .setIdentity("username", "password") 170 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0}); 171 172 EnterpriseConfig eapConfigAddedCert = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 173 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 174 .setIdentity("username", "password") 175 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 176 177 assertTrue(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 178 eapConfigAddedCert.enterpriseConfig)); 179 } 180 181 /** 182 * Verify that WifiEnterpriseConfig does not detect changes for identical configs. 183 */ 184 @Test testWifiEnterpriseConfigNoChanges()185 public void testWifiEnterpriseConfigNoChanges() { 186 EnterpriseConfig eapConfig = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 187 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 188 .setIdentity("username", "password") 189 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 190 191 // Just to be clear that check is not against the same object 192 EnterpriseConfig eapConfigSame = new EnterpriseConfig(WifiEnterpriseConfig.Eap.TTLS) 193 .setPhase2(WifiEnterpriseConfig.Phase2.MSCHAPV2) 194 .setIdentity("username", "password") 195 .setCaCerts(new X509Certificate[]{FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 196 197 assertFalse(WifiConfigurationUtil.hasEnterpriseConfigChanged(eapConfig.enterpriseConfig, 198 eapConfigSame.enterpriseConfig)); 199 } 200 201 /** 202 * Verify that the validate method successfully validates good WifiConfigurations with ASCII 203 * values. 204 */ 205 @Test testValidatePositiveCases_Ascii()206 public void testValidatePositiveCases_Ascii() { 207 assertTrue(WifiConfigurationUtil.validate( 208 WifiConfigurationTestUtil.createOpenNetwork(), 209 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 210 assertTrue(WifiConfigurationUtil.validate( 211 WifiConfigurationTestUtil.createPskNetwork(), 212 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 213 assertTrue(WifiConfigurationUtil.validate( 214 WifiConfigurationTestUtil.createWepNetwork(), 215 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 216 assertTrue(WifiConfigurationUtil.validate( 217 WifiConfigurationTestUtil.createEapNetwork(), 218 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 219 assertTrue(WifiConfigurationUtil.validate( 220 WifiConfigurationTestUtil.createOweNetwork(), 221 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 222 assertTrue(WifiConfigurationUtil.validate( 223 WifiConfigurationTestUtil.createSaeNetwork(), 224 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 225 assertTrue(WifiConfigurationUtil.validate( 226 WifiConfigurationTestUtil.createEapSuiteBNetwork(), 227 WifiConfigurationUtil.VALIDATE_FOR_ADD)); 228 } 229 230 /** 231 * Verify that the validate method successfully validates good WifiConfigurations with hex 232 * values. 233 */ 234 @Test testValidatePositiveCases_Hex()235 public void testValidatePositiveCases_Hex() { 236 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 237 config.SSID = "abcd1234555a"; 238 config.preSharedKey = "abcd123455151234556788990034556667332345667322344556676743233445"; 239 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 240 } 241 242 /** 243 * Verify that the validate method validates WifiConfiguration with masked psk string only for 244 * an update. 245 */ 246 @Test testValidatePositiveCases_MaskedPskString()247 public void testValidatePositiveCases_MaskedPskString() { 248 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 249 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 250 251 config.preSharedKey = WifiConfigurationUtil.PASSWORD_MASK; 252 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 253 assertTrue(WifiConfigurationUtil.validate( 254 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 255 } 256 257 /** 258 * Verify that the validate method validates WifiConfiguration with null ssid only for an 259 * update. 260 */ 261 @Test testValidatePositiveCases_OnlyUpdateIgnoresNullSsid()262 public void testValidatePositiveCases_OnlyUpdateIgnoresNullSsid() { 263 WifiConfiguration config = new WifiConfiguration(); 264 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 265 assertTrue(WifiConfigurationUtil.validate( 266 config, WifiConfigurationUtil.VALIDATE_FOR_UPDATE)); 267 } 268 269 /** 270 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 271 */ 272 @Test testValidateNegativeCases_BadAsciiSsidLength()273 public void testValidateNegativeCases_BadAsciiSsidLength() { 274 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 275 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 276 277 config.SSID = "\"abcdfefeeretretyetretetetetetrertertrsreqwrwe\""; 278 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 279 config.SSID = "\"\""; 280 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 281 } 282 283 /** 284 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 285 */ 286 @Test testValidateNegativeCases_BadUtf8SsidLength()287 public void testValidateNegativeCases_BadUtf8SsidLength() { 288 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 289 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 290 291 config.SSID = "\"가하아너너ㅓ저저ㅓ어아아다자저ㅓ더타아어어러두어\""; 292 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 293 config.SSID = "\"\""; 294 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 295 } 296 297 /** 298 * Verify that the validate method fails to validate WifiConfiguration with malformed ssid 299 * string. 300 */ 301 @Test testValidateNegativeCases_MalformedAsciiSsidString()302 public void testValidateNegativeCases_MalformedAsciiSsidString() { 303 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 304 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 305 306 config.SSID = "\"ab"; 307 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 308 } 309 310 /** 311 * Verify that the validate method fails to validate WifiConfiguration with bad ssid length. 312 */ 313 @Test testValidateNegativeCases_BadHexSsidLength()314 public void testValidateNegativeCases_BadHexSsidLength() { 315 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 316 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 317 318 config.SSID = "abcdfe012345632423343543453456464545656464545646454ace34534545634535"; 319 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 320 config.SSID = ""; 321 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 322 } 323 324 /** 325 * Verify that the validate method fails to validate WifiConfiguration with malformed ssid 326 * string. 327 */ 328 @Test testValidateNegativeCases_MalformedHexSsidString()329 public void testValidateNegativeCases_MalformedHexSsidString() { 330 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 331 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 332 333 config.SSID = "hello"; 334 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 335 } 336 337 /** 338 * Verify that the validate method fails to validate WifiConfiguration with bad psk length. 339 */ 340 @Test testValidateNegativeCases_BadAsciiPskLength()341 public void testValidateNegativeCases_BadAsciiPskLength() { 342 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 343 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 344 345 config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq" 346 + "weqe\""; 347 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 348 config.preSharedKey = "\"454\""; 349 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 350 } 351 352 /** 353 * Verify that the validate method fails to validate WifiConfiguration with bad sae length. 354 */ 355 @Test testValidateNegativeCases_BadAsciiSaeLength()356 public void testValidateNegativeCases_BadAsciiSaeLength() { 357 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 358 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 359 360 config.preSharedKey = "\"abcdffeeretretyetreteteteabe34tetrertertrsraaaaaaaaaaa345eqwrweewq" 361 + "weqe\""; 362 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 363 config.preSharedKey = "\"\""; 364 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 365 } 366 367 /** 368 * Verify that the validate method fails to validate WifiConfiguration with malformed psk 369 * string. 370 */ 371 @Test testValidateNegativeCases_MalformedAsciiPskString()372 public void testValidateNegativeCases_MalformedAsciiPskString() { 373 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 374 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 375 376 config.preSharedKey = "\"abcdfefeeretrety"; 377 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 378 } 379 380 /** 381 * Verify that the validate method fails to validate WifiConfiguration with malformed sae 382 * string. 383 */ 384 @Test testValidateNegativeCases_MalformedAsciiSaeString()385 public void testValidateNegativeCases_MalformedAsciiSaeString() { 386 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 387 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 388 389 config.preSharedKey = "\"abcdfefeeretrety"; 390 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 391 } 392 393 /** 394 * Verify that the validate method fails to validate WifiConfiguration with bad psk length. 395 */ 396 @Test testValidateNegativeCases_BadHexPskLength()397 public void testValidateNegativeCases_BadHexPskLength() { 398 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 399 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 400 401 config.preSharedKey = "abcd123456788990013453445345465465476546"; 402 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 403 config.preSharedKey = ""; 404 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 405 } 406 407 /** 408 * Verify that the validate method fails to validate WifiConfiguration with malformed psk 409 * string. 410 */ 411 @Test testValidateNegativeCases_MalformedHexPskString()412 public void testValidateNegativeCases_MalformedHexPskString() { 413 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 414 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 415 416 config.preSharedKey = "adbdfgretrtyrtyrty"; 417 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 418 } 419 420 /** 421 * Verify that the validate method fails to validate WifiConfiguration with malformed sae 422 * string. 423 */ 424 @Test testValidateNegativeCases_MalformedHexSaeString()425 public void testValidateNegativeCases_MalformedHexSaeString() { 426 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 427 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 428 429 config.preSharedKey = "adbdfgretrtyrtyrty"; 430 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 431 } 432 433 /** 434 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 435 */ 436 @Test testValidateNegativeCases_BadKeyMgmtPskEap()437 public void testValidateNegativeCases_BadKeyMgmtPskEap() { 438 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 439 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 440 441 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 442 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 443 } 444 445 /** 446 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 447 */ 448 @Test testValidateNegativeCases_BadKeyMgmtOpenPsk()449 public void testValidateNegativeCases_BadKeyMgmtOpenPsk() { 450 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 451 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 452 453 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 454 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 455 } 456 457 /** 458 * Verify that the validate method fails to validate WifiConfiguration with bad key mgmt values. 459 */ 460 @Test testValidateNegativeCases_BadKeyMgmt()461 public void testValidateNegativeCases_BadKeyMgmt() { 462 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 463 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 464 465 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 466 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 467 } 468 469 /** 470 * Verify that the validate method fails to validate WifiConfiguration with bad ipconfiguration 471 * values. 472 */ 473 @Test testValidateNegativeCases_BadIpconfiguration()474 public void testValidateNegativeCases_BadIpconfiguration() { 475 WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork(); 476 IpConfiguration ipConfig = 477 WifiConfigurationTestUtil.createStaticIpConfigurationWithPacProxy(); 478 config.setIpConfiguration(ipConfig); 479 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 480 481 ipConfig.setStaticIpConfiguration(null); 482 config.setIpConfiguration(ipConfig); 483 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 484 } 485 486 /** 487 * Verify that the validate method fails to validate WifiConfiguration with bad KeyMgmt value. 488 */ 489 @Test testValidateNegativeCases_InvalidKeyMgmt()490 public void testValidateNegativeCases_InvalidKeyMgmt() { 491 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 492 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 493 494 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.FT_EAP + 1); 495 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 496 } 497 498 /** 499 * Verify that the validate method fails to validate WifiConfiguration with bad Protocol value. 500 */ 501 @Test testValidateNegativeCases_InvalidProtocol()502 public void testValidateNegativeCases_InvalidProtocol() { 503 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 504 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 505 506 config.allowedProtocols.set(3); 507 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 508 } 509 510 /** 511 * Verify that the validate method fails to validate WifiConfiguration with bad AuthAlgorithm 512 * value. 513 */ 514 @Test testValidateNegativeCases_InvalidAuthAlgorithm()515 public void testValidateNegativeCases_InvalidAuthAlgorithm() { 516 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 517 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 518 519 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP + 3); 520 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 521 } 522 523 /** 524 * Verify that the validate method fails to validate WifiConfiguration with bad GroupCipher 525 * value. 526 */ 527 @Test testValidateNegativeCases_InvalidGroupCipher()528 public void testValidateNegativeCases_InvalidGroupCipher() { 529 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 530 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 531 532 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.GTK_NOT_USED + 2); 533 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 534 } 535 536 /** 537 * Verify that the validate method fails to validate WifiConfiguration with bad PairwiseCipher 538 * value. 539 */ 540 @Test testValidateNegativeCases_InvalidPairwiseCipher()541 public void testValidateNegativeCases_InvalidPairwiseCipher() { 542 WifiConfiguration config = WifiConfigurationTestUtil.createOpenNetwork(); 543 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 544 545 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP + 2); 546 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 547 } 548 549 /** 550 * Verify that the validate method fails to validate WifiConfiguration with malformed sae 551 * string. 552 */ 553 @Test testValidateNegativeCases_SaeMissingPmf()554 public void testValidateNegativeCases_SaeMissingPmf() { 555 WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork(); 556 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 557 558 config.requirePMF = false; 559 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 560 } 561 562 /** 563 * Verify that the validate method fails to validate WifiConfiguration with malformed owe 564 * string. 565 */ 566 @Test testValidateNegativeCases_OweMissingPmf()567 public void testValidateNegativeCases_OweMissingPmf() { 568 WifiConfiguration config = WifiConfigurationTestUtil.createOweNetwork(); 569 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 570 571 config.requirePMF = false; 572 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 573 } 574 575 /** 576 * Verify that the validate method fails to validate WifiConfiguration with malformed suiteb 577 * string. 578 */ 579 @Test testValidateNegativeCases_SuitebMissingPmf()580 public void testValidateNegativeCases_SuitebMissingPmf() { 581 WifiConfiguration config = WifiConfigurationTestUtil.createEapSuiteBNetwork(); 582 assertTrue(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 583 584 config.requirePMF = false; 585 assertFalse(WifiConfigurationUtil.validate(config, WifiConfigurationUtil.VALIDATE_FOR_ADD)); 586 } 587 588 /** 589 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 590 * only ssid pattern set. 591 */ 592 @Test testValidateNetworkSpecifierPositiveCases_SsidPattern()593 public void testValidateNetworkSpecifierPositiveCases_SsidPattern() { 594 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 595 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 596 Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS), 597 WifiConfigurationTestUtil.createOpenNetwork(), 598 TEST_UID, TEST_PACKAGE); 599 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 600 } 601 602 /** 603 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 604 * only bssid pattern set. 605 */ 606 @Test testValidateNetworkSpecifierPositiveCases_BssidPattern()607 public void testValidateNetworkSpecifierPositiveCases_BssidPattern() { 608 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 609 new PatternMatcher(".*", PatternMatcher.PATTERN_SIMPLE_GLOB), 610 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 611 WifiConfigurationTestUtil.createOpenNetwork(), 612 TEST_UID, TEST_PACKAGE); 613 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 614 } 615 616 /** 617 * Verify that the validate method successfully validates good WifiNetworkSpecifier with 618 * both ssid & bssid patterns set. 619 */ 620 @Test testValidateNetworkSpecifierPositiveCases_BothSsidPatternAndBssidPattern()621 public void testValidateNetworkSpecifierPositiveCases_BothSsidPatternAndBssidPattern() { 622 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 623 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 624 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.BROADCAST_ADDRESS), 625 WifiConfigurationTestUtil.createOpenNetwork(), 626 TEST_UID, TEST_PACKAGE); 627 assertTrue(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 628 } 629 630 /** 631 * Verify that the validate method fails to validate WifiNetworkSpecifier with no 632 * ssid/bssid info. 633 */ 634 @Test testValidateNetworkSpecifierNegativeCases_NoSsidBssid()635 public void testValidateNetworkSpecifierNegativeCases_NoSsidBssid() { 636 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 637 new PatternMatcher(".*", PatternMatcher.PATTERN_SIMPLE_GLOB), 638 Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS), 639 WifiConfigurationTestUtil.createOpenNetwork(), 640 TEST_UID, TEST_PACKAGE); 641 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 642 } 643 644 /** 645 * Verify that the validate method fails to validate WifiNetworkSpecifier with invalid SSID 646 * match pattern. 647 */ 648 @Test testValidateNetworkSpecifierNegativeCases_MatchNoneSsidPattern()649 public void testValidateNetworkSpecifierNegativeCases_MatchNoneSsidPattern() { 650 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 651 new PatternMatcher("", PatternMatcher.PATTERN_LITERAL), 652 Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS), 653 WifiConfigurationTestUtil.createOpenNetwork(), 654 TEST_UID, TEST_PACKAGE); 655 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 656 } 657 658 /** 659 * Verify that the validate method fails to validate WifiNetworkSpecifier with illegal 660 * pattern. 661 */ 662 @Test testValidateNetworkSpecifierNegativeCases_MatchNoneBssidPattern()663 public void testValidateNetworkSpecifierNegativeCases_MatchNoneBssidPattern() { 664 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 665 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 666 Pair.create(MacAddress.BROADCAST_ADDRESS, MacAddress.BROADCAST_ADDRESS), 667 WifiConfigurationTestUtil.createOpenNetwork(), 668 TEST_UID, TEST_PACKAGE); 669 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 670 } 671 672 /** 673 * Verify that the validate method fails to validate WifiNetworkSpecifier with illegal 674 * pattern. 675 */ 676 @Test testValidateNetworkSpecifierNegativeCases_InvalidBssidPattern()677 public void testValidateNetworkSpecifierNegativeCases_InvalidBssidPattern() { 678 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 679 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_LITERAL), 680 Pair.create(MacAddress.fromString(TEST_BSSID), MacAddress.ALL_ZEROS_ADDRESS), 681 WifiConfigurationTestUtil.createOpenNetwork(), 682 TEST_UID, TEST_PACKAGE); 683 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 684 } 685 686 /** 687 * Verify that the validate method fails to validate WifiNetworkSpecifier with SSID pattern 688 * for hidden network. 689 */ 690 @Test testValidateNetworkSpecifierNegativeCases_NoSsidPatternForHiddenNetwork()691 public void testValidateNetworkSpecifierNegativeCases_NoSsidPatternForHiddenNetwork() { 692 WifiNetworkSpecifier specifier = new WifiNetworkSpecifier( 693 new PatternMatcher(TEST_SSID, PatternMatcher.PATTERN_PREFIX), 694 Pair.create(MacAddress.ALL_ZEROS_ADDRESS, MacAddress.ALL_ZEROS_ADDRESS), 695 WifiConfigurationTestUtil.createOpenHiddenNetwork(), 696 TEST_UID, TEST_PACKAGE); 697 assertFalse(WifiConfigurationUtil.validateNetworkSpecifier(specifier)); 698 } 699 700 /** 701 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 702 * for an open network using {@link WifiConfigurationUtil#createPnoNetwork( 703 * WifiConfiguration)}. 704 */ 705 @Test testCreatePnoNetworkWithOpenNetwork()706 public void testCreatePnoNetworkWithOpenNetwork() { 707 WifiConfiguration network = WifiConfigurationTestUtil.createOpenNetwork(); 708 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 709 WifiConfigurationUtil.createPnoNetwork(network); 710 assertEquals(network.SSID, pnoNetwork.ssid); 711 assertEquals( 712 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 713 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 714 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_OPEN, pnoNetwork.authBitField); 715 } 716 717 /** 718 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 719 * for an open hidden network using {@link WifiConfigurationUtil#createPnoNetwork( 720 * WifiConfiguration)}. 721 */ 722 @Test testCreatePnoNetworkWithOpenHiddenNetwork()723 public void testCreatePnoNetworkWithOpenHiddenNetwork() { 724 WifiConfiguration network = WifiConfigurationTestUtil.createOpenHiddenNetwork(); 725 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 726 WifiConfigurationUtil.createPnoNetwork(network); 727 assertEquals(network.SSID, pnoNetwork.ssid); 728 assertEquals( 729 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 730 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND 731 | WifiScanner.PnoSettings.PnoNetwork.FLAG_DIRECTED_SCAN, pnoNetwork.flags); 732 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_OPEN, pnoNetwork.authBitField); 733 } 734 735 /** 736 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 737 * for a PSK network using {@link WifiConfigurationUtil#createPnoNetwork(WifiConfiguration) 738 * }. 739 */ 740 @Test testCreatePnoNetworkWithPskNetwork()741 public void testCreatePnoNetworkWithPskNetwork() { 742 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(); 743 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 744 WifiConfigurationUtil.createPnoNetwork(network); 745 assertEquals(network.SSID, pnoNetwork.ssid); 746 assertEquals( 747 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 748 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 749 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_PSK, pnoNetwork.authBitField); 750 } 751 752 /** 753 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 754 * objects have the same parameters. 755 */ 756 @Test testIsSameNetworkReturnsTrueOnSameNetwork()757 public void testIsSameNetworkReturnsTrueOnSameNetwork() { 758 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 759 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 760 assertTrue(WifiConfigurationUtil.isSameNetwork(network, network1)); 761 } 762 763 /** 764 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 765 * objects have the same parameters but different network selection BSSID's. 766 */ 767 @Test testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentBSSID()768 public void testIsSameNetworkReturnsTrueOnSameNetworkWithDifferentBSSID() { 769 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 770 network.getNetworkSelectionStatus().setNetworkSelectionBSSID(TEST_BSSID); 771 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 772 network1.getNetworkSelectionStatus().setNetworkSelectionBSSID(TEST_BSSID_1); 773 assertTrue(WifiConfigurationUtil.isSameNetwork(network, network1)); 774 } 775 776 /** 777 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 778 * objects have the different SSIDs. 779 */ 780 @Test testIsSameNetworkReturnsFalseOnDifferentSSID()781 public void testIsSameNetworkReturnsFalseOnDifferentSSID() { 782 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 783 WifiConfiguration network1 = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID_1); 784 assertFalse(WifiConfigurationUtil.isSameNetwork(network, network1)); 785 } 786 787 /** 788 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 789 * objects have the different security type. 790 */ 791 @Test testIsSameNetworkReturnsFalseOnDifferentSecurityType()792 public void testIsSameNetworkReturnsFalseOnDifferentSecurityType() { 793 WifiConfiguration network = WifiConfigurationTestUtil.createPskNetwork(TEST_SSID); 794 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 795 assertFalse(WifiConfigurationUtil.isSameNetwork(network, network1)); 796 } 797 798 /** 799 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 800 * objects have the different EAP identity. 801 */ 802 @Test testIsSameNetworkReturnsFalseOnDifferentEapIdentity()803 public void testIsSameNetworkReturnsFalseOnDifferentEapIdentity() { 804 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 805 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 806 network1.enterpriseConfig.setIdentity("Identity1"); 807 network2.enterpriseConfig.setIdentity("Identity2"); 808 assertFalse(WifiConfigurationUtil.isSameNetwork(network1, network2)); 809 } 810 811 /** 812 * Verify that WifiConfigurationUtil.isSameNetwork returns false when two WifiConfiguration 813 * objects have the different EAP anonymous identity. 814 */ 815 @Test testIsSameNetworkReturnsFalseOnDifferentEapAnonymousIdentity()816 public void testIsSameNetworkReturnsFalseOnDifferentEapAnonymousIdentity() { 817 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 818 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 819 network1.enterpriseConfig.setAnonymousIdentity("Identity1"); 820 network2.enterpriseConfig.setAnonymousIdentity("Identity2"); 821 assertFalse(WifiConfigurationUtil.isSameNetwork(network1, network2)); 822 } 823 824 /** 825 * Verify that WifiConfigurationUtil.isSameNetwork returns true when two WifiConfiguration 826 * objects have the different EAP anonymous(pseudonym) identity in EAP-SIM. 827 */ 828 @Test testIsSameNetworkReturnsTrueOnDifferentEapAnonymousIdentityInEapSim()829 public void testIsSameNetworkReturnsTrueOnDifferentEapAnonymousIdentityInEapSim() { 830 WifiConfiguration network1 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 831 WifiConfiguration network2 = WifiConfigurationTestUtil.createEapNetwork(TEST_SSID); 832 network1.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); 833 network2.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM); 834 network1.enterpriseConfig.setAnonymousIdentity("Identity1"); 835 network2.enterpriseConfig.setAnonymousIdentity("Identity2"); 836 assertTrue(WifiConfigurationUtil.isSameNetwork(network1, network2)); 837 } 838 839 /** 840 * Verify the instance of {@link android.net.wifi.WifiScanner.PnoSettings.PnoNetwork} created 841 * for a EAP network using {@link WifiConfigurationUtil#createPnoNetwork(WifiConfiguration) 842 * }. 843 */ 844 @Test testCreatePnoNetworkWithEapNetwork()845 public void testCreatePnoNetworkWithEapNetwork() { 846 WifiConfiguration network = WifiConfigurationTestUtil.createEapNetwork(); 847 WifiScanner.PnoSettings.PnoNetwork pnoNetwork = 848 WifiConfigurationUtil.createPnoNetwork(network); 849 assertEquals(network.SSID, pnoNetwork.ssid); 850 assertEquals( 851 WifiScanner.PnoSettings.PnoNetwork.FLAG_A_BAND 852 | WifiScanner.PnoSettings.PnoNetwork.FLAG_G_BAND, pnoNetwork.flags); 853 assertEquals(WifiScanner.PnoSettings.PnoNetwork.AUTH_CODE_EAPOL, pnoNetwork.authBitField); 854 } 855 856 /** 857 * Verify that the generalized 858 * {@link com.android.server.wifi.WifiConfigurationUtil.WifiConfigurationComparator} 859 * can be used to sort a List given a 'compareNetworkWithSameStatus' method. 860 */ 861 @Test testPnoListComparator()862 public void testPnoListComparator() { 863 List<WifiConfiguration> networks = new ArrayList<>(); 864 final WifiConfiguration enabledNetwork1 = WifiConfigurationTestUtil.createEapNetwork(); 865 enabledNetwork1.getNetworkSelectionStatus().setNetworkSelectionStatus( 866 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED); 867 final WifiConfiguration enabledNetwork2 = WifiConfigurationTestUtil.createEapNetwork(); 868 enabledNetwork2.getNetworkSelectionStatus().setNetworkSelectionStatus( 869 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_ENABLED); 870 final WifiConfiguration tempDisabledNetwork1 = WifiConfigurationTestUtil.createEapNetwork(); 871 tempDisabledNetwork1.getNetworkSelectionStatus().setNetworkSelectionStatus( 872 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); 873 final WifiConfiguration tempDisabledNetwork2 = WifiConfigurationTestUtil.createEapNetwork(); 874 tempDisabledNetwork2.getNetworkSelectionStatus().setNetworkSelectionStatus( 875 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_TEMPORARY_DISABLED); 876 WifiConfiguration permDisabledNetwork = WifiConfigurationTestUtil.createEapNetwork(); 877 permDisabledNetwork.getNetworkSelectionStatus().setNetworkSelectionStatus( 878 WifiConfiguration.NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED); 879 880 // Add all the networks to the list. 881 networks.add(tempDisabledNetwork1); 882 networks.add(enabledNetwork1); 883 networks.add(permDisabledNetwork); 884 networks.add(tempDisabledNetwork2); 885 networks.add(enabledNetwork2); 886 887 // Prefer |enabledNetwork1| over |enabledNetwork2| and |tempDisabledNetwork1| over 888 // |tempDisabledNetwork2|. 889 WifiConfigurationUtil.WifiConfigurationComparator comparator = 890 new WifiConfigurationUtil.WifiConfigurationComparator() { 891 @Override 892 public int compareNetworksWithSameStatus( 893 WifiConfiguration a, WifiConfiguration b) { 894 if (a == enabledNetwork1 && b == enabledNetwork2) { 895 return -1; 896 } else if (b == enabledNetwork1 && a == enabledNetwork2) { 897 return 1; 898 } else if (a == tempDisabledNetwork1 && b == tempDisabledNetwork2) { 899 return -1; 900 } else if (b == tempDisabledNetwork1 && a == tempDisabledNetwork2) { 901 return 1; 902 } 903 return 0; 904 } 905 }; 906 Collections.sort(networks, comparator); 907 908 // Now ensure that the networks were sorted correctly. 909 assertEquals(enabledNetwork1, networks.get(0)); 910 assertEquals(enabledNetwork2, networks.get(1)); 911 assertEquals(tempDisabledNetwork1, networks.get(2)); 912 assertEquals(tempDisabledNetwork2, networks.get(3)); 913 assertEquals(permDisabledNetwork, networks.get(4)); 914 } 915 916 /** 917 * Verifies that when the existing configuration is null and macRandomizationSetting in the 918 * newConfig is the default value, then hasMacRandomizationSettingsChanged returns false. 919 */ 920 @Test testHasMacRandomizationSettingsChangedNullExistingConfigDefaultNewConfig()921 public void testHasMacRandomizationSettingsChangedNullExistingConfigDefaultNewConfig() { 922 WifiConfiguration newConfig = new WifiConfiguration(); 923 assertFalse(WifiConfigurationUtil.hasMacRandomizationSettingsChanged(null, newConfig)); 924 } 925 926 /** 927 * Verifies that when the existing configuration is null and macRandomizationSetting in the 928 * newConfig is not the default value, then hasMacRandomizationSettingsChanged returns true. 929 */ 930 @Test testHasMacRandomizationSettingsChangedNullExistingConfigModifiedNewConfig()931 public void testHasMacRandomizationSettingsChangedNullExistingConfigModifiedNewConfig() { 932 WifiConfiguration newConfig = new WifiConfiguration(); 933 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 934 assertTrue(WifiConfigurationUtil.hasMacRandomizationSettingsChanged(null, newConfig)); 935 } 936 937 /** 938 * Verifies that when macRandomizationSetting in the newConfig is different from existingConfig 939 * hasMacRandomizationSettingsChanged returns true. 940 */ 941 @Test testHasMacRandomizationSettingsChangedFieldsDifferent()942 public void testHasMacRandomizationSettingsChangedFieldsDifferent() { 943 WifiConfiguration existingConfig = new WifiConfiguration(); 944 WifiConfiguration newConfig = new WifiConfiguration(); 945 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 946 assertTrue(WifiConfigurationUtil.hasMacRandomizationSettingsChanged( 947 existingConfig, newConfig)); 948 } 949 950 /** 951 * Verifies that when macRandomizationSetting in the newConfig is the same as existingConfig 952 * hasMacRandomizationSettingsChanged returns false. 953 */ 954 @Test testHasMacRandomizationSettingsChangedFieldsSame()955 public void testHasMacRandomizationSettingsChangedFieldsSame() { 956 WifiConfiguration existingConfig = new WifiConfiguration(); 957 existingConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 958 WifiConfiguration newConfig = new WifiConfiguration(); 959 newConfig.macRandomizationSetting = WifiConfiguration.RANDOMIZATION_NONE; 960 assertFalse(WifiConfigurationUtil.hasMacRandomizationSettingsChanged( 961 existingConfig, newConfig)); 962 } 963 964 private static class EnterpriseConfig { 965 public String eap; 966 public String phase2; 967 public String identity; 968 public String password; 969 public X509Certificate[] caCerts; 970 public WifiEnterpriseConfig enterpriseConfig; 971 EnterpriseConfig(int eapMethod)972 EnterpriseConfig(int eapMethod) { 973 enterpriseConfig = new WifiEnterpriseConfig(); 974 enterpriseConfig.setEapMethod(eapMethod); 975 eap = WifiEnterpriseConfig.Eap.strings[eapMethod]; 976 } 977 setPhase2(int phase2Method)978 public EnterpriseConfig setPhase2(int phase2Method) { 979 enterpriseConfig.setPhase2Method(phase2Method); 980 phase2 = "auth=" + WifiEnterpriseConfig.Phase2.strings[phase2Method]; 981 return this; 982 } 983 setIdentity(String identity, String password)984 public EnterpriseConfig setIdentity(String identity, String password) { 985 enterpriseConfig.setIdentity(identity); 986 enterpriseConfig.setPassword(password); 987 this.identity = identity; 988 this.password = password; 989 return this; 990 } 991 setCaCerts(X509Certificate[] certs)992 public EnterpriseConfig setCaCerts(X509Certificate[] certs) { 993 enterpriseConfig.setCaCertificates(certs); 994 caCerts = certs; 995 return this; 996 } 997 } 998 } 999