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