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