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.IpConfiguration;
22 import android.net.LinkAddress;
23 import android.net.NetworkUtils;
24 import android.net.ProxyInfo;
25 import android.net.StaticIpConfiguration;
26 import android.net.Uri;
27 import android.net.wifi.WifiConfiguration;
28 import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
29 import android.net.wifi.WifiEnterpriseConfig;
30 import android.net.wifi.WifiSsid;
31 import android.text.TextUtils;
32 
33 import java.net.InetAddress;
34 import java.security.cert.X509Certificate;
35 import java.util.Arrays;
36 import java.util.List;
37 
38 /**
39  * Helper for creating and populating WifiConfigurations in unit tests.
40  */
41 public class WifiConfigurationTestUtil {
42     /**
43      * These values are used to describe AP's security setting. One AP can support multiple of them,
44      * only if there is no conflict.
45      */
46     public static final int SECURITY_NONE = 0;
47     public static final int SECURITY_WEP =  1 << 0;
48     public static final int SECURITY_PSK =  1 << 1;
49     public static final int SECURITY_EAP =  1 << 2;
50     public static final int SECURITY_SAE =  1 << 3;
51     public static final int SECURITY_OWE =  1 << 4;
52     public static final int SECURITY_EAP_SUITE_B =  1 << 5;
53 
54     /**
55      * These values are used to describe ip configuration parameters for a network.
56      */
57     public static final int STATIC_IP_ASSIGNMENT = 0;
58     public static final int DHCP_IP_ASSIGNMENT = 1;
59     public static final int STATIC_PROXY_SETTING = 0;
60     public static final int PAC_PROXY_SETTING = 1;
61     public static final int NONE_PROXY_SETTING = 2;
62 
63     /**
64      * These are constants used to generate predefined WifiConfiguration objects.
65      */
66     public static final int TEST_NETWORK_ID = -1;
67     public static final int TEST_UID = 5;
68     public static final String TEST_SSID = "WifiConfigurationTestSSID";
69     public static final String TEST_PSK = "\"WifiConfigurationTestUtilPsk\"";
70     public static final String[] TEST_WEP_KEYS =
71             {"\"WifiConfigurationTestUtilWep1\"", "\"WifiConfigurationTestUtilWep2\"",
72                     "45342312ab", "45342312ab45342312ab34ac12"};
73     public static final String TEST_EAP_PASSWORD = "WifiConfigurationTestUtilEapPassword";
74     public static final int TEST_WEP_TX_KEY_INDEX = 1;
75     public static final String TEST_FQDN = "WifiConfigurationTestUtilFQDN";
76     public static final String TEST_PROVIDER_FRIENDLY_NAME =
77             "WifiConfigurationTestUtilFriendlyName";
78     public static final String TEST_STATIC_IP_LINK_ADDRESS = "192.168.48.2";
79     public static final int TEST_STATIC_IP_LINK_PREFIX_LENGTH = 8;
80     public static final String TEST_STATIC_IP_GATEWAY_ADDRESS = "192.168.48.1";
81     public static final String[] TEST_STATIC_IP_DNS_SERVER_ADDRESSES =
82             new String[]{"192.168.48.1", "192.168.48.10"};
83     public static final String TEST_STATIC_PROXY_HOST = "192.168.48.1";
84     public static final int TEST_STATIC_PROXY_PORT = 8000;
85     public static final String TEST_STATIC_PROXY_EXCLUSION_LIST = "";
86     public static final String TEST_PAC_PROXY_LOCATION = "http://";
87     public static final String TEST_CA_CERT_ALIAS = "WifiConfigurationTestUtilCaCertAlias";
88 
89     private static final int MAX_SSID_LENGTH = 32;
90     /**
91      * Index used to assign unique SSIDs for the generation of predefined WifiConfiguration objects.
92      */
93     private static int sNetworkIndex = 0;
94 
95     /**
96      * Construct a {@link android.net.wifi.WifiConfiguration}.
97      * @param networkId the configuration's networkId
98      * @param uid the configuration's creator uid
99      * @param ssid the configuration's ssid
100      * @param shared whether the configuration is shared with other users on the device
101      * @param enabled whether the configuration is enabled
102      * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
103      * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
104      * @return the constructed {@link android.net.wifi.WifiConfiguration}
105      */
generateWifiConfig(int networkId, int uid, String ssid, boolean shared, boolean enabled, String fqdn, String providerFriendlyName)106     public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
107             boolean shared, boolean enabled, String fqdn, String providerFriendlyName) {
108         final WifiConfiguration config = new WifiConfiguration();
109         config.SSID = ssid;
110         config.networkId = networkId;
111         config.creatorUid = uid;
112         config.shared = shared;
113         config.status = enabled ? WifiConfiguration.Status.ENABLED
114                 : WifiConfiguration.Status.DISABLED;
115         config.FQDN = fqdn;
116         config.providerFriendlyName = providerFriendlyName;
117         return config;
118     }
119 
120     /**
121      * Construct a {@link android.net.wifi.WifiConfiguration}.
122      * @param networkId the configuration's networkId
123      * @param uid the configuration's creator uid
124      * @param ssid the configuration's ssid
125      * @param shared whether the configuration is shared with other users on the device
126      * @param enabled whether the configuration is enabled
127      * @param fqdn the configuration's FQDN (Hotspot 2.0 only)
128      * @param providerFriendlyName the configuration's provider's friendly name (Hotspot 2.0 only)
129      * @param security the configuration's security type
130      * @return the constructed {@link android.net.wifi.WifiConfiguration}
131      */
generateWifiConfig(int networkId, int uid, String ssid, boolean shared, boolean enabled, String fqdn, String providerFriendlyName, int security)132     public static WifiConfiguration generateWifiConfig(int networkId, int uid, String ssid,
133             boolean shared, boolean enabled, String fqdn, String providerFriendlyName,
134             int security) {
135         WifiConfiguration config = generateWifiConfig(networkId, uid, ssid, shared, enabled, fqdn,
136                 providerFriendlyName);
137 
138         if ((security == SECURITY_NONE) || ((security & SECURITY_WEP) != 0)) {
139             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
140         } else {
141             if ((security & SECURITY_PSK) != 0) {
142                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
143             }
144 
145             if ((security & SECURITY_SAE) != 0) {
146                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.SAE);
147                 config.requirePMF = true;
148             }
149 
150             if ((security & SECURITY_OWE) != 0) {
151                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.OWE);
152                 config.requirePMF = true;
153             }
154 
155             if ((security & SECURITY_EAP) != 0) {
156                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
157                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
158                 config.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS);
159             }
160 
161             if ((security & SECURITY_EAP_SUITE_B) != 0) {
162                 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.SUITE_B_192);
163                 config.requirePMF = true;
164             }
165 
166         }
167         return config;
168     }
169 
170     /**
171      * Construct a {@link android.net.IpConfiguration }.
172      * @param ipAssignmentType One of {@link #STATIC_IP_ASSIGNMENT} or {@link #DHCP_IP_ASSIGNMENT}.
173      * @param proxySettingType One of {@link #STATIC_PROXY_SETTING} or {@link #PAC_PROXY_SETTING} or
174      *                        {@link #NONE_PROXY_SETTING}.
175      * @param linkAddress static ip address string.
176      * @param linkPrefixLength static ip address prefix length.
177      * @param gatewayAddress static gateway address.
178      * @param dnsServerAddresses list of dns servers for static ip configuration.
179      * @param proxyHost Static proxy server address.
180      * @param proxyPort Static proxy server port.
181      * @param proxyExclusionList Static proxy exclusion list.
182      * @param pacProxyPath Pac proxy server path.
183      * @return the constructed {@link android.net.IpConfiguration}
184      */
generateIpConfig( int ipAssignmentType, int proxySettingType, String linkAddress, int linkPrefixLength, String gatewayAddress, String[] dnsServerAddresses, String proxyHost, int proxyPort, String proxyExclusionList, String pacProxyPath)185     public static IpConfiguration generateIpConfig(
186             int ipAssignmentType, int proxySettingType, String linkAddress, int linkPrefixLength,
187             String gatewayAddress, String[] dnsServerAddresses, String proxyHost,
188             int proxyPort, String proxyExclusionList, String pacProxyPath) {
189         StaticIpConfiguration staticIpConfiguration = null;
190         ProxyInfo proxyInfo = null;
191         IpConfiguration.IpAssignment ipAssignment = IpConfiguration.IpAssignment.UNASSIGNED;
192         IpConfiguration.ProxySettings proxySettings = IpConfiguration.ProxySettings.UNASSIGNED;
193 
194         if (ipAssignmentType == STATIC_IP_ASSIGNMENT) {
195             staticIpConfiguration = new StaticIpConfiguration();
196             if (!TextUtils.isEmpty(linkAddress)) {
197                 LinkAddress linkAddr =
198                         new LinkAddress(
199                                 NetworkUtils.numericToInetAddress(linkAddress), linkPrefixLength);
200                 staticIpConfiguration.ipAddress = linkAddr;
201             }
202 
203             if (!TextUtils.isEmpty(gatewayAddress)) {
204                 InetAddress gatewayAddr =
205                         NetworkUtils.numericToInetAddress(gatewayAddress);
206                 staticIpConfiguration.gateway = gatewayAddr;
207             }
208             if (dnsServerAddresses != null) {
209                 for (String dnsServerAddress : dnsServerAddresses) {
210                     if (!TextUtils.isEmpty(dnsServerAddress)) {
211                         staticIpConfiguration.dnsServers.add(
212                                 NetworkUtils.numericToInetAddress(dnsServerAddress));
213                     }
214 
215                 }
216             }
217             ipAssignment = IpConfiguration.IpAssignment.STATIC;
218         } else if (ipAssignmentType == DHCP_IP_ASSIGNMENT) {
219             ipAssignment = IpConfiguration.IpAssignment.DHCP;
220         }
221 
222         if (proxySettingType == STATIC_PROXY_SETTING) {
223             proxyInfo = new ProxyInfo(proxyHost, proxyPort, proxyExclusionList);
224             proxySettings = IpConfiguration.ProxySettings.STATIC;
225         } else if (proxySettingType == PAC_PROXY_SETTING) {
226             proxyInfo = ProxyInfo.buildPacProxy(Uri.parse(pacProxyPath));
227             proxySettings = IpConfiguration.ProxySettings.PAC;
228         } else if (proxySettingType == NONE_PROXY_SETTING) {
229             proxySettings = IpConfiguration.ProxySettings.NONE;
230         }
231         return new IpConfiguration(ipAssignment, proxySettings, staticIpConfiguration, proxyInfo);
232     }
233 
234     /**
235      * Create a new SSID for the the network being created.
236      */
createNewSSID()237     private static String createNewSSID() {
238         String ssid = TEST_SSID + sNetworkIndex++;
239         assertTrue(ssid.length() <= MAX_SSID_LENGTH);
240         return "\"" + ssid + "\"";
241     }
242 
243     /**
244      * Helper methods to generate predefined WifiConfiguration objects of the required type. These
245      * use a static index to avoid duplicate configurations.
246      */
createOweNetwork()247     public static WifiConfiguration createOweNetwork() {
248         return createOweNetwork(createNewSSID());
249     }
250 
createOweNetwork(String ssid)251     public static WifiConfiguration createOweNetwork(String ssid) {
252         return generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
253                 null, SECURITY_OWE);
254     }
255 
createOpenNetwork()256     public static WifiConfiguration createOpenNetwork() {
257         return createOpenNetwork(createNewSSID());
258     }
259 
createOpenNetwork(String ssid)260     public static WifiConfiguration createOpenNetwork(String ssid) {
261         return generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
262                 null, SECURITY_NONE);
263     }
264 
createEphemeralNetwork()265     public static WifiConfiguration createEphemeralNetwork() {
266         WifiConfiguration configuration = createOpenNetwork();
267         configuration.ephemeral = true;
268         return configuration;
269     }
270 
createOpenHiddenNetwork()271     public static WifiConfiguration createOpenHiddenNetwork() {
272         WifiConfiguration configuration = createOpenNetwork();
273         configuration.hiddenSSID = true;
274         return configuration;
275     }
276 
createSaeNetwork()277     public static WifiConfiguration createSaeNetwork() {
278         WifiConfiguration configuration =
279                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
280                         null, SECURITY_SAE);
281 
282         // SAE password uses the same member.
283         configuration.preSharedKey = TEST_PSK;
284         configuration.requirePMF = true;
285         return configuration;
286     }
287 
createPskNetwork()288     public static WifiConfiguration createPskNetwork() {
289         WifiConfiguration configuration =
290                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
291                         null, SECURITY_PSK);
292         configuration.preSharedKey = TEST_PSK;
293         return configuration;
294     }
295 
createPskNetwork(String ssid)296     public static WifiConfiguration createPskNetwork(String ssid) {
297         WifiConfiguration configuration =
298                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
299                         null, SECURITY_PSK);
300         configuration.preSharedKey = TEST_PSK;
301         return configuration;
302     }
303 
createSaeNetwork(String ssid)304     public static WifiConfiguration createSaeNetwork(String ssid) {
305         WifiConfiguration configuration =
306                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true, null,
307                         null, SECURITY_SAE);
308         return configuration;
309     }
310 
createPskHiddenNetwork()311     public static WifiConfiguration createPskHiddenNetwork() {
312         WifiConfiguration configuration = createPskNetwork();
313         configuration.hiddenSSID = true;
314         return configuration;
315     }
316 
createWepNetwork()317     public static WifiConfiguration createWepNetwork() {
318         WifiConfiguration configuration =
319                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
320                         null, SECURITY_WEP);
321         configuration.wepKeys = TEST_WEP_KEYS;
322         configuration.wepTxKeyIndex = TEST_WEP_TX_KEY_INDEX;
323         return configuration;
324     }
325 
createWepHiddenNetwork()326     public static WifiConfiguration createWepHiddenNetwork() {
327         WifiConfiguration configuration = createWepNetwork();
328         configuration.hiddenSSID = true;
329         return configuration;
330     }
331 
createWepNetworkWithSingleKey()332     public static WifiConfiguration createWepNetworkWithSingleKey() {
333         WifiConfiguration configuration =
334                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true, null,
335                         null, SECURITY_WEP);
336         configuration.wepKeys[0] = TEST_WEP_KEYS[0];
337         configuration.wepTxKeyIndex = 0;
338         return configuration;
339     }
340 
341 
createEapNetwork()342     public static WifiConfiguration createEapNetwork() {
343         WifiConfiguration configuration =
344                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
345                         null, null, SECURITY_EAP);
346         return configuration;
347     }
348 
createEapNetwork(String ssid)349     public static WifiConfiguration createEapNetwork(String ssid) {
350         WifiConfiguration configuration =
351                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, ssid, true, true,
352                         null, null, SECURITY_EAP);
353         return configuration;
354     }
355 
356 
createEapNetwork(int eapMethod, int phase2Method)357     public static WifiConfiguration createEapNetwork(int eapMethod, int phase2Method) {
358         WifiConfiguration configuration =
359                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
360                         null, null, SECURITY_EAP);
361         configuration.enterpriseConfig.setEapMethod(eapMethod);
362         configuration.enterpriseConfig.setPhase2Method(phase2Method);
363         return configuration;
364     }
365 
createEapSuiteBNetwork()366     public static WifiConfiguration createEapSuiteBNetwork() {
367         WifiConfiguration configuration =
368                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
369                         null, null, SECURITY_EAP_SUITE_B);
370 
371         configuration.requirePMF = true;
372         configuration.enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
373         configuration.enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
374         return configuration;
375     }
376 
createPasspointNetwork()377     public static WifiConfiguration createPasspointNetwork() {
378         WifiConfiguration configuration =
379                 generateWifiConfig(TEST_NETWORK_ID, TEST_UID, createNewSSID(), true, true,
380                         TEST_FQDN, TEST_PROVIDER_FRIENDLY_NAME, SECURITY_EAP);
381         return configuration;
382     }
383 
createStaticIpConfigurationWithPacProxy()384     public static IpConfiguration createStaticIpConfigurationWithPacProxy() {
385         return generateIpConfig(
386                 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
387                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
388                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
389                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
390                 TEST_PAC_PROXY_LOCATION);
391     }
392 
createStaticIpConfigurationWithStaticProxy()393     public static IpConfiguration createStaticIpConfigurationWithStaticProxy() {
394         return generateIpConfig(
395                 STATIC_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
396                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
397                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
398                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
399                 TEST_PAC_PROXY_LOCATION);
400     }
401 
createPartialStaticIpConfigurationWithPacProxy()402     public static IpConfiguration createPartialStaticIpConfigurationWithPacProxy() {
403         return generateIpConfig(
404                 STATIC_IP_ASSIGNMENT, PAC_PROXY_SETTING,
405                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
406                 null, null,
407                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
408                 TEST_PAC_PROXY_LOCATION);
409     }
410 
createDHCPIpConfigurationWithPacProxy()411     public static IpConfiguration createDHCPIpConfigurationWithPacProxy() {
412         return generateIpConfig(
413                 DHCP_IP_ASSIGNMENT, PAC_PROXY_SETTING,
414                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
415                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
416                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
417                 TEST_PAC_PROXY_LOCATION);
418     }
419 
createDHCPIpConfigurationWithStaticProxy()420     public static IpConfiguration createDHCPIpConfigurationWithStaticProxy() {
421         return generateIpConfig(
422                 DHCP_IP_ASSIGNMENT, STATIC_PROXY_SETTING,
423                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
424                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
425                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
426                 TEST_PAC_PROXY_LOCATION);
427     }
428 
createDHCPIpConfigurationWithNoProxy()429     public static IpConfiguration createDHCPIpConfigurationWithNoProxy() {
430         return generateIpConfig(
431                 DHCP_IP_ASSIGNMENT, NONE_PROXY_SETTING,
432                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
433                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
434                 TEST_STATIC_PROXY_HOST, TEST_STATIC_PROXY_PORT, TEST_STATIC_PROXY_EXCLUSION_LIST,
435                 TEST_PAC_PROXY_LOCATION);
436     }
437 
438     /**
439      * Creates an IP configuration with specific parameters.
440      * @param proxySetting Must be one of {@link WifiConfigurationTestUtil#STATIC_PROXY_SETTING},
441      * {@link WifiConfigurationTestUtil#PAC_PROXY_SETTING},
442      * {@link WifiConfigurationTestUtil#NONE_PROXY_SETTING}
443      */
createDHCPIpConfigurationWithSpecificProxy( int proxySetting, String staticProxyHost, int staticProxyPort, String staticProxyExclusionList, String pacProxyLocation)444     public static IpConfiguration createDHCPIpConfigurationWithSpecificProxy(
445             int proxySetting,
446             String staticProxyHost,
447             int staticProxyPort,
448             String staticProxyExclusionList,
449             String pacProxyLocation) {
450         return generateIpConfig(
451                 DHCP_IP_ASSIGNMENT, proxySetting,
452                 TEST_STATIC_IP_LINK_ADDRESS, TEST_STATIC_IP_LINK_PREFIX_LENGTH,
453                 TEST_STATIC_IP_GATEWAY_ADDRESS, TEST_STATIC_IP_DNS_SERVER_ADDRESSES,
454                 staticProxyHost, staticProxyPort, staticProxyExclusionList,
455                 pacProxyLocation);
456     }
457 
458     // TODO: These enterprise configurations may need more parameters set.
createPEAPWifiEnterpriseConfigWithGTCPhase2()459     public static WifiEnterpriseConfig createPEAPWifiEnterpriseConfigWithGTCPhase2() {
460         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
461         config.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
462         config.setPhase2Method(WifiEnterpriseConfig.Phase2.GTC);
463         config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "PEAP"});
464         config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
465         return config;
466     }
467 
createTLSWifiEnterpriseConfigWithNonePhase2()468     public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithNonePhase2() {
469         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
470         config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
471         config.setPhase2Method(WifiEnterpriseConfig.Phase2.NONE);
472         config.setCaCertificateAliases(new String[] {TEST_CA_CERT_ALIAS + "TLS"});
473         config.setCaCertificates(new X509Certificate[] {FakeKeys.CA_CERT0, FakeKeys.CA_CERT1});
474         return config;
475     }
476 
createTLSWifiEnterpriseConfigWithAkaPhase2()477     public static WifiEnterpriseConfig createTLSWifiEnterpriseConfigWithAkaPhase2() {
478         WifiEnterpriseConfig config = new WifiEnterpriseConfig();
479         config.setEapMethod(WifiEnterpriseConfig.Eap.TLS);
480         config.setPhase2Method(WifiEnterpriseConfig.Phase2.AKA);
481         return config;
482     }
483 
484     /**
485      * Gets scan result capabilities for a particular network configuration.
486      */
getScanResultCapsForNetwork(WifiConfiguration configuration)487     public static String getScanResultCapsForNetwork(WifiConfiguration configuration) {
488         String caps;
489         if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) {
490             caps = "[RSN-PSK-CCMP]";
491         } else if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)
492                 || configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) {
493             caps = "[RSN-EAP-CCMP]";
494         } else if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)
495                 && WifiConfigurationUtil.hasAnyValidWepKey(configuration.wepKeys)) {
496             caps = "[WEP]";
497         } else if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SAE)) {
498             caps = "[RSN-SAE-CCMP]";
499         } else if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.OWE)) {
500             caps = "[RSN-OWE-CCMP]";
501         } else if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.SUITE_B_192)) {
502             caps = "[RSN-SUITE-B-192-CCMP]";
503         } else {
504             caps = "[]";
505         }
506         return caps;
507     }
508 
509     /**
510      * Gets scan result capabilities for a WPA2/WPA3-Transition mode network configuration
511      */
512     private static String
getScanResultCapsForWpa2Wpa3TransitionNetwork(WifiConfiguration configuration)513             getScanResultCapsForWpa2Wpa3TransitionNetwork(WifiConfiguration configuration) {
514         String caps = "[RSN-PSK+SAE-CCMP]";
515         return caps;
516     }
517 
518     /**
519      * Creates a scan detail corresponding to the provided network and given BSSID, etc.
520      */
createScanDetailForNetwork( WifiConfiguration configuration, String bssid, int level, int frequency, long tsf, long seen)521     public static ScanDetail createScanDetailForNetwork(
522             WifiConfiguration configuration, String bssid, int level, int frequency,
523             long tsf, long seen) {
524         String caps = getScanResultCapsForNetwork(configuration);
525         WifiSsid ssid = WifiSsid.createFromAsciiEncoded(configuration.getPrintableSsid());
526         return new ScanDetail(ssid, bssid, caps, level, frequency, tsf, seen);
527     }
528 
529     /**
530      * Creates a scan detail corresponding to the provided network and given BSSID, but sets
531      * the capabilities to WPA2/WPA3-Transition mode network.
532      */
createScanDetailForWpa2Wpa3TransitionModeNetwork( WifiConfiguration configuration, String bssid, int level, int frequency, long tsf, long seen)533     public static ScanDetail createScanDetailForWpa2Wpa3TransitionModeNetwork(
534             WifiConfiguration configuration, String bssid, int level, int frequency,
535             long tsf, long seen) {
536         String caps = getScanResultCapsForWpa2Wpa3TransitionNetwork(configuration);
537         WifiSsid ssid = WifiSsid.createFromAsciiEncoded(configuration.getPrintableSsid());
538         return new ScanDetail(ssid, bssid, caps, level, frequency, tsf, seen);
539     }
540 
541 
542     /**
543      * Asserts that the 2 WifiConfigurations are equal in the elements saved for both backup/restore
544      * and config store.
545      */
assertCommonConfigurationElementsEqual( WifiConfiguration expected, WifiConfiguration actual)546     private static void assertCommonConfigurationElementsEqual(
547             WifiConfiguration expected, WifiConfiguration actual) {
548         assertNotNull(expected);
549         assertNotNull(actual);
550         assertEquals(expected.SSID, actual.SSID);
551         assertEquals(expected.BSSID, actual.BSSID);
552         assertEquals(expected.preSharedKey, actual.preSharedKey);
553         assertEquals(expected.wepKeys, actual.wepKeys);
554         assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
555         assertEquals(expected.hiddenSSID, actual.hiddenSSID);
556         assertEquals(expected.requirePMF, actual.requirePMF);
557         assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
558         assertEquals(expected.allowedProtocols, actual.allowedProtocols);
559         assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
560         assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
561         assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
562         assertEquals(expected.shared, actual.shared);
563         assertEquals(expected.getIpConfiguration(), actual.getIpConfiguration());
564     }
565 
566     /**
567      * Asserts that the 2 WifiConfigurations are equal. This only compares the elements saved
568      * for backup/restore.
569      */
assertConfigurationEqualForBackup( WifiConfiguration expected, WifiConfiguration actual)570     public static void assertConfigurationEqualForBackup(
571             WifiConfiguration expected, WifiConfiguration actual) {
572         assertCommonConfigurationElementsEqual(expected, actual);
573         assertEquals(expected.meteredOverride, actual.meteredOverride);
574     }
575 
576     /**
577      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements saved for
578      * config store.
579      */
assertConfigurationEqualForConfigStore( WifiConfiguration expected, WifiConfiguration actual)580     public static void assertConfigurationEqualForConfigStore(
581             WifiConfiguration expected, WifiConfiguration actual) {
582         assertCommonConfigurationElementsEqual(expected, actual);
583         assertEquals(expected.status, actual.status);
584         assertEquals(expected.FQDN, actual.FQDN);
585         assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
586         assertTrue(Arrays.equals(expected.roamingConsortiumIds, actual.roamingConsortiumIds));
587         assertEquals(expected.linkedConfigurations, actual.linkedConfigurations);
588         assertEquals(expected.defaultGwMacAddress, actual.defaultGwMacAddress);
589         assertEquals(expected.validatedInternetAccess, actual.validatedInternetAccess);
590         assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
591         assertEquals(expected.userApproved, actual.userApproved);
592         assertEquals(expected.meteredHint, actual.meteredHint);
593         assertEquals(expected.meteredOverride, actual.meteredOverride);
594         assertEquals(expected.useExternalScores, actual.useExternalScores);
595         assertEquals(expected.numAssociation, actual.numAssociation);
596         assertEquals(expected.creatorUid, actual.creatorUid);
597         assertEquals(expected.creatorName, actual.creatorName);
598         assertEquals(expected.creationTime, actual.creationTime);
599         assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
600         assertEquals(expected.lastUpdateName, actual.lastUpdateName);
601         assertEquals(expected.lastConnectUid, actual.lastConnectUid);
602         assertEquals(expected.updateTime, actual.updateTime);
603         assertEquals(expected.isLegacyPasspointConfig, actual.isLegacyPasspointConfig);
604         assertEquals(expected.getRandomizedMacAddress(), actual.getRandomizedMacAddress());
605         assertEquals(expected.macRandomizationSetting, actual.macRandomizationSetting);
606         assertNetworkSelectionStatusEqualForConfigStore(
607                 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
608         assertWifiEnterpriseConfigEqualForConfigStore(
609                 expected.enterpriseConfig, actual.enterpriseConfig);
610     }
611 
612     /**
613      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
614      * saved into internal database by WifiConfigurationManager for network additions/updates.
615      */
assertConfigurationEqualForConfigManagerAddOrUpdate( WifiConfiguration expected, WifiConfiguration actual)616     public static void assertConfigurationEqualForConfigManagerAddOrUpdate(
617             WifiConfiguration expected, WifiConfiguration actual) {
618         assertCommonConfigurationElementsEqual(expected, actual);
619         assertEquals(expected.FQDN, actual.FQDN);
620         assertEquals(expected.providerFriendlyName, actual.providerFriendlyName);
621         assertEquals(expected.noInternetAccessExpected, actual.noInternetAccessExpected);
622         assertEquals(expected.meteredHint, actual.meteredHint);
623         assertEquals(expected.meteredOverride, actual.meteredOverride);
624         assertEquals(expected.useExternalScores, actual.useExternalScores);
625         assertEquals(expected.ephemeral, actual.ephemeral);
626         assertEquals(expected.osu, actual.osu);
627         assertEquals(expected.trusted, actual.trusted);
628         assertEquals(expected.fromWifiNetworkSuggestion, actual.fromWifiNetworkSuggestion);
629         assertEquals(expected.fromWifiNetworkSpecifier, actual.fromWifiNetworkSpecifier);
630         assertEquals(expected.creatorUid, actual.creatorUid);
631         assertEquals(expected.creatorName, actual.creatorName);
632         assertEquals(expected.creationTime, actual.creationTime);
633         assertEquals(expected.lastUpdateUid, actual.lastUpdateUid);
634         assertEquals(expected.lastUpdateName, actual.lastUpdateName);
635         assertEquals(expected.updateTime, actual.updateTime);
636         assertNetworkSelectionStatusEqualForConfigStore(
637                 expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
638         assertWifiEnterpriseConfigEqualForConfigStore(
639                 expected.enterpriseConfig, actual.enterpriseConfig);
640     }
641 
642     /**
643      * Asserts that the 2 WifiConfigurations are equal. This compares all the elements that are
644      * saved into wpa_supplicant by SupplicantStaNetwork.
645      */
assertConfigurationEqualForSupplicant( WifiConfiguration expected, WifiConfiguration actual)646     public static void assertConfigurationEqualForSupplicant(
647             WifiConfiguration expected, WifiConfiguration actual) {
648         assertNotNull(expected);
649         assertNotNull(actual);
650         assertEquals(expected.SSID, actual.SSID);
651         assertEquals(expected.getNetworkSelectionStatus().getNetworkSelectionBSSID(),
652                 actual.getNetworkSelectionStatus().getNetworkSelectionBSSID());
653         assertEquals(expected.preSharedKey, actual.preSharedKey);
654         assertEquals(expected.wepKeys, actual.wepKeys);
655         assertEquals(expected.wepTxKeyIndex, actual.wepTxKeyIndex);
656         assertEquals(expected.hiddenSSID, actual.hiddenSSID);
657         assertEquals(expected.requirePMF, actual.requirePMF);
658         assertEquals(expected.allowedKeyManagement, actual.allowedKeyManagement);
659         assertEquals(expected.allowedProtocols, actual.allowedProtocols);
660         assertEquals(expected.allowedAuthAlgorithms, actual.allowedAuthAlgorithms);
661         assertEquals(expected.allowedGroupCiphers, actual.allowedGroupCiphers);
662         assertEquals(expected.allowedPairwiseCiphers, actual.allowedPairwiseCiphers);
663         assertWifiEnterpriseConfigEqualForConfigStore(
664                 expected.enterpriseConfig, actual.enterpriseConfig);
665     }
666 
667     /**
668      * Asserts that the 2 WifiConfigurations are equal. This is a generic version of the comparator
669      * which is used in QNS tests for comparing the network selections.
670      * This importantly checks that the networkId's of the 2 configs are equal.
671      */
assertConfigurationEqual( WifiConfiguration expected, WifiConfiguration actual)672     public static void assertConfigurationEqual(
673             WifiConfiguration expected, WifiConfiguration actual) {
674         assertCommonConfigurationElementsEqual(expected, actual);
675         assertEquals(expected.networkId, actual.networkId);
676         assertEquals(expected.ephemeral, actual.ephemeral);
677         assertEquals(expected.fromWifiNetworkSuggestion, actual.fromWifiNetworkSuggestion);
678         assertEquals(expected.fromWifiNetworkSpecifier, actual.fromWifiNetworkSpecifier);
679         assertEquals(expected.trusted, actual.trusted);
680     }
681 
682     /**
683      * Assert that the 2 NetworkSelectionStatus's are equal. This compares all the elements saved
684      * for config store.
685      */
assertNetworkSelectionStatusEqualForConfigStore( NetworkSelectionStatus expected, NetworkSelectionStatus actual)686     public static void assertNetworkSelectionStatusEqualForConfigStore(
687             NetworkSelectionStatus expected, NetworkSelectionStatus actual) {
688         if (expected.isNetworkTemporaryDisabled()) {
689             // Temporarily disabled networks are enabled when persisted.
690             assertEquals(
691                     NetworkSelectionStatus.NETWORK_SELECTION_ENABLED,
692                     actual.getNetworkSelectionStatus());
693             assertEquals(
694                     NetworkSelectionStatus.NETWORK_SELECTION_ENABLE,
695                     actual.getNetworkSelectionDisableReason());
696         } else {
697             assertEquals(expected.getNetworkSelectionStatus(), actual.getNetworkSelectionStatus());
698             assertEquals(
699                     expected.getNetworkSelectionDisableReason(),
700                     actual.getNetworkSelectionDisableReason());
701         }
702         assertEquals(expected.getConnectChoice(), actual.getConnectChoice());
703         assertEquals(expected.getConnectChoiceTimestamp(), actual.getConnectChoiceTimestamp());
704         assertEquals(expected.getHasEverConnected(), actual.getHasEverConnected());
705     }
706 
707     /**
708      * Assert that the 2 WifiEnterpriseConfig's are equal. This compares all the elements saved
709      * for config store.
710      */
assertWifiEnterpriseConfigEqualForConfigStore( WifiEnterpriseConfig expected, WifiEnterpriseConfig actual)711     public static void assertWifiEnterpriseConfigEqualForConfigStore(
712             WifiEnterpriseConfig expected, WifiEnterpriseConfig actual) {
713         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY),
714                 actual.getFieldValue(WifiEnterpriseConfig.IDENTITY_KEY));
715         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY),
716                 actual.getFieldValue(WifiEnterpriseConfig.ANON_IDENTITY_KEY));
717         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY),
718                 actual.getFieldValue(WifiEnterpriseConfig.PASSWORD_KEY));
719         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY),
720                 actual.getFieldValue(WifiEnterpriseConfig.CLIENT_CERT_KEY));
721         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY),
722                 actual.getFieldValue(WifiEnterpriseConfig.CA_CERT_KEY));
723         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY),
724                 actual.getFieldValue(WifiEnterpriseConfig.SUBJECT_MATCH_KEY));
725         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY),
726                 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_KEY));
727         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY),
728                 actual.getFieldValue(WifiEnterpriseConfig.ENGINE_ID_KEY));
729         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY),
730                 actual.getFieldValue(WifiEnterpriseConfig.PRIVATE_KEY_ID_KEY));
731         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY),
732                 actual.getFieldValue(WifiEnterpriseConfig.ALTSUBJECT_MATCH_KEY));
733         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY),
734                 actual.getFieldValue(WifiEnterpriseConfig.DOM_SUFFIX_MATCH_KEY));
735         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY),
736                 actual.getFieldValue(WifiEnterpriseConfig.CA_PATH_KEY));
737         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.REALM_KEY),
738                 actual.getFieldValue(WifiEnterpriseConfig.REALM_KEY));
739         assertEquals(expected.getFieldValue(WifiEnterpriseConfig.PLMN_KEY),
740                 actual.getFieldValue(WifiEnterpriseConfig.PLMN_KEY));
741         assertEquals(expected.getEapMethod(), actual.getEapMethod());
742         assertEquals(expected.getPhase2Method(), actual.getPhase2Method());
743     }
744 
745     /**
746      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
747      * saved for backup/restore.
748      */
assertConfigurationsEqualForBackup( List<WifiConfiguration> expected, List<WifiConfiguration> actual)749     public static void assertConfigurationsEqualForBackup(
750             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
751         assertEquals(expected.size(), actual.size());
752         for (WifiConfiguration expectedConfiguration : expected) {
753             String expectedConfigKey = expectedConfiguration.configKey();
754             boolean didCompare = false;
755             for (WifiConfiguration actualConfiguration : actual) {
756                 String actualConfigKey = actualConfiguration.configKey();
757                 if (actualConfigKey.equals(expectedConfigKey)) {
758                     assertConfigurationEqualForBackup(
759                             expectedConfiguration, actualConfiguration);
760                     didCompare = true;
761                 }
762             }
763             assertTrue(didCompare);
764         }
765     }
766 
767     /**
768      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
769      * that are saved into internal database by WifiConfigurationManager for network
770      * additions/updates.
771      */
assertConfigurationsEqualForConfigManagerAddOrUpdate( List<WifiConfiguration> expected, List<WifiConfiguration> actual)772     public static void assertConfigurationsEqualForConfigManagerAddOrUpdate(
773             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
774         assertEquals(expected.size(), actual.size());
775         for (WifiConfiguration expectedConfiguration : expected) {
776             String expectedConfigKey = expectedConfiguration.configKey();
777             boolean didCompare = false;
778             for (WifiConfiguration actualConfiguration : actual) {
779                 String actualConfigKey = actualConfiguration.configKey();
780                 if (actualConfigKey.equals(expectedConfigKey)) {
781                     assertConfigurationEqualForConfigManagerAddOrUpdate(
782                             expectedConfiguration, actualConfiguration);
783                     didCompare = true;
784                 }
785             }
786             assertTrue(didCompare);
787         }
788     }
789 
790     /**
791      * Asserts that the 2 lists of WifiConfigurations are equal. This compares all the elements
792      * saved for config store.
793      */
assertConfigurationsEqualForConfigStore( List<WifiConfiguration> expected, List<WifiConfiguration> actual)794     public static void assertConfigurationsEqualForConfigStore(
795             List<WifiConfiguration> expected, List<WifiConfiguration> actual) {
796         assertEquals(expected.size(), actual.size());
797         for (WifiConfiguration expectedConfiguration : expected) {
798             String expectedConfigKey = expectedConfiguration.configKey();
799             boolean didCompare = false;
800             for (WifiConfiguration actualConfiguration : actual) {
801                 String actualConfigKey = actualConfiguration.configKey();
802                 if (actualConfigKey.equals(expectedConfigKey)) {
803                     assertConfigurationEqualForConfigStore(
804                             expectedConfiguration, actualConfiguration);
805                     didCompare = true;
806                 }
807             }
808             assertTrue(didCompare);
809         }
810     }
811 }
812