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.net.IpConfiguration; 22 import android.net.LinkAddress; 23 import android.net.NetworkUtils; 24 import android.net.ProxyInfo; 25 import android.net.StaticIpConfiguration; 26 import android.net.wifi.WifiConfiguration; 27 import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; 28 import android.net.wifi.WifiEnterpriseConfig; 29 import android.text.TextUtils; 30 31 import java.net.InetAddress; 32 import java.security.cert.X509Certificate; 33 import java.util.Arrays; 34 import java.util.List; 35 36 /** 37 * Helper for creating and populating WifiConfigurations in unit tests. 38 */ 39 public class WifiConfigurationTestUtil { 40 /** 41 * These values are used to describe AP's security setting. One AP can support multiple of them, 42 * only if there is no conflict. 43 */ 44 public static final int SECURITY_NONE = 0; 45 public static final int SECURITY_WEP = 1 << 0; 46 public static final int SECURITY_PSK = 1 << 1; 47 public static final int SECURITY_EAP = 1 << 2; 48 49 /** 50 * These values are used to describe ip configuration parameters for a network. 51 */ 52 public static final int STATIC_IP_ASSIGNMENT = 0; 53 public static final int DHCP_IP_ASSIGNMENT = 1; 54 public static final int STATIC_PROXY_SETTING = 0; 55 public static final int PAC_PROXY_SETTING = 1; 56 public static final int NONE_PROXY_SETTING = 2; 57 58 /** 59 * These are constants used to generate predefined WifiConfiguration objects. 60 */ 61 public static final int TEST_NETWORK_ID = -1; 62 public static final int TEST_UID = 5; 63 public static final String TEST_SSID = "WifiConfigurationTestUtilSSID"; 64 public static final String TEST_PSK = "\"WifiConfigurationTestUtilPsk\""; 65 public static final String[] TEST_WEP_KEYS = 66 {"\"WifiConfigurationTestUtilWep1\"", "\"WifiConfigurationTestUtilWep2\"", 67 "45342312ab", "45342312ab45342312ab34ac12"}; 68 public static final String TEST_EAP_PASSWORD = "WifiConfigurationTestUtilEapPassword"; 69 public static final int TEST_WEP_TX_KEY_INDEX = 1; 70 public static final String TEST_FQDN = "WifiConfigurationTestUtilFQDN"; 71 public static final String TEST_PROVIDER_FRIENDLY_NAME = 72 "WifiConfigurationTestUtilFriendlyName"; 73 public static final String TEST_STATIC_IP_LINK_ADDRESS = "192.168.48.2"; 74 public static final int TEST_STATIC_IP_LINK_PREFIX_LENGTH = 8; 75 public static final String TEST_STATIC_IP_GATEWAY_ADDRESS = "192.168.48.1"; 76 public static final String[] TEST_STATIC_IP_DNS_SERVER_ADDRESSES = 77 new String[]{"192.168.48.1", "192.168.48.10"}; 78 public static final String TEST_STATIC_PROXY_HOST = "192.168.48.1"; 79 public static final int TEST_STATIC_PROXY_PORT = 8000; 80 public static final String TEST_STATIC_PROXY_EXCLUSION_LIST = ""; 81 public static final String TEST_PAC_PROXY_LOCATION = "http://"; 82 public static final String TEST_CA_CERT_ALIAS = "WifiConfigurationTestUtilCaCertAlias"; 83 84 private static final int MAX_SSID_LENGTH = 32; 85 /** 86 * Index used to assign unique SSIDs for the generation of predefined WifiConfiguration objects. 87 */ 88 private static int sNetworkIndex = 0; 89 90 /** 91 * Construct a {@link android.net.wifi.WifiConfiguration}. 92 * @param networkId the configuration's networkId 93 * @param uid the configuration's creator uid 94 * @param ssid the configuration's ssid 95 * @param shared whether the configuration is shared with other users on the device 96 * @param enabled whether the configuration is enabled 97 * @param fqdn the configuration's FQDN (Hotspot 2.0 only) 98 * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only) 99 * @return the constructed {@link android.net.wifi.WifiConfiguration} 100 */ 101 public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid, 102 boolean shared, boolean enabled, String fqdn, String providerFriendlyName) { 103 final WifiConfiguration config = new WifiConfiguration(); 104 config.SSID = ssid; 105 config.networkId = networkId; 106 config.creatorUid = uid; 107 config.shared = shared; 108 config.status = enabled ? WifiConfiguration.Status.ENABLED 109 : WifiConfiguration.Status.DISABLED; 110 config.FQDN = fqdn; 111 config.providerFriendlyName = providerFriendlyName; 112 return config; 113 } 114 115 /** 116 * Construct a {@link android.net.wifi.WifiConfiguration}. 117 * @param networkId the configuration's networkId 118 * @param uid the configuration's creator uid 119 * @param ssid the configuration's ssid 120 * @param shared whether the configuration is shared with other users on the device 121 * @param enabled whether the configuration is enabled 122 * @param fqdn the configuration's FQDN (Hotspot 2.0 only) 123 * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only) 124 * @param security the configuration's security type 125 * @return the constructed {@link android.net.wifi.WifiConfiguration} 126 */ 127 public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid, 128 boolean shared, boolean enabled, String fqdn, String providerFriendlyName, 129 int security) { 130 WifiConfiguration config = generateWifiConfig(networkId, uid, ssid, shared, enabled, fqdn, 131 providerFriendlyName); 132 133 if ((security == SECURITY_NONE) || ((security & SECURITY_WEP) != 0)) { 134 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 135 } else { 136 if ((security & SECURITY_PSK) != 0) { 137 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 138 } 139 140 if ((security & SECURITY_EAP) != 0) { 141 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 142 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); 143 config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS); 144 } 145 } 146 return config; 147 } 148 149 /** 150 * Construct a {@link android.net.IpConfiguration }. 151 * @param ipAssignmentType One of {@link #STATIC_IP_ASSIGNMENT} or {@link #DHCP_IP_ASSIGNMENT}. 152 * @param proxySettingType One of {@link #STATIC_PROXY_SETTING} or {@link #PAC_PROXY_SETTING} or 153 * {@link #NONE_PROXY_SETTING}. 154 * @param linkAddress static ip address string. 155 * @param linkPrefixLength static ip address prefix length. 156 * @param gatewayAddress static gateway address. 157 * @param dnsServerAddresses list of dns servers for static ip configuration. 158 * @param proxyHost Static proxy server address. 159 * @param proxyPort Static proxy server port. 160 * @param proxyExclusionList Static proxy exclusion list. 161 * @param pacProxyPath Pac proxy server path. 162 * @return the constructed {@link android.net.IpConfiguration} 163 */ 164 public static IpConfiguration generateIpConfig( 165 int ipAssignmentType, int proxySettingType, String linkAddress, int linkPrefixLength, 166 String gatewayAddress, String[] dnsServerAddresses, String proxyHost, 167 int proxyPort, String proxyExclusionList, String pacProxyPath) { 168 StaticIpConfiguration staticIpConfiguration = null; 169 ProxyInfo proxyInfo = null; 170 IpConfiguration.IpAssignment ipAssignment = IpConfiguration.IpAssignment.UNASSIGNED; 171 IpConfiguration.ProxySettings proxySettings = IpConfiguration.ProxySettings.UNASSIGNED; 172 173 if (ipAssignmentType == STATIC_IP_ASSIGNMENT) { 174 staticIpConfiguration = new StaticIpConfiguration(); 175 if (!TextUtils.isEmpty(linkAddress)) { 176 LinkAddress linkAddr = 177 new LinkAddress( 178 NetworkUtils.numericToInetAddress(linkAddress), linkPrefixLength); 179 staticIpConfiguration.ipAddress = linkAddr; 180 } 181 182 if (!TextUtils.isEmpty(gatewayAddress)) { 183 InetAddress gatewayAddr = 184 NetworkUtils.numericToInetAddress(gatewayAddress); 185 staticIpConfiguration.gateway = gatewayAddr; 186 } 187 if (dnsServerAddresses != null) { 188 for (String dnsServerAddress : dnsServerAddresses) { 189 if (!TextUtils.isEmpty(dnsServerAddress)) { 190 staticIpConfiguration.dnsServers.add( 191 NetworkUtils.numericToInetAddress(dnsServerAddress)); 192 } 193 194 } 195 } 196 ipAssignment = IpConfiguration.IpAssignment.STATIC; 197 } else if (ipAssignmentType == DHCP_IP_ASSIGNMENT) { 198 ipAssignment = IpConfiguration.IpAssignment.DHCP; 199 } 200 201 if (proxySettingType == STATIC_PROXY_SETTING) { 202 proxyInfo = new ProxyInfo(proxyHost, proxyPort, proxyExclusionList); 203 proxySettings = IpConfiguration.ProxySettings.STATIC; 204 } else if (proxySettingType == PAC_PROXY_SETTING) { 205 proxyInfo = new ProxyInfo(pacProxyPath); 206 proxySettings = IpConfiguration.ProxySettings.PAC; 207 } else if (proxySettingType == NONE_PROXY_SETTING) { 208 proxySettings = IpConfiguration.ProxySettings.NONE; 209 } 210 return new IpConfiguration(ipAssignment, proxySettings, staticIpConfiguration, proxyInfo); 211 } 212 213 /** 214 * Create a new SSID for the the network being created. 215 */ 216 private static String createNewSSID() { 217 String ssid = TEST_SSID + sNetworkIndex++; 218 assertTrue(ssid.length() <= MAX_SSID_LENGTH); 219 return "\"" + ssid + "\""; 220 } 221 222 /** 223 * Helper methods to generate predefined WifiConfiguration objects of the required type. These 224 * use a static index to avoid duplicate configurations. 225 */ 226 public static WifiConfiguration createOpenNetwork() { 227 return generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null, 228 null, SECURITY_NONE); 229 } 230 231 public static WifiConfiguration createOpenHiddenNetwork() { 232 WifiConfiguration configuration = createOpenNetwork(); 233 configuration.hiddenSSID = true; 234 return configuration; 235 } 236 237 public static WifiConfiguration createPskNetwork() { 238 WifiConfiguration configuration = 239 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null, 240 null, SECURITY_PSK); 241 configuration.preSharedKey = TEST_PSK; 242 return configuration; 243 } 244 245 public static WifiConfiguration createPskNetwork(String ssid) { 246 WifiConfiguration configuration = 247 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null, 248 null, SECURITY_PSK); 249 configuration.preSharedKey = TEST_PSK; 250 return configuration; 251 } 252 253 254 public static WifiConfiguration createPskHiddenNetwork() { 255 WifiConfiguration configuration = createPskNetwork(); 256 configuration.hiddenSSID = true; 257 return configuration; 258 } 259 260 public static WifiConfiguration createWepNetwork() { 261 WifiConfiguration configuration = 262 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null, 263 null, SECURITY_WEP); 264 configuration.wepKeys = TEST_WEP_KEYS; 265 configuration.wepTxKeyIndex = TEST_WEP_TX_KEY_INDEX; 266 return configuration; 267 } 268 269 public static WifiConfiguration createWepHiddenNetwork() { 270 WifiConfiguration configuration = createWepNetwork(); 271 configuration.hiddenSSID = true; 272 return configuration; 273 } 274 275 276 public static WifiConfiguration createWepNetworkWithSingleKey() { 277 WifiConfiguration configuration = 278 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null, 279 null, SECURITY_WEP); 280 configuration.wepKeys[0] = TEST_WEP_KEYS[0]; 281 configuration.wepTxKeyIndex = 0; 282 return configuration; 283 } 284 285 286 public static WifiConfiguration createEapNetwork() { 287 WifiConfiguration configuration = 288 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, 289 null, null, SECURITY_EAP); 290 return configuration; 291 } 292 293 public static WifiConfiguration createEapNetwork(String ssid) { 294 WifiConfiguration configuration = 295 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, 296 null, null, SECURITY_EAP); 297 return configuration; 298 } 299 300 301 public static WifiConfiguration createEapNetwork(int eapMethod, int phase2Method) { 302 WifiConfiguration configuration = 303 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, 304 null, null, SECURITY_EAP); 305 configuration.enterpriseConfig.setEapMethod(eapMethod); 306 configuration.enterpriseConfig.setPhase2Method(phase2Method); 307 return configuration; 308 } 309 310 public static WifiConfiguration createPasspointNetwork() { 311 WifiConfiguration configuration = 312 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, 313 TEST_FQDN, TEST_PROVIDER_FRIENDLY_NAME, SECURITY_EAP); 314 return configuration; 315 } 316 317 public static IpConfiguration createStaticIpConfigurationWithPacProxy() { 318 return generateIpConfig( 319 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING, 320 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 321 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 322 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 323 TEST_PAC_PROXY_LOCATION); 324 } 325 326 public static IpConfiguration createStaticIpConfigurationWithStaticProxy() { 327 return generateIpConfig( 328 STATIC_IP_ASSIGNMENT, STATIC_PROXY_SETTING, 329 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 330 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 331 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 332 TEST_PAC_PROXY_LOCATION); 333 } 334 335 public static IpConfiguration createPartialStaticIpConfigurationWithPacProxy() { 336 return generateIpConfig( 337 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING, 338 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 339 null, null, 340 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 341 TEST_PAC_PROXY_LOCATION); 342 } 343 344 public static IpConfiguration createDHCPIpConfigurationWithPacProxy() { 345 return generateIpConfig( 346 DHCP_IP_ASSIGNMENT, PAC_PROXY_SETTING, 347 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 348 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 349 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 350 TEST_PAC_PROXY_LOCATION); 351 } 352 353 public static IpConfiguration createDHCPIpConfigurationWithStaticProxy() { 354 return generateIpConfig( 355 DHCP_IP_ASSIGNMENT, STATIC_PROXY_SETTING, 356 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 357 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 358 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 359 TEST_PAC_PROXY_LOCATION); 360 } 361 362 public static IpConfiguration createDHCPIpConfigurationWithNoProxy() { 363 return generateIpConfig( 364 DHCP_IP_ASSIGNMENT, NONE_PROXY_SETTING, 365 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 366 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 367 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST, 368 TEST_PAC_PROXY_LOCATION); 369 } 370 371 /** 372 * Creates an IP configuration with specific parameters. 373 * @param proxySetting Must be one of {@link WifiConfigurationTestUtil#STATIC_PROXY_SETTING}, 374 * {@link WifiConfigurationTestUtil#PAC_PROXY_SETTING}, 375 * {@link WifiConfigurationTestUtil#NONE_PROXY_SETTING} 376 */ 377 public static IpConfiguration createDHCPIpConfigurationWithSpecificProxy( 378 int proxySetting, 379 String staticProxyHost, 380 int staticProxyPort, 381 String staticProxyExclusionList, 382 String pacProxyLocation) { 383 return generateIpConfig( 384 DHCP_IP_ASSIGNMENT, proxySetting, 385 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH, 386 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES, 387 staticProxyHost, staticProxyPort, staticProxyExclusionList, 388 pacProxyLocation); 389 } 390 391 // TODO: These enterprise configurations may need more parameters set. 392 public static WifiEnterpriseConfig createPEAPWifiEnterpriseConfigWithGTCPhase2() { 393 WifiEnterpriseConfig config = new WifiEnterpriseConfig(); 394 config.setEapMethod(WifiEnterpriseConfig.Eap.PEAP); 395 config.setPhase2Method(WifiEnterpriseConfig.Phase2.GTC); 396 config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "PEAP"}); 397 config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 398 return config; 399 } 400 401 public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithNonePhase2() { 402 WifiEnterpriseConfig config = new WifiEnterpriseConfig(); 403 config.setEapMethod(WifiEnterpriseConfig.Eap.TLS); 404 config.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE); 405 config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "TLS"}); 406 config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1}); 407 return config; 408 } 409 410 public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithAkaPhase2() { 411 WifiEnterpriseConfig config = new WifiEnterpriseConfig(); 412 config.setEapMethod(WifiEnterpriseConfig.Eap.TLS); 413 config.setPhase2Method(WifiEnterpriseConfig.Phase2.AKA); 414 return config; 415 } 416 417 /** 418 * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore 419 * and config store. 420 */ 421 private static void assertCommonConfigurationElementsEqual( 422 WifiConfiguration expected, WifiConfiguration actual) { 423 assertNotNull(expected); 424 assertNotNull(actual); 425 assertEquals(expected.SSID, actual.SSID); 426 assertEquals(expected.BSSID, actual.BSSID); 427 assertEquals(expected.preSharedKey, actual.preSharedKey); 428 assertEquals(expected.wepKeys, actual.wepKeys); 429 assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex); 430 assertEquals(expected.hiddenSSID, actual.hiddenSSID); 431 assertEquals(expected.requirePMF, actual.requirePMF); 432 assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement); 433 assertEquals(expected.allowedProtocols, actual.allowedProtocols); 434 assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms); 435 assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers); 436 assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers); 437 assertEquals(expected.shared, actual.shared); 438 assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration()); 439 } 440 441 /** 442 * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved 443 * fpr backup/restore. 444 */ 445 public static void assertConfigurationEqualForBackup( 446 WifiConfiguration expected, WifiConfiguration actual) { 447 assertCommonConfigurationElementsEqual(expected, actual); 448 } 449 450 /** 451 * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for 452 * config store. 453 */ 454 public static void assertConfigurationEqualForConfigStore( 455 WifiConfiguration expected, WifiConfiguration actual) { 456 assertCommonConfigurationElementsEqual(expected, actual); 457 assertEquals(expected.status, actual.status); 458 assertEquals(expected.FQDN, actual.FQDN); 459 assertEquals(expected.providerFriendlyName, actual.providerFriendlyName); 460 assertTrue(Arrays.equals(expected.roamingConsortiumIds, actual.roamingConsortiumIds)); 461 assertEquals(expected.linkedConfigurations, actual.linkedConfigurations); 462 assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress); 463 assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess); 464 assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected); 465 assertEquals(expected.userApproved, actual.userApproved); 466 assertEquals(expected.meteredHint, actual.meteredHint); 467 assertEquals(expected.useExternalScores, actual.useExternalScores); 468 assertEquals(expected.numAssociation, actual.numAssociation); 469 assertEquals(expected.creatorUid, actual.creatorUid); 470 assertEquals(expected.creatorName, actual.creatorName); 471 assertEquals(expected.creationTime, actual.creationTime); 472 assertEquals(expected.lastUpdateUid, actual.lastUpdateUid); 473 assertEquals(expected.lastUpdateName, actual.lastUpdateName); 474 assertEquals(expected.lastConnectUid, actual.lastConnectUid); 475 assertEquals(expected.updateTime, actual.updateTime); 476 assertEquals(expected.isLegacyPasspointConfig, actual.isLegacyPasspointConfig); 477 assertNetworkSelectionStatusEqualForConfigStore( 478 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus()); 479 assertWifiEnterpriseConfigEqualForConfigStore( 480 expected.enterpriseConfig, actual.enterpriseConfig); 481 } 482 483 /** 484 * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are 485 * saved into internal database by WifiConfigurationManager for network additions/updates. 486 */ 487 public static void assertConfigurationEqualForConfigManagerAddOrUpdate( 488 WifiConfiguration expected, WifiConfiguration actual) { 489 assertCommonConfigurationElementsEqual(expected, actual); 490 assertEquals(expected.FQDN, actual.FQDN); 491 assertEquals(expected.providerFriendlyName, actual.providerFriendlyName); 492 assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected); 493 assertEquals(expected.meteredHint, actual.meteredHint); 494 assertEquals(expected.useExternalScores, actual.useExternalScores); 495 assertEquals(expected.ephemeral, actual.ephemeral); 496 assertEquals(expected.creatorUid, actual.creatorUid); 497 assertEquals(expected.creatorName, actual.creatorName); 498 assertEquals(expected.creationTime, actual.creationTime); 499 assertEquals(expected.lastUpdateUid, actual.lastUpdateUid); 500 assertEquals(expected.lastUpdateName, actual.lastUpdateName); 501 assertEquals(expected.updateTime, actual.updateTime); 502 assertNetworkSelectionStatusEqualForConfigStore( 503 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus()); 504 assertWifiEnterpriseConfigEqualForConfigStore( 505 expected.enterpriseConfig, actual.enterpriseConfig); 506 } 507 508 /** 509 * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are 510 * saved into wpa_supplicant by SupplicantStaNetwork. 511 */ 512 public static void assertConfigurationEqualForSupplicant( 513 WifiConfiguration expected, WifiConfiguration actual) { 514 assertNotNull(expected); 515 assertNotNull(actual); 516 assertEquals(expected.SSID, actual.SSID); 517 assertEquals(expected.getNetworkSelectionStatus().getNetworkSelectionBSSID(), 518 actual.getNetworkSelectionStatus().getNetworkSelectionBSSID()); 519 assertEquals(expected.preSharedKey, actual.preSharedKey); 520 assertEquals(expected.wepKeys, actual.wepKeys); 521 assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex); 522 assertEquals(expected.hiddenSSID, actual.hiddenSSID); 523 assertEquals(expected.requirePMF, actual.requirePMF); 524 assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement); 525 assertEquals(expected.allowedProtocols, actual.allowedProtocols); 526 assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms); 527 assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers); 528 assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers); 529 assertWifiEnterpriseConfigEqualForConfigStore( 530 expected.enterpriseConfig, actual.enterpriseConfig); 531 } 532 533 /** 534 * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator 535 * which is used in QNS tests for comparing the network selections. 536 * This importantly checks that the networkId's of the 2 configs are equal. 537 */ 538 public static void assertConfigurationEqual( 539 WifiConfiguration expected, WifiConfiguration actual) { 540 assertCommonConfigurationElementsEqual(expected, actual); 541 assertEquals(expected.networkId, actual.networkId); 542 } 543 544 /** 545 * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved 546 * for config store. 547 */ 548 public static void assertNetworkSelectionStatusEqualForConfigStore( 549 NetworkSelectionStatus expected, NetworkSelectionStatus actual) { 550 if (expected.isNetworkTemporaryDisabled()) { 551 // Temporarily disabled networks are enabled when persisted. 552 assertEquals( 553 NetworkSelectionStatus.NETWORK_SELECTION_ENABLED, 554 actual.getNetworkSelectionStatus()); 555 assertEquals( 556 NetworkSelectionStatus.NETWORK_SELECTION_ENABLE, 557 actual.getNetworkSelectionDisableReason()); 558 } else { 559 assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus()); 560 assertEquals( 561 expected.getNetworkSelectionDisableReason(), 562 actual.getNetworkSelectionDisableReason()); 563 } 564 assertEquals(expected.getConnectChoice(), actual.getConnectChoice()); 565 assertEquals(expected.getConnectChoiceTimestamp(), actual.getConnectChoiceTimestamp()); 566 assertEquals(expected.getHasEverConnected(), actual.getHasEverConnected()); 567 } 568 569 /** 570 * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved 571 * for config store. 572 */ 573 public static void assertWifiEnterpriseConfigEqualForConfigStore( 574 WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) { 575 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY), 576 actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY)); 577 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY), 578 actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY)); 579 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY), 580 actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY)); 581 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY), 582 actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY)); 583 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY), 584 actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY)); 585 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY), 586 actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY)); 587 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY), 588 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY)); 589 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY), 590 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY)); 591 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY), 592 actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY)); 593 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY), 594 actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY)); 595 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY), 596 actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY)); 597 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY), 598 actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY)); 599 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.REALM_KEY), 600 actual.getFieldValue(WifiEnterpriseConfig.REALM_KEY)); 601 assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PLMN_KEY), 602 actual.getFieldValue(WifiEnterpriseConfig.PLMN_KEY)); 603 assertEquals(expected.getEapMethod(), actual.getEapMethod()); 604 assertEquals(expected.getPhase2Method(), actual.getPhase2Method()); 605 } 606 607 /** 608 * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements 609 * saved for backup/restore. 610 */ 611 public static void assertConfigurationsEqualForBackup( 612 List<WifiConfiguration> expected, List<WifiConfiguration> actual) { 613 assertEquals(expected.size(), actual.size()); 614 for (WifiConfiguration expectedConfiguration : expected) { 615 String expectedConfigKey = expectedConfiguration.configKey(); 616 boolean didCompare = false; 617 for (WifiConfiguration actualConfiguration : actual) { 618 String actualConfigKey = actualConfiguration.configKey(); 619 if (actualConfigKey.equals(expectedConfigKey)) { 620 assertConfigurationEqualForBackup( 621 expectedConfiguration, actualConfiguration); 622 didCompare = true; 623 } 624 } 625 assertTrue(didCompare); 626 } 627 } 628 629 /** 630 * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements 631 * that are saved into internal database by WifiConfigurationManager for network 632 * additions/updates. 633 */ 634 public static void assertConfigurationsEqualForConfigManagerAddOrUpdate( 635 List<WifiConfiguration> expected, List<WifiConfiguration> actual) { 636 assertEquals(expected.size(), actual.size()); 637 for (WifiConfiguration expectedConfiguration : expected) { 638 String expectedConfigKey = expectedConfiguration.configKey(); 639 boolean didCompare = false; 640 for (WifiConfiguration actualConfiguration : actual) { 641 String actualConfigKey = actualConfiguration.configKey(); 642 if (actualConfigKey.equals(expectedConfigKey)) { 643 assertConfigurationEqualForConfigManagerAddOrUpdate( 644 expectedConfiguration, actualConfiguration); 645 didCompare = true; 646 } 647 } 648 assertTrue(didCompare); 649 } 650 } 651 652 /** 653 * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements 654 * saved for config store. 655 */ 656 public static void assertConfigurationsEqualForConfigStore( 657 List<WifiConfiguration> expected, List<WifiConfiguration> actual) { 658 assertEquals(expected.size(), actual.size()); 659 for (WifiConfiguration expectedConfiguration : expected) { 660 String expectedConfigKey = expectedConfiguration.configKey(); 661 boolean didCompare = false; 662 for (WifiConfiguration actualConfiguration : actual) { 663 String actualConfigKey = actualConfiguration.configKey(); 664 if (actualConfigKey.equals(expectedConfigKey)) { 665 assertConfigurationEqualForConfigStore( 666 expectedConfiguration, actualConfiguration); 667 didCompare = true; 668 } 669 } 670 assertTrue(didCompare); 671 } 672 } 673 } 674