WifiConfigurationTestUtil.java revision d83ef7d8c4948afc328d8ef0e746b32c195f271e
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
17package com.android.server.wifi;
18
19import static org.junit.Assert.*;
20
21import android.net.IpConfiguration;
22import android.net.LinkAddress;
23import android.net.NetworkUtils;
24import android.net.ProxyInfo;
25import android.net.StaticIpConfiguration;
26import android.net.wifi.FakeKeys;
27import android.net.wifi.WifiConfiguration;
28import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
29import android.net.wifi.WifiEnterpriseConfig;
30import android.text.TextUtils;
31
32import java.net.InetAddress;
33import java.security.cert.X509Certificate;
34import java.util.List;
35
36/**
37 * Helper for creating and populating WifiConfigurations in unit tests.
38 */
39public 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 = 1;
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                    "WifiConfigurationTestUtilWep3", "WifiConfigurationTestUtilWep3"};
68    public static final int TEST_WEP_TX_KEY_INDEX = 1;
69    public static final String TEST_FQDN = "WifiConfigurationTestUtilFQDN";
70    public static final String TEST_PROVIDER_FRIENDLY_NAME =
71            "WifiConfigurationTestUtilFriendlyName";
72    public static final String TEST_STATIC_IP_LINK_ADDRESS = "192.168.48.2";
73    public static final int TEST_STATIC_IP_LINK_PREFIX_LENGTH = 8;
74    public static final String TEST_STATIC_IP_GATEWAY_ADDRESS = "192.168.48.1";
75    public static final String[] TEST_STATIC_IP_DNS_SERVER_ADDRESSES =
76            new String[]{"192.168.48.1", "192.168.48.10"};
77    public static final String TEST_STATIC_PROXY_HOST = "192.168.48.1";
78    public static final int TEST_STATIC_PROXY_PORT = 8000;
79    public static final String TEST_STATIC_PROXY_EXCLUSION_LIST = "";
80    public static final String TEST_PAC_PROXY_LOCATION = "http://";
81    public static final String TEST_CA_CERT_ALIAS = "WifiConfigurationTestUtilCaCertAlias";
82
83    private static final int MAX_SSID_LENGTH = 32;
84    /**
85     * Index used to assign unique SSIDs for the generation of predefined WifiConfiguration objects.
86     */
87    private static int sNetworkIndex = 0;
88
89    /**
90     * Construct a {@link android.net.wifi.WifiConfiguration}.
91     * @param networkId the configuration's networkId
92     * @param uid the configuration's creator uid
93     * @param ssid the configuration's ssid
94     * @param shared whether the configuration is shared with other users on the device
95     * @param enabled whether the configuration is enabled
96     * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
97     * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
98     * @return the constructed {@link android.net.wifi.WifiConfiguration}
99     */
100    public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
101            boolean shared, boolean enabled, String fqdn, String providerFriendlyName) {
102        final WifiConfiguration config = new WifiConfiguration();
103        config.SSID = ssid;
104        config.networkId = networkId;
105        config.creatorUid = uid;
106        config.shared = shared;
107        config.status = enabled ? WifiConfiguration.Status.ENABLED
108                : WifiConfiguration.Status.DISABLED;
109        if (fqdn != null) {
110            config.FQDN = fqdn;
111            config.providerFriendlyName = providerFriendlyName;
112            config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM);
113        }
114        return config;
115    }
116
117    /**
118     * Construct a {@link android.net.wifi.WifiConfiguration}.
119     * @param networkId the configuration's networkId
120     * @param uid the configuration's creator uid
121     * @param ssid the configuration's ssid
122     * @param shared whether the configuration is shared with other users on the device
123     * @param enabled whether the configuration is enabled
124     * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
125     * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
126     * @param security the configuration's security type
127     * @return the constructed {@link android.net.wifi.WifiConfiguration}
128     */
129    public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
130            boolean shared, boolean enabled, String fqdn, String providerFriendlyName,
131            int security) {
132        WifiConfiguration config = generateWifiConfig(networkId, uid, ssid, shared, enabled, fqdn,
133                providerFriendlyName);
134
135        if ((security == SECURITY_NONE) || ((security & SECURITY_WEP) != 0)) {
136            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
137        } else {
138            if ((security & SECURITY_PSK) != 0) {
139                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
140            }
141
142            if ((security & SECURITY_EAP) != 0) {
143                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
144                config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
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 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
229                null, SECURITY_NONE);
230    }
231
232    public static WifiConfiguration createOpenHiddenNetwork() {
233        WifiConfiguration configuration = createOpenNetwork();
234        configuration.hiddenSSID = true;
235        return configuration;
236    }
237
238    public static WifiConfiguration createPskNetwork() {
239        WifiConfiguration configuration =
240                generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
241                        null, SECURITY_PSK);
242        configuration.preSharedKey = TEST_PSK;
243        return configuration;
244    }
245
246    public static WifiConfiguration createPskHiddenNetwork() {
247        WifiConfiguration configuration = createPskNetwork();
248        configuration.hiddenSSID = true;
249        return configuration;
250    }
251
252    public static WifiConfiguration createWepNetwork() {
253        WifiConfiguration configuration =
254                generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
255                        null, SECURITY_WEP);
256        configuration.wepKeys = TEST_WEP_KEYS;
257        configuration.wepTxKeyIndex = TEST_WEP_TX_KEY_INDEX;
258        return configuration;
259    }
260
261    public static WifiConfiguration createWepHiddenNetwork() {
262        WifiConfiguration configuration = createWepNetwork();
263        configuration.hiddenSSID = true;
264        return configuration;
265    }
266
267
268    public static WifiConfiguration createWepNetworkWithSingleKey() {
269        WifiConfiguration configuration =
270                generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
271                        null, SECURITY_WEP);
272        configuration.wepKeys[0] = TEST_WEP_KEYS[0];
273        configuration.wepTxKeyIndex = 0;
274        return configuration;
275    }
276
277
278    public static WifiConfiguration createEapNetwork() {
279        WifiConfiguration configuration =
280                generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
281                        TEST_FQDN, TEST_PROVIDER_FRIENDLY_NAME, SECURITY_EAP);
282        return configuration;
283    }
284
285    public static IpConfiguration createStaticIpConfigurationWithPacProxy() {
286        return generateIpConfig(
287                STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
288                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
289                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
290                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
291                TEST_PAC_PROXY_LOCATION);
292    }
293
294    public static IpConfiguration createStaticIpConfigurationWithStaticProxy() {
295        return generateIpConfig(
296                STATIC_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
297                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
298                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
299                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
300                TEST_PAC_PROXY_LOCATION);
301    }
302
303    public static IpConfiguration createPartialStaticIpConfigurationWithPacProxy() {
304        return generateIpConfig(
305                STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
306                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
307                null, null,
308                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
309                TEST_PAC_PROXY_LOCATION);
310    }
311
312    public static IpConfiguration createDHCPIpConfigurationWithPacProxy() {
313        return generateIpConfig(
314                DHCP_IP_ASSIGNMENT, PAC_PROXY_SETTING,
315                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
316                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
317                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
318                TEST_PAC_PROXY_LOCATION);
319    }
320
321    public static IpConfiguration createDHCPIpConfigurationWithStaticProxy() {
322        return generateIpConfig(
323                DHCP_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
324                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
325                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
326                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
327                TEST_PAC_PROXY_LOCATION);
328    }
329
330    public static IpConfiguration createDHCPIpConfigurationWithNoProxy() {
331        return generateIpConfig(
332                DHCP_IP_ASSIGNMENT, NONE_PROXY_SETTING,
333                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
334                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
335                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
336                TEST_PAC_PROXY_LOCATION);
337    }
338
339    // TODO: These enterprise configurations may need more parameters set.
340    public static WifiEnterpriseConfig createPEAPWifiEnterpriseConfigWithGTCPhase2() {
341        WifiEnterpriseConfig config = new WifiEnterpriseConfig();
342        config.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
343        config.setPhase2Method(WifiEnterpriseConfig.Phase2.GTC);
344        config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "PEAP"});
345        config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
346        return config;
347    }
348
349    public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithNonePhase2() {
350        WifiEnterpriseConfig config = new WifiEnterpriseConfig();
351        config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
352        config.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
353        config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "TLS"});
354        config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
355        return config;
356    }
357
358    /**
359     * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore
360     * and config store.
361     */
362    private static void assertCommonConfigurationElementsEqual(
363            WifiConfiguration expected, WifiConfiguration actual) {
364        assertNotNull(expected);
365        assertNotNull(actual);
366        assertEquals(expected.SSID, actual.SSID);
367        assertEquals(expected.BSSID, actual.BSSID);
368        assertEquals(expected.preSharedKey, actual.preSharedKey);
369        assertEquals(expected.wepKeys, actual.wepKeys);
370        assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
371        assertEquals(expected.hiddenSSID, actual.hiddenSSID);
372        assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
373        assertEquals(expected.allowedProtocols, actual.allowedProtocols);
374        assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
375        assertEquals(expected.shared, actual.shared);
376        assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration());
377    }
378
379    /**
380     * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
381     * fpr backup/restore.
382     */
383    public static void assertConfigurationEqualForBackup(
384            WifiConfiguration expected, WifiConfiguration actual) {
385        assertCommonConfigurationElementsEqual(expected, actual);
386    }
387
388    /**
389     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for
390     * config store.
391     */
392    public static void assertConfigurationEqualForConfigStore(
393            WifiConfiguration expected, WifiConfiguration actual) {
394        assertCommonConfigurationElementsEqual(expected, actual);
395        assertEquals(expected.FQDN, actual.FQDN);
396        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
397        assertEquals(expected.linkedConfigurations, actual.linkedConfigurations);
398        assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress);
399        assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess);
400        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
401        assertEquals(expected.userApproved, actual.userApproved);
402        assertEquals(expected.meteredHint, actual.meteredHint);
403        assertEquals(expected.useExternalScores, actual.useExternalScores);
404        assertEquals(expected.numAssociation, actual.numAssociation);
405        assertEquals(expected.creatorUid, actual.creatorUid);
406        assertEquals(expected.creatorName, actual.creatorName);
407        assertEquals(expected.creationTime, actual.creationTime);
408        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
409        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
410        assertEquals(expected.lastConnectUid, actual.lastConnectUid);
411        assertEquals(expected.updateTime, actual.updateTime);
412        assertNetworkSelectionStatusEqualForConfigStore(
413                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
414        assertWifiEnterpriseConfigEqualForConfigStore(
415                expected.enterpriseConfig, actual.enterpriseConfig);
416    }
417
418    /**
419     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
420     * saved into internal database by WifiConfigurationManager for network additions/updates.
421     */
422    public static void assertConfigurationEqualForConfigManagerAddOrUpdate(
423            WifiConfiguration expected, WifiConfiguration actual) {
424        assertCommonConfigurationElementsEqual(expected, actual);
425        assertEquals(expected.FQDN, actual.FQDN);
426        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
427        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
428        assertEquals(expected.meteredHint, actual.meteredHint);
429        assertEquals(expected.useExternalScores, actual.useExternalScores);
430        assertEquals(expected.ephemeral, actual.ephemeral);
431        assertEquals(expected.creatorUid, actual.creatorUid);
432        assertEquals(expected.creatorName, actual.creatorName);
433        assertEquals(expected.creationTime, actual.creationTime);
434        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
435        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
436        assertEquals(expected.updateTime, actual.updateTime);
437        assertNetworkSelectionStatusEqualForConfigStore(
438                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
439        assertWifiEnterpriseConfigEqualForConfigStore(
440                expected.enterpriseConfig, actual.enterpriseConfig);
441    }
442
443    /**
444     * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator
445     * which is used in QNS tests for comparing the network selections.
446     * This importantly checks that the networkId's of the 2 configs are equal.
447     */
448    public static void assertConfigurationEqual(
449            WifiConfiguration expected, WifiConfiguration actual) {
450        assertCommonConfigurationElementsEqual(expected, actual);
451        assertEquals(expected.networkId, actual.networkId);
452    }
453
454    /**
455     * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved
456     * for config store.
457     */
458    public static void assertNetworkSelectionStatusEqualForConfigStore(
459            NetworkSelectionStatus expected, NetworkSelectionStatus actual) {
460        if (expected.isNetworkTemporaryDisabled()) {
461            // Temporarily disabled networks are enabled when persisted.
462            assertEquals(
463                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLED,
464                    actual.getNetworkSelectionStatus());
465            assertEquals(
466                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLE,
467                    actual.getNetworkSelectionDisableReason());
468        } else {
469            assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
470            assertEquals(
471                    expected.getNetworkSelectionDisableReason(),
472                    actual.getNetworkSelectionDisableReason());
473        }
474        assertEquals(expected.getConnectChoice(), actual.getConnectChoice());
475        assertEquals(expected.getConnectChoiceTimestamp(), actual.getConnectChoiceTimestamp());
476        assertEquals(expected.getHasEverConnected(), actual.getHasEverConnected());
477    }
478
479    /**
480     * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved
481     * for config store.
482     */
483    public static void assertWifiEnterpriseConfigEqualForConfigStore(
484            WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) {
485        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY),
486                actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
487        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY),
488                actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
489        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY),
490                actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
491        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY),
492                actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
493        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY),
494                actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
495        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY),
496                actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
497        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY),
498                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
499        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY),
500                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
501        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY),
502                actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
503        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY),
504                actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
505        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY),
506                actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
507        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY),
508                actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
509        assertEquals(expected.getEapMethod(), actual.getEapMethod());
510        assertEquals(expected.getPhase2Method(), actual.getPhase2Method());
511    }
512
513    /**
514     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
515     * saved for backup/restore.
516     */
517    public static void assertConfigurationsEqualForBackup(
518            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
519        assertEquals(expected.size(), actual.size());
520        for (WifiConfiguration expectedConfiguration : expected) {
521            String expectedConfigKey = expectedConfiguration.configKey();
522            boolean didCompare = false;
523            for (WifiConfiguration actualConfiguration : actual) {
524                String actualConfigKey = actualConfiguration.configKey();
525                if (actualConfigKey.equals(expectedConfigKey)) {
526                    assertConfigurationEqualForBackup(
527                            expectedConfiguration, actualConfiguration);
528                    didCompare = true;
529                }
530            }
531            assertTrue(didCompare);
532        }
533    }
534
535    /**
536     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
537     * that are saved into internal database by WifiConfigurationManager for network
538     * additions/updates.
539     */
540    public static void assertConfigurationsEqualForConfigManagerAddOrUpdate(
541            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
542        assertEquals(expected.size(), actual.size());
543        for (WifiConfiguration expectedConfiguration : expected) {
544            String expectedConfigKey = expectedConfiguration.configKey();
545            boolean didCompare = false;
546            for (WifiConfiguration actualConfiguration : actual) {
547                String actualConfigKey = actualConfiguration.configKey();
548                if (actualConfigKey.equals(expectedConfigKey)) {
549                    assertConfigurationEqualForConfigManagerAddOrUpdate(
550                            expectedConfiguration, actualConfiguration);
551                    didCompare = true;
552                }
553            }
554            assertTrue(didCompare);
555        }
556    }
557
558    /**
559     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
560     * saved for config store.
561     */
562    public static void assertConfigurationsEqualForConfigStore(
563            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
564        assertEquals(expected.size(), actual.size());
565        for (WifiConfiguration expectedConfiguration : expected) {
566            String expectedConfigKey = expectedConfiguration.configKey();
567            boolean didCompare = false;
568            for (WifiConfiguration actualConfiguration : actual) {
569                String actualConfigKey = actualConfiguration.configKey();
570                if (actualConfigKey.equals(expectedConfigKey)) {
571                    assertConfigurationEqualForConfigStore(
572                            expectedConfiguration, actualConfiguration);
573                    didCompare = true;
574                }
575            }
576            assertTrue(didCompare);
577        }
578    }
579}
580