1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.server.wifi;
18 
19 import static org.junit.Assert.*;
20 
21 import android.net.InetAddresses;
22 import android.net.IpConfiguration;
23 import android.net.LinkAddress;
24 import android.net.ProxyInfo;
25 import android.net.StaticIpConfiguration;
26 import android.net.Uri;
27 import android.net.wifi.SecurityParams;
28 import android.net.wifi.WifiConfiguration;
29 import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
30 import android.net.wifi.WifiEnterpriseConfig;
31 import android.net.wifi.WifiSsid;
32 import android.text.TextUtils;
33 
34 import com.android.modules.utils.build.SdkLevel;
35 import com.android.server.wifi.util.NativeUtil;
36 
37 import java.net.InetAddress;
38 import java.security.cert.X509Certificate;
39 import java.util.Arrays;
40 import java.util.List;
41 
42 /**
43  * Helper for creating and populating WifiConfigurations in unit tests.
44  */
45 public class WifiConfigurationTestUtil {
46     /**
47      * These values are used to describe AP's security setting. One AP can support multiple of them,
48      * only if there is no conflict.
49      */
50     public static final int SECURITY_NONE = 1 << 0;
51     public static final int SECURITY_WEP =  1 << 1;
52     public static final int SECURITY_PSK =  1 << 2;
53     public static final int SECURITY_EAP =  1 << 3;
54     public static final int SECURITY_SAE =  1 << 4;
55     public static final int SECURITY_OWE =  1 << 5;
56     public static final int SECURITY_EAP_SUITE_B =  1 << 6;
57     public static final int SECURITY_WAPI_PSK =     1 << 7;
58     public static final int SECURITY_WAPI_CERT =    1 << 8;
59     public static final int SECURITY_WPA3_ENTERPRISE = 1 << 9;
60 
61     /**
62      * These values are used to describe ip configuration parameters for a network.
63      */
64     public static final int STATIC_IP_ASSIGNMENT = 0;
65     public static final int DHCP_IP_ASSIGNMENT = 1;
66     public static final int STATIC_PROXY_SETTING = 0;
67     public static final int PAC_PROXY_SETTING = 1;
68     public static final int NONE_PROXY_SETTING = 2;
69 
70     /**
71      * These are constants used to generate predefined WifiConfiguration objects.
72      */
73     public static final int TEST_NETWORK_ID = -1;
74     public static final int TEST_UID = 5;
75     public static final String TEST_SSID = "WifiConfigurationTestSSID";
76     public static final String TEST_PSK = "\"WifiConfigurationTestUtilPsk\"";
77     public static final String[] TEST_WEP_KEYS =
78             {"\"WifiTestWep12\"", "\"WifiTestWep34\"",
79                     "45342312ab", "45342312ab45342312ab34ac12"};
80     public static final String[] TEST_WEP_KEYS_WITH_NULL = {
81         "\"WifiTestWep12\"", "\"WifiTestWep34\"", "45342312ab45342312ab34ac12", null
82     };
83     public static final String TEST_EAP_PASSWORD = "WifiConfigurationTestUtilEapPassword";
84     public static final int TEST_WEP_TX_KEY_INDEX = 1;
85     public static final String TEST_FQDN = "WifiConfigurationTestUtilFQDN";
86     public static final String TEST_PROVIDER_FRIENDLY_NAME =
87             "WifiConfigurationTestUtilFriendlyName";
88     public static final String TEST_STATIC_IP_LINK_ADDRESS = "192.168.48.2";
89     public static final int TEST_STATIC_IP_LINK_PREFIX_LENGTH = 8;
90     public static final String TEST_STATIC_IP_GATEWAY_ADDRESS = "192.168.48.1";
91     public static final String[] TEST_STATIC_IP_DNS_SERVER_ADDRESSES =
92             new String[]{"192.168.48.1", "192.168.48.10"};
93     public static final String TEST_STATIC_PROXY_HOST = "192.168.48.1";
94     public static final int TEST_STATIC_PROXY_PORT = 8000;
95     public static final String TEST_STATIC_PROXY_EXCLUSION_LIST = "";
96     public static final String TEST_PAC_PROXY_LOCATION = "http://";
97     public static final String TEST_CA_CERT_ALIAS = "WifiConfigurationTestUtilCaCertAlias";
98     public static final String TEST_CA_CERT_SUITE_B_ALIAS = "SuiteBCaCertAlias";
99     public static final String TEST_CA_CERT_PATH = "caPath";
100     public static final String TEST_DOM_SUBJECT_MATCH = "domSubjectMatch";
101     public static final String TEST_IDENTITY = "user@example.com";
102 
103     private static final int MAX_SSID_LENGTH = 32;
104     /**
105      * Index used to assign unique SSIDs for the generation of predefined WifiConfiguration objects.
106      */
107     private static int sNetworkIndex = 0;
108 
109     /**
110      * Construct a {@link android.net.wifi.WifiConfiguration}.
111      * @param networkId the configuration's networkId
112      * @param uid the configuration's creator uid
113      * @param ssid the configuration's ssid
114      * @param shared whether the configuration is shared with other users on the device
115      * @param enabled whether the configuration is enabled
116      * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
117      * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
118      * @return the constructed {@link android.net.wifi.WifiConfiguration}
119      */
generateWifiConfig(int networkId, int uid, String ssid, boolean shared, boolean enabled, String fqdn, String providerFriendlyName)120     private static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
121             boolean shared, boolean enabled, String fqdn, String providerFriendlyName) {
122         final WifiConfiguration config = new WifiConfiguration();
123         config.SSID = ssid;
124         config.networkId = networkId;
125         config.creatorUid = uid;
126         config.shared = shared;
127         config.status = enabled ? WifiConfiguration.Status.ENABLED
128                 : WifiConfiguration.Status.DISABLED;
129         config.FQDN = fqdn;
130         config.providerFriendlyName = providerFriendlyName;
131         if (config.FQDN != null) {
132             int uniqueId = config.FQDN.hashCode() + config.networkId + config.creatorUid;
133             // Generate a Passpoint unique id locally for the test
134             config.setPasspointUniqueId(config.FQDN + "_" + uniqueId);
135         }
136         return config;
137     }
138 
139     /**
140      * Construct a {@link android.net.wifi.WifiConfiguration}.
141      * @param networkId the configuration's networkId
142      * @param uid the configuration's creator uid
143      * @param ssid the configuration's ssid
144      * @param shared whether the configuration is shared with other users on the device
145      * @param enabled whether the configuration is enabled
146      * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
147      * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
148      * @param security the configuration's security type
149      * @return the constructed {@link android.net.wifi.WifiConfiguration}
150      */
generateWifiConfig(int networkId, int uid, String ssid, boolean shared, boolean enabled, String fqdn, String providerFriendlyName, int security)151     public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
152             boolean shared, boolean enabled, String fqdn, String providerFriendlyName,
153             int security) {
154         WifiConfiguration config = generateWifiConfig(networkId, uid, ssid, shared, enabled, fqdn,
155                 providerFriendlyName);
156 
157         if ((security & SECURITY_NONE) != 0) {
158             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
159         }
160         if ((security & SECURITY_WEP) != 0) {
161             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
162         }
163         if ((security & SECURITY_PSK) != 0) {
164             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
165         }
166 
167         if ((security & SECURITY_SAE) != 0) {
168             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
169         }
170 
171         if ((security & SECURITY_OWE) != 0) {
172             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_OWE);
173         }
174 
175         if ((security & SECURITY_EAP) != 0) {
176             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP);
177             config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS);
178             config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);
179             config.enterpriseConfig.setIdentity(TEST_IDENTITY);
180             config.enterpriseConfig.setPassword(TEST_EAP_PASSWORD);
181             config.enterpriseConfig.setCaPath(TEST_CA_CERT_PATH);
182             config.enterpriseConfig.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
183         }
184 
185         if ((security & SECURITY_WPA3_ENTERPRISE) != 0) {
186             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE);
187             config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
188             config.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);
189             config.enterpriseConfig.setIdentity(TEST_IDENTITY);
190             config.enterpriseConfig.setPassword(TEST_EAP_PASSWORD);
191             config.enterpriseConfig.setCaPath(TEST_CA_CERT_PATH);
192             config.enterpriseConfig.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
193         }
194 
195         if ((security & SECURITY_EAP_SUITE_B) != 0) {
196             config.addSecurityParams(
197                     WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT);
198             config.enterpriseConfig.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
199         }
200 
201         if ((security & SECURITY_WAPI_PSK) != 0) {
202             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_WAPI_PSK);
203         }
204 
205         if ((security & SECURITY_WAPI_CERT) != 0) {
206             config.addSecurityParams(WifiConfiguration.SECURITY_TYPE_WAPI_CERT);
207             config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.WAPI_CERT);
208         }
209         return config;
210     }
211 
212     /**
213      * Construct a {@link android.net.IpConfiguration }.
214      * @param ipAssignmentType One of {@link #STATIC_IP_ASSIGNMENT} or {@link #DHCP_IP_ASSIGNMENT}.
215      * @param proxySettingType One of {@link #STATIC_PROXY_SETTING} or {@link #PAC_PROXY_SETTING} or
216      *                        {@link #NONE_PROXY_SETTING}.
217      * @param linkAddress static ip address string.
218      * @param linkPrefixLength static ip address prefix length.
219      * @param gatewayAddress static gateway address.
220      * @param dnsServerAddresses list of dns servers for static ip configuration.
221      * @param proxyHost Static proxy server address.
222      * @param proxyPort Static proxy server port.
223      * @param proxyExclusionList Static proxy exclusion list.
224      * @param pacProxyPath Pac proxy server path.
225      * @return the constructed {@link android.net.IpConfiguration}
226      */
generateIpConfig( int ipAssignmentType, int proxySettingType, String linkAddress, int linkPrefixLength, String gatewayAddress, String[] dnsServerAddresses, String proxyHost, int proxyPort, String proxyExclusionList, String pacProxyPath)227     public static IpConfiguration generateIpConfig(
228             int ipAssignmentType, int proxySettingType, String linkAddress, int linkPrefixLength,
229             String gatewayAddress, String[] dnsServerAddresses, String proxyHost,
230             int proxyPort, String proxyExclusionList, String pacProxyPath) {
231         StaticIpConfiguration staticIpConfiguration = null;
232         ProxyInfo proxyInfo = null;
233         IpConfiguration.IpAssignment ipAssignment = IpConfiguration.IpAssignment.UNASSIGNED;
234         IpConfiguration.ProxySettings proxySettings = IpConfiguration.ProxySettings.UNASSIGNED;
235 
236         if (ipAssignmentType == STATIC_IP_ASSIGNMENT) {
237             staticIpConfiguration = new StaticIpConfiguration();
238             if (!TextUtils.isEmpty(linkAddress)) {
239                 LinkAddress linkAddr =
240                         new LinkAddress(
241                                 InetAddresses.parseNumericAddress(linkAddress), linkPrefixLength);
242                 staticIpConfiguration.ipAddress = linkAddr;
243             }
244 
245             if (!TextUtils.isEmpty(gatewayAddress)) {
246                 InetAddress gatewayAddr =
247                         InetAddresses.parseNumericAddress(gatewayAddress);
248                 staticIpConfiguration.gateway = gatewayAddr;
249             }
250             if (dnsServerAddresses != null) {
251                 for (String dnsServerAddress : dnsServerAddresses) {
252                     if (!TextUtils.isEmpty(dnsServerAddress)) {
253                         staticIpConfiguration.dnsServers.add(
254                                 InetAddresses.parseNumericAddress(dnsServerAddress));
255                     }
256 
257                 }
258             }
259             ipAssignment = IpConfiguration.IpAssignment.STATIC;
260         } else if (ipAssignmentType == DHCP_IP_ASSIGNMENT) {
261             ipAssignment = IpConfiguration.IpAssignment.DHCP;
262         }
263 
264         if (proxySettingType == STATIC_PROXY_SETTING) {
265             proxyInfo = new ProxyInfo(proxyHost, proxyPort, proxyExclusionList);
266             proxySettings = IpConfiguration.ProxySettings.STATIC;
267         } else if (proxySettingType == PAC_PROXY_SETTING) {
268             proxyInfo = ProxyInfo.buildPacProxy(Uri.parse(pacProxyPath));
269             proxySettings = IpConfiguration.ProxySettings.PAC;
270         } else if (proxySettingType == NONE_PROXY_SETTING) {
271             proxySettings = IpConfiguration.ProxySettings.NONE;
272         }
273         return new IpConfiguration(ipAssignment, proxySettings, staticIpConfiguration, proxyInfo);
274     }
275 
276     /**
277      * Create a new SSID for the the network being created.
278      */
createNewSSID()279     private static String createNewSSID() {
280         String ssid = TEST_SSID + sNetworkIndex++;
281         assertTrue(ssid.length() <= MAX_SSID_LENGTH);
282         return "\"" + ssid + "\"";
283     }
284 
285     /**
286      * Helper methods to generate predefined WifiConfiguration objects of the required type. These
287      * use a static index to avoid duplicate configurations.
288      */
createOweNetwork()289     public static WifiConfiguration createOweNetwork() {
290         return createOweNetwork(createNewSSID());
291     }
292 
createOweNetwork(String ssid)293     public static WifiConfiguration createOweNetwork(String ssid) {
294         return generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
295                 null, SECURITY_OWE);
296     }
297 
createOpenNetwork()298     public static WifiConfiguration createOpenNetwork() {
299         return createOpenNetwork(createNewSSID());
300     }
301 
createOpenNetwork(String ssid)302     public static WifiConfiguration createOpenNetwork(String ssid) {
303         WifiConfiguration config = generateWifiConfig(
304                 TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
305                 null, SECURITY_NONE);
306         return config;
307     }
308 
createOpenOweNetwork()309     public static WifiConfiguration createOpenOweNetwork() {
310         return createOpenOweNetwork(createNewSSID());
311     }
312 
createOpenOweNetwork(String ssid)313     public static WifiConfiguration createOpenOweNetwork(String ssid) {
314         WifiConfiguration config = generateWifiConfig(
315                 TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
316                 null, SECURITY_NONE);
317         // the upgradable type is always added.
318         SecurityParams params = SecurityParams
319                 .createSecurityParamsBySecurityType(WifiConfiguration.SECURITY_TYPE_OWE);
320         params.setIsAddedByAutoUpgrade(true);
321         config.addSecurityParams(params);
322         return config;
323     }
324 
createEphemeralNetwork()325     public static WifiConfiguration createEphemeralNetwork() {
326         WifiConfiguration configuration = createOpenNetwork();
327         configuration.ephemeral = true;
328         return configuration;
329     }
330 
createOpenHiddenNetwork()331     public static WifiConfiguration createOpenHiddenNetwork() {
332         WifiConfiguration configuration = createOpenNetwork();
333         configuration.hiddenSSID = true;
334         return configuration;
335     }
336 
createSaeNetwork()337     public static WifiConfiguration createSaeNetwork() {
338         return createSaeNetwork(createNewSSID());
339     }
340 
createPskNetwork()341     public static WifiConfiguration createPskNetwork() {
342         return createPskNetwork(createNewSSID());
343     }
344 
createPskNetwork(String ssid)345     public static WifiConfiguration createPskNetwork(String ssid) {
346         WifiConfiguration configuration =
347                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
348                         null, SECURITY_PSK);
349         configuration.preSharedKey = TEST_PSK;
350         return configuration;
351     }
352 
createPskSaeNetwork()353     public static WifiConfiguration createPskSaeNetwork() {
354         return createPskSaeNetwork(createNewSSID());
355     }
356 
createPskSaeNetwork(String ssid)357     public static WifiConfiguration createPskSaeNetwork(String ssid) {
358         WifiConfiguration configuration =
359                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
360                         null, SECURITY_PSK);
361         configuration.preSharedKey = TEST_PSK;
362         // the upgradable type is always added.
363         SecurityParams params = SecurityParams
364                 .createSecurityParamsBySecurityType(WifiConfiguration.SECURITY_TYPE_SAE);
365         params.setIsAddedByAutoUpgrade(true);
366         configuration.addSecurityParams(params);
367         return configuration;
368     }
369 
createSaeNetwork(String ssid)370     public static WifiConfiguration createSaeNetwork(String ssid) {
371         WifiConfiguration configuration =
372                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
373                         null, SECURITY_SAE);
374 
375         // SAE password uses the same member.
376         configuration.preSharedKey = TEST_PSK;
377         configuration.requirePmf = true;
378         return configuration;
379     }
380 
createPskHiddenNetwork()381     public static WifiConfiguration createPskHiddenNetwork() {
382         WifiConfiguration configuration = createPskNetwork();
383         configuration.hiddenSSID = true;
384         return configuration;
385     }
386 
createCaptivePortalNetwork()387     public static WifiConfiguration createCaptivePortalNetwork() {
388         WifiConfiguration configuration = createPskNetwork();
389         NetworkSelectionStatus.Builder builder = new NetworkSelectionStatus.Builder();
390         NetworkSelectionStatus networkSelectionStatus = builder.build();
391         networkSelectionStatus.setHasNeverDetectedCaptivePortal(false);
392         configuration.setNetworkSelectionStatus(networkSelectionStatus);
393         return configuration;
394     }
395 
createWepNetwork()396     public static WifiConfiguration createWepNetwork() {
397         WifiConfiguration configuration =
398                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
399                         null, SECURITY_WEP);
400         configuration.wepKeys = TEST_WEP_KEYS;
401         configuration.wepTxKeyIndex = TEST_WEP_TX_KEY_INDEX;
402         return configuration;
403     }
404 
createWepHiddenNetwork()405     public static WifiConfiguration createWepHiddenNetwork() {
406         WifiConfiguration configuration = createWepNetwork();
407         configuration.hiddenSSID = true;
408         return configuration;
409     }
410 
createWepNetworkWithSingleKey()411     public static WifiConfiguration createWepNetworkWithSingleKey() {
412         WifiConfiguration configuration =
413                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
414                         null, SECURITY_WEP);
415         configuration.wepKeys[0] = TEST_WEP_KEYS[0];
416         configuration.wepTxKeyIndex = 0;
417         return configuration;
418     }
419 
420 
createEapNetwork()421     public static WifiConfiguration createEapNetwork() {
422         return createEapNetwork(createNewSSID());
423     }
424 
createEapNetwork(String ssid)425     public static WifiConfiguration createEapNetwork(String ssid) {
426         return createEapNetwork(ssid,
427                 WifiEnterpriseConfig.Eap.NONE,
428                 WifiEnterpriseConfig.Phase2.NONE);
429     }
430 
431 
createEapNetwork(int eapMethod, int phase2Method)432     public static WifiConfiguration createEapNetwork(int eapMethod, int phase2Method) {
433         return createEapNetwork(createNewSSID(), eapMethod, phase2Method);
434     }
435 
createEapNetwork(String ssid, int eapMethod, int phase2Method)436     public static WifiConfiguration createEapNetwork(String ssid, int eapMethod, int phase2Method) {
437         WifiConfiguration configuration =
438                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true,
439                         null, null, SECURITY_EAP);
440         if (eapMethod != WifiEnterpriseConfig.Eap.NONE) {
441             configuration.enterpriseConfig.setEapMethod(eapMethod);
442             configuration.enterpriseConfig.setPhase2Method(phase2Method);
443         }
444         return configuration;
445     }
446 
createWpa2Wpa3EnterpriseNetwork()447     public static WifiConfiguration createWpa2Wpa3EnterpriseNetwork() {
448         return createWpa2Wpa3EnterpriseNetwork(createNewSSID());
449     }
450 
createWpa2Wpa3EnterpriseNetwork( int eapMethod, int phase2Method)451     public static WifiConfiguration createWpa2Wpa3EnterpriseNetwork(
452             int eapMethod, int phase2Method) {
453         return createWpa2Wpa3EnterpriseNetwork(createNewSSID(), eapMethod, phase2Method);
454     }
455 
createWpa2Wpa3EnterpriseNetwork(String ssid)456     public static WifiConfiguration createWpa2Wpa3EnterpriseNetwork(String ssid) {
457         return createWpa2Wpa3EnterpriseNetwork(
458                 ssid,
459                 WifiEnterpriseConfig.Eap.NONE,
460                 WifiEnterpriseConfig.Phase2.NONE);
461     }
462 
createWpa2Wpa3EnterpriseNetwork( String ssid, int eapMethod, int phase2Method)463     public static WifiConfiguration createWpa2Wpa3EnterpriseNetwork(
464             String ssid, int eapMethod, int phase2Method) {
465         WifiConfiguration configuration =
466                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true,
467                         null, null, SECURITY_EAP);
468         if (eapMethod != WifiEnterpriseConfig.Eap.NONE) {
469             configuration.enterpriseConfig.setEapMethod(eapMethod);
470             configuration.enterpriseConfig.setPhase2Method(phase2Method);
471         }
472         // the upgradable type is always added.
473         SecurityParams params = SecurityParams
474                 .createSecurityParamsBySecurityType(
475                         WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE);
476         params.setIsAddedByAutoUpgrade(true);
477         configuration.addSecurityParams(params);
478         return configuration;
479     }
480 
createWpa3EnterpriseNetwork(String ssid)481     public static WifiConfiguration createWpa3EnterpriseNetwork(String ssid) {
482         WifiConfiguration configuration =
483                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true,
484                         null, null, SECURITY_WPA3_ENTERPRISE);
485         return configuration;
486     }
487 
createEapSuiteBNetwork()488     public static WifiConfiguration createEapSuiteBNetwork() {
489         return createEapSuiteBNetwork(WifiConfiguration.SuiteBCipher.ECDHE_RSA);
490     }
491 
createEapSuiteBNetwork(int signatureType)492     public static WifiConfiguration createEapSuiteBNetwork(int signatureType) {
493         WifiConfiguration configuration =
494                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
495                         null, null, SECURITY_EAP_SUITE_B);
496 
497         configuration.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
498         configuration.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
499 
500         if (signatureType == WifiConfiguration.SuiteBCipher.ECDHE_ECDSA) {
501             configuration.enterpriseConfig.setCaCertificate(FakeKeys.CA_SUITE_B_ECDSA_CERT);
502             configuration.enterpriseConfig.setClientKeyEntryWithCertificateChain(
503                     FakeKeys.CLIENT_SUITE_B_ECC_KEY,
504                     new X509Certificate[] {FakeKeys.CLIENT_SUITE_B_ECDSA_CERT});
505         } else {
506             configuration.enterpriseConfig.setCaCertificate(FakeKeys.CA_SUITE_B_RSA3072_CERT);
507             configuration.enterpriseConfig.setClientKeyEntryWithCertificateChain(
508                     FakeKeys.CLIENT_SUITE_B_RSA3072_KEY,
509                     new X509Certificate[] {FakeKeys.CLIENT_SUITE_B_RSA3072_CERT});
510         }
511         return configuration;
512     }
513 
createPasspointNetwork()514     public static WifiConfiguration createPasspointNetwork() {
515         WifiConfiguration configuration =
516                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
517                         TEST_FQDN, TEST_PROVIDER_FRIENDLY_NAME, SECURITY_EAP);
518         return configuration;
519     }
520 
createWapiPskNetwork()521     public static WifiConfiguration createWapiPskNetwork() {
522         WifiConfiguration configuration =
523                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
524                         null, SECURITY_WAPI_PSK);
525         configuration.preSharedKey = TEST_PSK;
526         return configuration;
527     }
528 
createWapiCertNetwork()529     public static WifiConfiguration createWapiCertNetwork() {
530         WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
531         enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.WAPI_CERT);
532         enterpriseConfig.setWapiCertSuite("wapiCertSuite");
533         WifiConfiguration configuration =
534                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
535                         null, SECURITY_WAPI_CERT);
536         configuration.enterpriseConfig = enterpriseConfig;
537         return configuration;
538     }
539 
createStaticIpConfigurationWithPacProxy()540     public static IpConfiguration createStaticIpConfigurationWithPacProxy() {
541         return generateIpConfig(
542                 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
543                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
544                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
545                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
546                 TEST_PAC_PROXY_LOCATION);
547     }
548 
createStaticIpConfigurationWithStaticProxy()549     public static IpConfiguration createStaticIpConfigurationWithStaticProxy() {
550         return generateIpConfig(
551                 STATIC_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
552                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
553                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
554                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
555                 TEST_PAC_PROXY_LOCATION);
556     }
557 
createPartialStaticIpConfigurationWithPacProxy()558     public static IpConfiguration createPartialStaticIpConfigurationWithPacProxy() {
559         return generateIpConfig(
560                 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
561                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
562                 null, null,
563                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
564                 TEST_PAC_PROXY_LOCATION);
565     }
566 
createDHCPIpConfigurationWithPacProxy()567     public static IpConfiguration createDHCPIpConfigurationWithPacProxy() {
568         return generateIpConfig(
569                 DHCP_IP_ASSIGNMENT, PAC_PROXY_SETTING,
570                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
571                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
572                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
573                 TEST_PAC_PROXY_LOCATION);
574     }
575 
createDHCPIpConfigurationWithStaticProxy()576     public static IpConfiguration createDHCPIpConfigurationWithStaticProxy() {
577         return generateIpConfig(
578                 DHCP_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
579                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
580                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
581                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
582                 TEST_PAC_PROXY_LOCATION);
583     }
584 
createDHCPIpConfigurationWithNoProxy()585     public static IpConfiguration createDHCPIpConfigurationWithNoProxy() {
586         return generateIpConfig(
587                 DHCP_IP_ASSIGNMENT, NONE_PROXY_SETTING,
588                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
589                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
590                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
591                 TEST_PAC_PROXY_LOCATION);
592     }
593 
594     /**
595      * Creates an IP configuration with specific parameters.
596      * @param proxySetting Must be one of {@link WifiConfigurationTestUtil#STATIC_PROXY_SETTING},
597      * {@link WifiConfigurationTestUtil#PAC_PROXY_SETTING},
598      * {@link WifiConfigurationTestUtil#NONE_PROXY_SETTING}
599      */
createDHCPIpConfigurationWithSpecificProxy( int proxySetting, String staticProxyHost, int staticProxyPort, String staticProxyExclusionList, String pacProxyLocation)600     public static IpConfiguration createDHCPIpConfigurationWithSpecificProxy(
601             int proxySetting,
602             String staticProxyHost,
603             int staticProxyPort,
604             String staticProxyExclusionList,
605             String pacProxyLocation) {
606         return generateIpConfig(
607                 DHCP_IP_ASSIGNMENT, proxySetting,
608                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
609                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
610                 staticProxyHost, staticProxyPort, staticProxyExclusionList,
611                 pacProxyLocation);
612     }
613 
614     // TODO: These enterprise configurations may need more parameters set.
createPEAPWifiEnterpriseConfigWithGTCPhase2()615     public static WifiEnterpriseConfig createPEAPWifiEnterpriseConfigWithGTCPhase2() {
616         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
617         config.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
618         config.setPhase2Method(WifiEnterpriseConfig.Phase2.GTC);
619         config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "PEAP"});
620         config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
621         config.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
622         config.setIdentity(TEST_IDENTITY);
623         config.setPassword(TEST_EAP_PASSWORD);
624         return config;
625     }
626 
createTLSWifiEnterpriseConfigWithNonePhase2()627     public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithNonePhase2() {
628         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
629         config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
630         config.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
631         config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "TLS"});
632         config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
633         config.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
634         return config;
635     }
636 
createTLSWifiEnterpriseConfigWithAkaPhase2()637     public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithAkaPhase2() {
638         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
639         config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
640         config.setPhase2Method(WifiEnterpriseConfig.Phase2.AKA);
641         config.setDomainSuffixMatch(TEST_DOM_SUBJECT_MATCH);
642         return config;
643     }
644 
645     /**
646      * Gets scan result capabilities for a particular network configuration.
647      */
getScanResultCapsForNetwork(WifiConfiguration configuration)648     public static String getScanResultCapsForNetwork(WifiConfiguration configuration) {
649         String caps = "";
650         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_PSK)) {
651             caps += "[RSN-PSK-CCMP]";
652         }
653         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE)) {
654             caps += "[RSN-EAP/SHA256-CCMP][MFPC][MFPR]";
655         }
656         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_EAP)) {
657             caps += "[RSN-EAP/SHA1-CCMP]";
658         }
659         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_WEP)
660                 && WifiConfigurationUtil.hasAnyValidWepKey(configuration.wepKeys)) {
661             caps += "[WEP]";
662         }
663         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_SAE)) {
664             caps += "[RSN-SAE-CCMP]";
665         }
666         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_OWE)) {
667             caps += "[RSN-OWE-CCMP]";
668         }
669         if (configuration.isSecurityType(
670                 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT)) {
671             caps += "[RSN-SUITE_B_192-CCMP][MFPR]";
672         }
673         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_WAPI_PSK)) {
674             caps += "[WAPI-WAPI-PSK-SMS4]";
675         }
676         if (configuration.isSecurityType(WifiConfiguration.SECURITY_TYPE_WAPI_CERT)) {
677             caps += "[WAPI-WAPI-CERT-SMS4]";
678         }
679         return caps;
680     }
681 
682     /**
683      * Gets scan result capabilities for a WPA2/WPA3-Transition mode network configuration
684      */
getScanResultCapsForWpa2Wpa3TransitionNetwork()685     public static String getScanResultCapsForWpa2Wpa3TransitionNetwork() {
686         String caps = "[RSN-PSK+SAE-CCMP]";
687         return caps;
688     }
689 
690     /**
691      * Creates a scan detail corresponding to the provided network and given BSSID, etc.
692      */
createScanDetailForNetwork( WifiConfiguration configuration, String bssid, int level, int frequency, long tsf, long seen)693     public static ScanDetail createScanDetailForNetwork(
694             WifiConfiguration configuration, String bssid, int level, int frequency,
695             long tsf, long seen) {
696         String caps = getScanResultCapsForNetwork(configuration);
697         WifiSsid ssid = WifiSsid.fromString(configuration.SSID);
698         return new ScanDetail(ssid, bssid, caps, level, frequency, tsf, seen);
699     }
700 
701     /**
702      * Creates a scan detail corresponding to the provided network and given BSSID, but sets
703      * the capabilities to WPA2/WPA3-Transition mode network.
704      */
createScanDetailForWpa2Wpa3TransitionModeNetwork( WifiConfiguration configuration, String bssid, int level, int frequency, long tsf, long seen)705     public static ScanDetail createScanDetailForWpa2Wpa3TransitionModeNetwork(
706             WifiConfiguration configuration, String bssid, int level, int frequency,
707             long tsf, long seen) {
708         String caps = getScanResultCapsForWpa2Wpa3TransitionNetwork();
709         WifiSsid ssid = WifiSsid.fromUtf8Text(configuration.getPrintableSsid());
710         return new ScanDetail(ssid, bssid, caps, level, frequency, tsf, seen);
711     }
712 
713     /**
714      * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore
715      * and config store.
716      */
assertCommonConfigurationElementsEqual( WifiConfiguration expected, WifiConfiguration actual, boolean isSupplicantBackup)717     private static void assertCommonConfigurationElementsEqual(
718             WifiConfiguration expected, WifiConfiguration actual, boolean isSupplicantBackup) {
719         assertNotNull(expected);
720         assertNotNull(actual);
721         assertEquals(WifiSsid.fromString(expected.SSID), WifiSsid.fromString(actual.SSID));
722         assertEquals(expected.BSSID, actual.BSSID);
723         assertEquals(expected.preSharedKey, actual.preSharedKey);
724         assertEquals(expected.wepKeys, actual.wepKeys);
725         assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
726         assertEquals(expected.hiddenSSID, actual.hiddenSSID);
727         assertEquals(expected.requirePmf, actual.requirePmf);
728         assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
729         assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
730         assertEquals(expected.priority, actual.priority);
731         // Supplicant backup does not include the following fields.
732         if (!isSupplicantBackup) {
733             assertEquals(expected.allowedProtocols, actual.allowedProtocols);
734             assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
735             assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
736             assertEquals(expected.shared, actual.shared);
737         }
738         assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration());
739     }
740 
741 
742    /**
743      * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
744      * for softAp used.
745      */
assertConfigurationEqualForSoftAp( WifiConfiguration expected, WifiConfiguration actual)746     public static void assertConfigurationEqualForSoftAp(
747             WifiConfiguration expected, WifiConfiguration actual) {
748         assertEquals(expected.SSID, actual.SSID);
749         assertEquals(expected.BSSID, actual.BSSID);
750         assertEquals(expected.apBand, actual.apBand);
751         assertEquals(expected.apChannel, actual.apChannel);
752         assertEquals(expected.preSharedKey, actual.preSharedKey);
753         assertEquals(expected.getAuthType(), actual.getAuthType());
754         assertEquals(expected.hiddenSSID, actual.hiddenSSID);
755     }
756 
757     /**
758      * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
759      * for backup/restore.
760      */
assertConfigurationEqualForBackup( WifiConfiguration expected, WifiConfiguration actual)761     public static void assertConfigurationEqualForBackup(
762             WifiConfiguration expected, WifiConfiguration actual) {
763         assertCommonConfigurationElementsEqual(expected, actual, true);
764         assertEquals(expected.meteredOverride, actual.meteredOverride);
765     }
766 
767     /**
768      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for
769      * config store.
770      */
assertConfigurationEqualForConfigStore( WifiConfiguration expected, WifiConfiguration actual)771     public static void assertConfigurationEqualForConfigStore(
772             WifiConfiguration expected, WifiConfiguration actual) {
773         assertCommonConfigurationElementsEqual(expected, actual, false);
774         assertEquals(expected.status, actual.status);
775         assertEquals(expected.FQDN, actual.FQDN);
776         assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
777         assertTrue(Arrays.equals(expected.roamingConsortiumIds, actual.roamingConsortiumIds));
778         assertEquals(expected.linkedConfigurations, actual.linkedConfigurations);
779         assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress);
780         assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess);
781         assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
782         assertEquals(expected.meteredHint, actual.meteredHint);
783         assertEquals(expected.meteredOverride, actual.meteredOverride);
784         assertEquals(expected.useExternalScores, actual.useExternalScores);
785         assertEquals(expected.trusted, actual.trusted);
786         assertEquals(expected.oemPaid, actual.oemPaid);
787         assertEquals(expected.oemPrivate, actual.oemPrivate);
788         assertEquals(expected.carrierMerged, actual.carrierMerged);
789         assertEquals(expected.restricted, actual.restricted);
790         assertEquals(0, actual.numAssociation);
791         assertEquals(expected.creatorUid, actual.creatorUid);
792         assertEquals(expected.creatorName, actual.creatorName);
793         assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
794         assertEquals(expected.lastUpdateName, actual.lastUpdateName);
795         assertEquals(expected.lastConnectUid, actual.lastConnectUid);
796         assertEquals(expected.isLegacyPasspointConfig, actual.isLegacyPasspointConfig);
797         assertEquals(expected.getRandomizedMacAddress(), actual.getRandomizedMacAddress());
798         assertEquals(expected.macRandomizationSetting, actual.macRandomizationSetting);
799         assertNetworkSelectionStatusEqualForConfigStore(
800                 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
801         assertWifiEnterpriseConfigEqualForConfigStore(
802                 expected.enterpriseConfig, actual.enterpriseConfig);
803         if (SdkLevel.isAtLeastV()) {
804             assertEquals(expected.getVendorData(), actual.getVendorData());
805         }
806     }
807 
808     /**
809      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
810      * saved into internal database by WifiConfigurationManager for network additions/updates.
811      */
assertConfigurationEqualForConfigManagerAddOrUpdate( WifiConfiguration expected, WifiConfiguration actual)812     public static void assertConfigurationEqualForConfigManagerAddOrUpdate(
813             WifiConfiguration expected, WifiConfiguration actual) {
814         assertCommonConfigurationElementsEqual(expected, actual, false);
815         assertEquals(expected.FQDN, actual.FQDN);
816         assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
817         assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
818         assertEquals(expected.meteredHint, actual.meteredHint);
819         assertEquals(expected.meteredOverride, actual.meteredOverride);
820         assertEquals(expected.useExternalScores, actual.useExternalScores);
821         assertEquals(expected.ephemeral, actual.ephemeral);
822         assertEquals(expected.osu, actual.osu);
823         assertEquals(expected.trusted, actual.trusted);
824         assertEquals(expected.oemPaid, actual.oemPaid);
825         assertEquals(expected.fromWifiNetworkSuggestion, actual.fromWifiNetworkSuggestion);
826         assertEquals(expected.fromWifiNetworkSpecifier, actual.fromWifiNetworkSpecifier);
827         assertEquals(expected.creatorUid, actual.creatorUid);
828         assertEquals(expected.creatorName, actual.creatorName);
829         assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
830         assertEquals(expected.lastUpdateName, actual.lastUpdateName);
831         assertNetworkSelectionStatusEqualForConfigStore(
832                 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
833         assertWifiEnterpriseConfigEqualForConfigStore(
834                 expected.enterpriseConfig, actual.enterpriseConfig);
835     }
836 
837     /**
838      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
839      * saved into wpa_supplicant by SupplicantStaNetwork.
840      */
assertConfigurationEqualForSupplicant( WifiConfiguration expected, WifiConfiguration actual)841     public static void assertConfigurationEqualForSupplicant(
842             WifiConfiguration expected, WifiConfiguration actual) {
843         assertNotNull(expected);
844         assertNotNull(actual);
845         assertEquals(expected.SSID, actual.SSID);
846         assertArrayEquals(NativeUtil.macAddressToByteArray(expected.getNetworkSelectionStatus()
847                         .getNetworkSelectionBSSID()),
848                 NativeUtil.macAddressToByteArray(actual.getNetworkSelectionStatus()
849                         .getNetworkSelectionBSSID()));
850         assertEquals(expected.preSharedKey, actual.preSharedKey);
851         assertEquals(expected.wepKeys, actual.wepKeys);
852         assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
853         assertEquals(expected.hiddenSSID, actual.hiddenSSID);
854         assertEquals(expected.requirePmf, actual.requirePmf);
855         assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
856         /* security params are static to the security type, no need to check them anymore */
857         assertWifiEnterpriseConfigEqualForConfigStore(
858                 expected.enterpriseConfig, actual.enterpriseConfig);
859     }
860 
861     /**
862      * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator
863      * which is used in QNS tests for comparing the network selections.
864      * This importantly checks that the networkId's of the 2 configs are equal.
865      */
assertConfigurationEqual( WifiConfiguration expected, WifiConfiguration actual)866     public static void assertConfigurationEqual(
867             WifiConfiguration expected, WifiConfiguration actual) {
868         assertCommonConfigurationElementsEqual(expected, actual, false);
869         assertEquals(expected.networkId, actual.networkId);
870         assertEquals(expected.ephemeral, actual.ephemeral);
871         assertEquals(expected.fromWifiNetworkSuggestion, actual.fromWifiNetworkSuggestion);
872         assertEquals(expected.fromWifiNetworkSpecifier, actual.fromWifiNetworkSpecifier);
873         assertEquals(expected.trusted, actual.trusted);
874     }
875 
876     /**
877      * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved
878      * for config store.
879      */
assertNetworkSelectionStatusEqualForConfigStore( NetworkSelectionStatus expected, NetworkSelectionStatus actual)880     public static void assertNetworkSelectionStatusEqualForConfigStore(
881             NetworkSelectionStatus expected, NetworkSelectionStatus actual) {
882         if (expected.isNetworkTemporaryDisabled()) {
883             // Temporarily disabled networks are enabled when persisted.
884             assertEquals(
885                     NetworkSelectionStatus.NETWORK_SELECTION_ENABLED,
886                     actual.getNetworkSelectionStatus());
887             assertEquals(
888                     NetworkSelectionStatus.DISABLED_NONE,
889                     actual.getNetworkSelectionDisableReason());
890         } else {
891             assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
892             assertEquals(
893                     expected.getNetworkSelectionDisableReason(),
894                     actual.getNetworkSelectionDisableReason());
895         }
896         assertEquals(expected.getConnectChoice(), actual.getConnectChoice());
897         assertEquals(expected.hasEverConnected(), actual.hasEverConnected());
898     }
899 
900     /**
901      * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved
902      * for config store.
903      */
assertWifiEnterpriseConfigEqualForConfigStore( WifiEnterpriseConfig expected, WifiEnterpriseConfig actual)904     public static void assertWifiEnterpriseConfigEqualForConfigStore(
905             WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) {
906         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY),
907                 actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
908         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY),
909                 actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
910         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY),
911                 actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
912         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY),
913                 actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
914         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY),
915                 actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
916         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY),
917                 actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
918         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY),
919                 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
920         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY),
921                 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
922         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY),
923                 actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
924         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY),
925                 actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
926         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY),
927                 actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
928         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY),
929                 actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
930         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.REALM_KEY),
931                 actual.getFieldValue(WifiEnterpriseConfig.REALM_KEY));
932         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PLMN_KEY),
933                 actual.getFieldValue(WifiEnterpriseConfig.PLMN_KEY));
934         assertEquals(expected.getEapMethod(), actual.getEapMethod());
935         assertEquals(expected.getPhase2Method(), actual.getPhase2Method());
936         if (SdkLevel.isAtLeastS()) {
937             assertEquals(expected.getClientKeyPairAlias(), actual.getClientKeyPairAlias());
938         }
939     }
940 
941     /**
942      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
943      * saved for backup/restore.
944      */
assertConfigurationsEqualForBackup( List<WifiConfiguration> expected, List<WifiConfiguration> actual)945     public static void assertConfigurationsEqualForBackup(
946             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
947         assertEquals(expected.size(), actual.size());
948         for (WifiConfiguration expectedConfiguration : expected) {
949             String expectedConfigKey = expectedConfiguration.getProfileKey();
950             boolean didCompare = false;
951             for (WifiConfiguration actualConfiguration : actual) {
952                 String actualConfigKey = actualConfiguration.getProfileKey();
953                 if (actualConfigKey.equals(expectedConfigKey)) {
954                     assertConfigurationEqualForBackup(
955                             expectedConfiguration, actualConfiguration);
956                     didCompare = true;
957                 }
958             }
959             assertTrue(didCompare);
960         }
961     }
962 
963     /**
964      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
965      * that are saved into internal database by WifiConfigurationManager for network
966      * additions/updates.
967      */
assertConfigurationsEqualForConfigManagerAddOrUpdate( List<WifiConfiguration> expected, List<WifiConfiguration> actual)968     public static void assertConfigurationsEqualForConfigManagerAddOrUpdate(
969             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
970         assertEquals(expected.size(), actual.size());
971         for (WifiConfiguration expectedConfiguration : expected) {
972             String expectedConfigKey = expectedConfiguration.getProfileKey();
973             boolean didCompare = false;
974             for (WifiConfiguration actualConfiguration : actual) {
975                 String actualConfigKey = actualConfiguration.getProfileKey();
976                 if (actualConfigKey.equals(expectedConfigKey)) {
977                     assertConfigurationEqualForConfigManagerAddOrUpdate(
978                             expectedConfiguration, actualConfiguration);
979                     didCompare = true;
980                 }
981             }
982             assertTrue(didCompare);
983         }
984     }
985 
986     /**
987      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
988      * saved for config store.
989      */
assertConfigurationsEqualForConfigStore( List<WifiConfiguration> expected, List<WifiConfiguration> actual)990     public static void assertConfigurationsEqualForConfigStore(
991             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
992         assertEquals(expected.size(), actual.size());
993         for (WifiConfiguration expectedConfiguration : expected) {
994             String expectedConfigKey = expectedConfiguration.getProfileKey();
995             boolean didCompare = false;
996             for (WifiConfiguration actualConfiguration : actual) {
997                 String actualConfigKey = actualConfiguration.getProfileKey();
998                 if (actualConfigKey.equals(expectedConfigKey)) {
999                     assertConfigurationEqualForConfigStore(
1000                             expectedConfiguration, actualConfiguration);
1001                     didCompare = true;
1002                 }
1003             }
1004             assertTrue(didCompare);
1005         }
1006     }
1007 }
1008