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