WifiConfigurationTestUtil.java revision cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3
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 WifiConfiguration createPasspointNetwork() {
283        WifiConfiguration configuration =
284                generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
285                        TEST_FQDN, TEST_PROVIDER_FRIENDLY_NAME, SECURITY_EAP);
286        return configuration;
287    }
288
289    public static IpConfiguration createStaticIpConfigurationWithPacProxy() {
290        return generateIpConfig(
291                STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
292                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
293                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
294                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
295                TEST_PAC_PROXY_LOCATION);
296    }
297
298    public static IpConfiguration createStaticIpConfigurationWithStaticProxy() {
299        return generateIpConfig(
300                STATIC_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
301                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
302                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
303                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
304                TEST_PAC_PROXY_LOCATION);
305    }
306
307    public static IpConfiguration createPartialStaticIpConfigurationWithPacProxy() {
308        return generateIpConfig(
309                STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
310                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
311                null, null,
312                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
313                TEST_PAC_PROXY_LOCATION);
314    }
315
316    public static IpConfiguration createDHCPIpConfigurationWithPacProxy() {
317        return generateIpConfig(
318                DHCP_IP_ASSIGNMENT, PAC_PROXY_SETTING,
319                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
320                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
321                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
322                TEST_PAC_PROXY_LOCATION);
323    }
324
325    public static IpConfiguration createDHCPIpConfigurationWithStaticProxy() {
326        return generateIpConfig(
327                DHCP_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
328                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
329                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
330                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
331                TEST_PAC_PROXY_LOCATION);
332    }
333
334    public static IpConfiguration createDHCPIpConfigurationWithNoProxy() {
335        return generateIpConfig(
336                DHCP_IP_ASSIGNMENT, NONE_PROXY_SETTING,
337                TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
338                TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
339                TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
340                TEST_PAC_PROXY_LOCATION);
341    }
342
343    /**
344     * Creates an IP configuration with specific parameters.
345     * @param proxySetting Must be one of {@link WifiConfigurationTestUtil#STATIC_PROXY_SETTING},
346     * {@link WifiConfigurationTestUtil#PAC_PROXY_SETTING},
347     * {@link WifiConfigurationTestUtil#NONE_PROXY_SETTING}
348     */
349    public static IpConfiguration createDHCPIpConfigurationWithSpecificProxy(
350            int proxySetting,
351            String staticProxyHost,
352            int staticProxyPort,
353            String staticProxyExclusionList,
354            String pacProxyLocation) {
355        return generateIpConfig(
356                DHCP_IP_ASSIGNMENT, proxySetting,
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                staticProxyHost, staticProxyPort, staticProxyExclusionList,
360                pacProxyLocation);
361    }
362
363    // TODO: These enterprise configurations may need more parameters set.
364    public static WifiEnterpriseConfig createPEAPWifiEnterpriseConfigWithGTCPhase2() {
365        WifiEnterpriseConfig config = new WifiEnterpriseConfig();
366        config.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
367        config.setPhase2Method(WifiEnterpriseConfig.Phase2.GTC);
368        config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "PEAP"});
369        config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
370        return config;
371    }
372
373    public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithNonePhase2() {
374        WifiEnterpriseConfig config = new WifiEnterpriseConfig();
375        config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
376        config.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
377        config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "TLS"});
378        config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
379        return config;
380    }
381
382    public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithAkaPhase2() {
383        WifiEnterpriseConfig config = new WifiEnterpriseConfig();
384        config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
385        config.setPhase2Method(WifiEnterpriseConfig.Phase2.AKA);
386        return config;
387    }
388
389    /**
390     * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore
391     * and config store.
392     */
393    private static void assertCommonConfigurationElementsEqual(
394            WifiConfiguration expected, WifiConfiguration actual) {
395        assertNotNull(expected);
396        assertNotNull(actual);
397        assertEquals(expected.SSID, actual.SSID);
398        assertEquals(expected.BSSID, actual.BSSID);
399        assertEquals(expected.preSharedKey, actual.preSharedKey);
400        assertEquals(expected.wepKeys, actual.wepKeys);
401        assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
402        assertEquals(expected.hiddenSSID, actual.hiddenSSID);
403        assertEquals(expected.requirePMF, actual.requirePMF);
404        assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
405        assertEquals(expected.allowedProtocols, actual.allowedProtocols);
406        assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
407        assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
408        assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
409        assertEquals(expected.shared, actual.shared);
410        assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration());
411    }
412
413    /**
414     * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
415     * fpr backup/restore.
416     */
417    public static void assertConfigurationEqualForBackup(
418            WifiConfiguration expected, WifiConfiguration actual) {
419        assertCommonConfigurationElementsEqual(expected, actual);
420    }
421
422    /**
423     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for
424     * config store.
425     */
426    public static void assertConfigurationEqualForConfigStore(
427            WifiConfiguration expected, WifiConfiguration actual) {
428        assertCommonConfigurationElementsEqual(expected, actual);
429        assertEquals(expected.status, actual.status);
430        assertEquals(expected.FQDN, actual.FQDN);
431        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
432        assertEquals(expected.linkedConfigurations, actual.linkedConfigurations);
433        assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress);
434        assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess);
435        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
436        assertEquals(expected.userApproved, actual.userApproved);
437        assertEquals(expected.meteredHint, actual.meteredHint);
438        assertEquals(expected.useExternalScores, actual.useExternalScores);
439        assertEquals(expected.numAssociation, actual.numAssociation);
440        assertEquals(expected.creatorUid, actual.creatorUid);
441        assertEquals(expected.creatorName, actual.creatorName);
442        assertEquals(expected.creationTime, actual.creationTime);
443        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
444        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
445        assertEquals(expected.lastConnectUid, actual.lastConnectUid);
446        assertEquals(expected.updateTime, actual.updateTime);
447        assertNetworkSelectionStatusEqualForConfigStore(
448                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
449        assertWifiEnterpriseConfigEqualForConfigStore(
450                expected.enterpriseConfig, actual.enterpriseConfig);
451    }
452
453    /**
454     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
455     * saved into internal database by WifiConfigurationManager for network additions/updates.
456     */
457    public static void assertConfigurationEqualForConfigManagerAddOrUpdate(
458            WifiConfiguration expected, WifiConfiguration actual) {
459        assertCommonConfigurationElementsEqual(expected, actual);
460        assertEquals(expected.FQDN, actual.FQDN);
461        assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
462        assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
463        assertEquals(expected.meteredHint, actual.meteredHint);
464        assertEquals(expected.useExternalScores, actual.useExternalScores);
465        assertEquals(expected.ephemeral, actual.ephemeral);
466        assertEquals(expected.creatorUid, actual.creatorUid);
467        assertEquals(expected.creatorName, actual.creatorName);
468        assertEquals(expected.creationTime, actual.creationTime);
469        assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
470        assertEquals(expected.lastUpdateName, actual.lastUpdateName);
471        assertEquals(expected.updateTime, actual.updateTime);
472        assertNetworkSelectionStatusEqualForConfigStore(
473                expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
474        assertWifiEnterpriseConfigEqualForConfigStore(
475                expected.enterpriseConfig, actual.enterpriseConfig);
476    }
477
478    /**
479     * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
480     * saved into wpa_supplicant by SupplicantStaNetwork.
481     */
482    public static void assertConfigurationEqualForSupplicant(
483            WifiConfiguration expected, WifiConfiguration actual) {
484        assertNotNull(expected);
485        assertNotNull(actual);
486        assertEquals(expected.SSID, actual.SSID);
487        assertEquals(expected.getNetworkSelectionStatus().getNetworkSelectionBSSID(),
488                actual.getNetworkSelectionStatus().getNetworkSelectionBSSID());
489        assertEquals(expected.preSharedKey, actual.preSharedKey);
490        assertEquals(expected.wepKeys, actual.wepKeys);
491        assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
492        assertEquals(expected.hiddenSSID, actual.hiddenSSID);
493        assertEquals(expected.requirePMF, actual.requirePMF);
494        assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
495        assertEquals(expected.allowedProtocols, actual.allowedProtocols);
496        assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
497        assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
498        assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
499        assertWifiEnterpriseConfigEqualForConfigStore(
500                expected.enterpriseConfig, actual.enterpriseConfig);
501    }
502
503    /**
504     * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator
505     * which is used in QNS tests for comparing the network selections.
506     * This importantly checks that the networkId's of the 2 configs are equal.
507     */
508    public static void assertConfigurationEqual(
509            WifiConfiguration expected, WifiConfiguration actual) {
510        assertCommonConfigurationElementsEqual(expected, actual);
511        assertEquals(expected.networkId, actual.networkId);
512    }
513
514    /**
515     * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved
516     * for config store.
517     */
518    public static void assertNetworkSelectionStatusEqualForConfigStore(
519            NetworkSelectionStatus expected, NetworkSelectionStatus actual) {
520        if (expected.isNetworkTemporaryDisabled()) {
521            // Temporarily disabled networks are enabled when persisted.
522            assertEquals(
523                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLED,
524                    actual.getNetworkSelectionStatus());
525            assertEquals(
526                    NetworkSelectionStatus.NETWORK_SELECTION_ENABLE,
527                    actual.getNetworkSelectionDisableReason());
528        } else {
529            assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
530            assertEquals(
531                    expected.getNetworkSelectionDisableReason(),
532                    actual.getNetworkSelectionDisableReason());
533        }
534        assertEquals(expected.getConnectChoice(), actual.getConnectChoice());
535        assertEquals(expected.getConnectChoiceTimestamp(), actual.getConnectChoiceTimestamp());
536        assertEquals(expected.getHasEverConnected(), actual.getHasEverConnected());
537    }
538
539    /**
540     * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved
541     * for config store.
542     */
543    public static void assertWifiEnterpriseConfigEqualForConfigStore(
544            WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) {
545        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY),
546                actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
547        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY),
548                actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
549        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY),
550                actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
551        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY),
552                actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
553        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY),
554                actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
555        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY),
556                actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
557        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY),
558                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
559        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY),
560                actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
561        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY),
562                actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
563        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY),
564                actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
565        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY),
566                actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
567        assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY),
568                actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
569        assertEquals(expected.getEapMethod(), actual.getEapMethod());
570        assertEquals(expected.getPhase2Method(), actual.getPhase2Method());
571    }
572
573    /**
574     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
575     * saved for backup/restore.
576     */
577    public static void assertConfigurationsEqualForBackup(
578            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
579        assertEquals(expected.size(), actual.size());
580        for (WifiConfiguration expectedConfiguration : expected) {
581            String expectedConfigKey = expectedConfiguration.configKey();
582            boolean didCompare = false;
583            for (WifiConfiguration actualConfiguration : actual) {
584                String actualConfigKey = actualConfiguration.configKey();
585                if (actualConfigKey.equals(expectedConfigKey)) {
586                    assertConfigurationEqualForBackup(
587                            expectedConfiguration, actualConfiguration);
588                    didCompare = true;
589                }
590            }
591            assertTrue(didCompare);
592        }
593    }
594
595    /**
596     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
597     * that are saved into internal database by WifiConfigurationManager for network
598     * additions/updates.
599     */
600    public static void assertConfigurationsEqualForConfigManagerAddOrUpdate(
601            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
602        assertEquals(expected.size(), actual.size());
603        for (WifiConfiguration expectedConfiguration : expected) {
604            String expectedConfigKey = expectedConfiguration.configKey();
605            boolean didCompare = false;
606            for (WifiConfiguration actualConfiguration : actual) {
607                String actualConfigKey = actualConfiguration.configKey();
608                if (actualConfigKey.equals(expectedConfigKey)) {
609                    assertConfigurationEqualForConfigManagerAddOrUpdate(
610                            expectedConfiguration, actualConfiguration);
611                    didCompare = true;
612                }
613            }
614            assertTrue(didCompare);
615        }
616    }
617
618    /**
619     * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
620     * saved for config store.
621     */
622    public static void assertConfigurationsEqualForConfigStore(
623            List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
624        assertEquals(expected.size(), actual.size());
625        for (WifiConfiguration expectedConfiguration : expected) {
626            String expectedConfigKey = expectedConfiguration.configKey();
627            boolean didCompare = false;
628            for (WifiConfiguration actualConfiguration : actual) {
629                String actualConfigKey = actualConfiguration.configKey();
630                if (actualConfigKey.equals(expectedConfigKey)) {
631                    assertConfigurationEqualForConfigStore(
632                            expectedConfiguration, actualConfiguration);
633                    didCompare = true;
634                }
635            }
636            assertTrue(didCompare);
637        }
638    }
639}
640