WifiConfigurationTestUtil.java revision 241605aebc6a8f55624026e8b72246bceb1c2ac2
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.requirePMF, actual.requirePMF);
373        assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
374        assertEquals(expected.allowedProtocols, actual.allowedProtocols);
375        assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
376        assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
377        assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
378        assertEquals(expected.shared, actual.shared);
379        assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration());
380    }
381
382    /**
383     * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
384     * fpr backup/restore.
385     */
386    public static void assertConfigurationEqualForBackup(
387            WifiConfiguration expected, WifiConfiguration actual) {
388        assertCommonConfigurationElementsEqual(expected, actual);
389    }
390
391    /**
392     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for
393     * config store.
394     */
395    public static void assertConfigurationEqualForConfigStore(
396            WifiConfiguration expected, WifiConfiguration actual) {
397        assertCommonConfigurationElementsEqual(expected, actual);
398        assertEquals(expected.FQDN, actual.FQDN);
399        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
400        assertEquals(expected.linkedConfigurations, actual.linkedConfigurations);
401        assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress);
402        assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess);
403        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
404        assertEquals(expected.userApproved, actual.userApproved);
405        assertEquals(expected.meteredHint, actual.meteredHint);
406        assertEquals(expected.useExternalScores, actual.useExternalScores);
407        assertEquals(expected.numAssociation, actual.numAssociation);
408        assertEquals(expected.creatorUid, actual.creatorUid);
409        assertEquals(expected.creatorName, actual.creatorName);
410        assertEquals(expected.creationTime, actual.creationTime);
411        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
412        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
413        assertEquals(expected.lastConnectUid, actual.lastConnectUid);
414        assertEquals(expected.updateTime, actual.updateTime);
415        assertNetworkSelectionStatusEqualForConfigStore(
416                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
417        assertWifiEnterpriseConfigEqualForConfigStore(
418                expected.enterpriseConfig, actual.enterpriseConfig);
419    }
420
421    /**
422     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
423     * saved into internal database by WifiConfigurationManager for network additions/updates.
424     */
425    public static void assertConfigurationEqualForConfigManagerAddOrUpdate(
426            WifiConfiguration expected, WifiConfiguration actual) {
427        assertCommonConfigurationElementsEqual(expected, actual);
428        assertEquals(expected.FQDN, actual.FQDN);
429        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
430        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
431        assertEquals(expected.meteredHint, actual.meteredHint);
432        assertEquals(expected.useExternalScores, actual.useExternalScores);
433        assertEquals(expected.ephemeral, actual.ephemeral);
434        assertEquals(expected.creatorUid, actual.creatorUid);
435        assertEquals(expected.creatorName, actual.creatorName);
436        assertEquals(expected.creationTime, actual.creationTime);
437        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
438        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
439        assertEquals(expected.updateTime, actual.updateTime);
440        assertNetworkSelectionStatusEqualForConfigStore(
441                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
442        assertWifiEnterpriseConfigEqualForConfigStore(
443                expected.enterpriseConfig, actual.enterpriseConfig);
444    }
445
446    /**
447     * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator
448     * which is used in QNS tests for comparing the network selections.
449     * This importantly checks that the networkId's of the 2 configs are equal.
450     */
451    public static void assertConfigurationEqual(
452            WifiConfiguration expected, WifiConfiguration actual) {
453        assertCommonConfigurationElementsEqual(expected, actual);
454        assertEquals(expected.networkId, actual.networkId);
455    }
456
457    /**
458     * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved
459     * for config store.
460     */
461    public static void assertNetworkSelectionStatusEqualForConfigStore(
462            NetworkSelectionStatus expected, NetworkSelectionStatus actual) {
463        if (expected.isNetworkTemporaryDisabled()) {
464            // Temporarily disabled networks are enabled when persisted.
465            assertEquals(
466                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLED,
467                    actual.getNetworkSelectionStatus());
468            assertEquals(
469                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLE,
470                    actual.getNetworkSelectionDisableReason());
471        } else {
472            assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
473            assertEquals(
474                    expected.getNetworkSelectionDisableReason(),
475                    actual.getNetworkSelectionDisableReason());
476        }
477        assertEquals(expected.getConnectChoice(), actual.getConnectChoice());
478        assertEquals(expected.getConnectChoiceTimestamp(), actual.getConnectChoiceTimestamp());
479        assertEquals(expected.getHasEverConnected(), actual.getHasEverConnected());
480    }
481
482    /**
483     * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved
484     * for config store.
485     */
486    public static void assertWifiEnterpriseConfigEqualForConfigStore(
487            WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) {
488        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY),
489                actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
490        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY),
491                actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
492        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY),
493                actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
494        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY),
495                actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
496        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY),
497                actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
498        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY),
499                actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
500        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY),
501                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
502        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY),
503                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
504        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY),
505                actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
506        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY),
507                actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
508        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY),
509                actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
510        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY),
511                actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
512        assertEquals(expected.getEapMethod(), actual.getEapMethod());
513        assertEquals(expected.getPhase2Method(), actual.getPhase2Method());
514    }
515
516    /**
517     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
518     * saved for backup/restore.
519     */
520    public static void assertConfigurationsEqualForBackup(
521            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
522        assertEquals(expected.size(), actual.size());
523        for (WifiConfiguration expectedConfiguration : expected) {
524            String expectedConfigKey = expectedConfiguration.configKey();
525            boolean didCompare = false;
526            for (WifiConfiguration actualConfiguration : actual) {
527                String actualConfigKey = actualConfiguration.configKey();
528                if (actualConfigKey.equals(expectedConfigKey)) {
529                    assertConfigurationEqualForBackup(
530                            expectedConfiguration, actualConfiguration);
531                    didCompare = true;
532                }
533            }
534            assertTrue(didCompare);
535        }
536    }
537
538    /**
539     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
540     * that are saved into internal database by WifiConfigurationManager for network
541     * additions/updates.
542     */
543    public static void assertConfigurationsEqualForConfigManagerAddOrUpdate(
544            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
545        assertEquals(expected.size(), actual.size());
546        for (WifiConfiguration expectedConfiguration : expected) {
547            String expectedConfigKey = expectedConfiguration.configKey();
548            boolean didCompare = false;
549            for (WifiConfiguration actualConfiguration : actual) {
550                String actualConfigKey = actualConfiguration.configKey();
551                if (actualConfigKey.equals(expectedConfigKey)) {
552                    assertConfigurationEqualForConfigManagerAddOrUpdate(
553                            expectedConfiguration, actualConfiguration);
554                    didCompare = true;
555                }
556            }
557            assertTrue(didCompare);
558        }
559    }
560
561    /**
562     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
563     * saved for config store.
564     */
565    public static void assertConfigurationsEqualForConfigStore(
566            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
567        assertEquals(expected.size(), actual.size());
568        for (WifiConfiguration expectedConfiguration : expected) {
569            String expectedConfigKey = expectedConfiguration.configKey();
570            boolean didCompare = false;
571            for (WifiConfiguration actualConfiguration : actual) {
572                String actualConfigKey = actualConfiguration.configKey();
573                if (actualConfigKey.equals(expectedConfigKey)) {
574                    assertConfigurationEqualForConfigStore(
575                            expectedConfiguration, actualConfiguration);
576                    didCompare = true;
577                }
578            }
579            assertTrue(didCompare);
580        }
581    }
582}
583