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