1 /*
2  * Copyright (C) 2020 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 android.net.wifi;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotEquals;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
25 import android.net.wifi.WifiConfiguration.GroupCipher;
26 import android.net.wifi.WifiConfiguration.GroupMgmtCipher;
27 import android.net.wifi.WifiConfiguration.KeyMgmt;
28 import android.net.wifi.WifiConfiguration.PairwiseCipher;
29 import android.net.wifi.WifiConfiguration.Protocol;
30 import android.os.Parcel;
31 
32 import androidx.test.filters.SmallTest;
33 
34 import org.junit.Test;
35 
36 import java.util.BitSet;
37 
38 /**
39  * Unit tests for {@link android.net.wifi.WifiInfo}.
40  */
41 @SmallTest
42 public class SecurityParamsTest {
43 
verifySecurityParams(SecurityParams params, int expectedSecurityType, int[] expectedAllowedKeyManagement, int[] expectedAllowedProtocols, int[] expectedAllowedAuthAlgorithms, int[] expectedAllowedPairwiseCiphers, int[] expectedAllowedGroupCiphers, boolean expectedRequirePmf)44     private void verifySecurityParams(SecurityParams params,
45             int expectedSecurityType,
46             int[] expectedAllowedKeyManagement,
47             int[] expectedAllowedProtocols,
48             int[] expectedAllowedAuthAlgorithms,
49             int[] expectedAllowedPairwiseCiphers,
50             int[] expectedAllowedGroupCiphers,
51             boolean expectedRequirePmf) {
52         assertTrue(params.isSecurityType(expectedSecurityType));
53         assertEquals(expectedSecurityType, params.getSecurityType());
54         for (int b: expectedAllowedKeyManagement) {
55             assertTrue(params.getAllowedKeyManagement().get(b));
56         }
57         for (int b: expectedAllowedProtocols) {
58             assertTrue(params.getAllowedProtocols().get(b));
59         }
60         for (int b: expectedAllowedAuthAlgorithms) {
61             assertTrue(params.getAllowedAuthAlgorithms().get(b));
62         }
63         for (int b: expectedAllowedPairwiseCiphers) {
64             assertTrue(params.getAllowedPairwiseCiphers().get(b));
65         }
66         for (int b: expectedAllowedGroupCiphers) {
67             assertTrue(params.getAllowedGroupCiphers().get(b));
68         }
69         assertEquals(expectedRequirePmf, params.isRequirePmf());
70     }
71 
72     /** Verify the security params created by security type. */
73     @Test
testSecurityTypeCreator()74     public void testSecurityTypeCreator() throws Exception {
75         int[] securityTypes = new int[] {
76                 WifiConfiguration.SECURITY_TYPE_WAPI_CERT,
77                 WifiConfiguration.SECURITY_TYPE_WAPI_PSK,
78                 WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT,
79                 WifiConfiguration.SECURITY_TYPE_OWE,
80                 WifiConfiguration.SECURITY_TYPE_SAE,
81                 WifiConfiguration.SECURITY_TYPE_OSEN,
82                 WifiConfiguration.SECURITY_TYPE_EAP,
83                 WifiConfiguration.SECURITY_TYPE_PSK,
84                 WifiConfiguration.SECURITY_TYPE_OPEN,
85                 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2,
86                 WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3,
87         };
88 
89         for (int type: securityTypes) {
90             assertEquals(type,
91                     SecurityParams.createSecurityParamsBySecurityType(type).getSecurityType());
92         }
93     }
94 
95     /** Verify EAP params creator. */
96     @Test
testEapCreator()97     public void testEapCreator() throws Exception {
98         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP;
99         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X};
100         int[] expectedAllowedProtocols = new int[] {};
101         int[] expectedAllowedAuthAlgorithms = new int[] {};
102         int[] expectedAllowedPairwiseCiphers = new int[] {};
103         int[] expectedAllowedGroupCiphers = new int[] {};
104         boolean expectedRequirePmf = false;
105         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
106                 expectedSecurityType);
107         verifySecurityParams(p, expectedSecurityType,
108                 expectedAllowedKeyManagement, expectedAllowedProtocols,
109                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
110                 expectedAllowedGroupCiphers, expectedRequirePmf);
111     }
112 
113     /** Verify Passpoint R1/R2 params creator. */
114     @Test
testEapPasspointR1R2Creator()115     public void testEapPasspointR1R2Creator() throws Exception {
116         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2;
117         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X};
118         int[] expectedAllowedProtocols = new int[] {};
119         int[] expectedAllowedAuthAlgorithms = new int[] {};
120         int[] expectedAllowedPairwiseCiphers = new int[] {};
121         int[] expectedAllowedGroupCiphers = new int[] {};
122         boolean expectedRequirePmf = false;
123         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
124                 expectedSecurityType);
125         verifySecurityParams(p, expectedSecurityType,
126                 expectedAllowedKeyManagement, expectedAllowedProtocols,
127                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
128                 expectedAllowedGroupCiphers, expectedRequirePmf);
129     }
130 
131     /** Verify Passpoint R3 params creator. */
132     @Test
testEapPasspointR3Creator()133     public void testEapPasspointR3Creator() throws Exception {
134         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3;
135         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X};
136         int[] expectedAllowedProtocols = new int[] {};
137         int[] expectedAllowedAuthAlgorithms = new int[] {};
138         int[] expectedAllowedPairwiseCiphers = new int[] {};
139         int[] expectedAllowedGroupCiphers = new int[] {};
140         boolean expectedRequirePmf = true;
141         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
142                 expectedSecurityType);
143         verifySecurityParams(p, expectedSecurityType,
144                 expectedAllowedKeyManagement, expectedAllowedProtocols,
145                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
146                 expectedAllowedGroupCiphers, expectedRequirePmf);
147     }
148 
149     /** Verify Enhanced Open params creator. */
150     @Test
testEnhancedOpenCreator()151     public void testEnhancedOpenCreator() throws Exception {
152         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OWE;
153         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OWE};
154         int[] expectedAllowedProtocols = new int[] {Protocol.RSN};
155         int[] expectedAllowedAuthAlgorithms = new int[] {};
156         int[] expectedAllowedPairwiseCiphers = new int[] {
157                 PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256};
158         int[] expectedAllowedGroupCiphers = new int[] {
159                 GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256};
160         boolean expectedRequirePmf = true;
161         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
162                 expectedSecurityType);
163         verifySecurityParams(p, expectedSecurityType,
164                 expectedAllowedKeyManagement, expectedAllowedProtocols,
165                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
166                 expectedAllowedGroupCiphers, expectedRequirePmf);
167     }
168 
169     /** Verify Open params creator. */
170     @Test
testOpenCreator()171     public void testOpenCreator() throws Exception {
172         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OPEN;
173         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE};
174         int[] expectedAllowedProtocols = new int[] {};
175         int[] expectedAllowedAuthAlgorithms = new int[] {};
176         int[] expectedAllowedPairwiseCiphers = new int[] {};
177         int[] expectedAllowedGroupCiphers = new int[] {};
178         boolean expectedRequirePmf = false;
179         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
180                                 expectedSecurityType);
181         verifySecurityParams(p, expectedSecurityType,
182                 expectedAllowedKeyManagement, expectedAllowedProtocols,
183                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
184                 expectedAllowedGroupCiphers, expectedRequirePmf);
185     }
186 
187     /** Verify OSEN params creator. */
188     @Test
testOsenCreator()189     public void testOsenCreator() throws Exception {
190         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_OSEN;
191         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.OSEN};
192         int[] expectedAllowedProtocols = new int[] {Protocol.OSEN};
193         int[] expectedAllowedAuthAlgorithms = new int[] {};
194         int[] expectedAllowedPairwiseCiphers = new int[] {};
195         int[] expectedAllowedGroupCiphers = new int[] {};
196         boolean expectedRequirePmf = false;
197         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
198                                 expectedSecurityType);
199         verifySecurityParams(p, expectedSecurityType,
200                 expectedAllowedKeyManagement, expectedAllowedProtocols,
201                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
202                 expectedAllowedGroupCiphers, expectedRequirePmf);
203     }
204 
205     /** Verify WAPI CERT params creator. */
206     @Test
testWapiCertCreator()207     public void testWapiCertCreator() throws Exception {
208         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_CERT;
209         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_CERT};
210         int[] expectedAllowedProtocols = new int[] {Protocol.WAPI};
211         int[] expectedAllowedAuthAlgorithms = new int[] {};
212         int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4};
213         int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4};
214         boolean expectedRequirePmf = false;
215         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
216                                 expectedSecurityType);
217         verifySecurityParams(p, expectedSecurityType,
218                 expectedAllowedKeyManagement, expectedAllowedProtocols,
219                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
220                 expectedAllowedGroupCiphers, expectedRequirePmf);
221     }
222 
223     /** Verify WAPI PSK params creator. */
224     @Test
testWapiPskCreator()225     public void testWapiPskCreator() throws Exception {
226         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WAPI_PSK;
227         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WAPI_PSK};
228         int[] expectedAllowedProtocols = new int[] {Protocol.WAPI};
229         int[] expectedAllowedAuthAlgorithms = new int[] {};
230         int[] expectedAllowedPairwiseCiphers = new int[] {PairwiseCipher.SMS4};
231         int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.SMS4};
232         boolean expectedRequirePmf = false;
233         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
234                                 expectedSecurityType);
235         verifySecurityParams(p, expectedSecurityType,
236                 expectedAllowedKeyManagement, expectedAllowedProtocols,
237                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
238                 expectedAllowedGroupCiphers, expectedRequirePmf);
239     }
240 
241     /** Verify WEP params creator. */
242     @Test
testWepCreator()243     public void testWepCreator() throws Exception {
244         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_WEP;
245         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.NONE};
246         int[] expectedAllowedProtocols = new int[] {};
247         int[] expectedAllowedAuthAlgorithms = new int[] {AuthAlgorithm.OPEN, AuthAlgorithm.SHARED};
248         int[] expectedAllowedPairwiseCiphers = new int[] {};
249         int[] expectedAllowedGroupCiphers = new int[] {};
250         boolean expectedRequirePmf = false;
251         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
252                                 expectedSecurityType);
253         verifySecurityParams(p, expectedSecurityType,
254                 expectedAllowedKeyManagement, expectedAllowedProtocols,
255                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
256                 expectedAllowedGroupCiphers, expectedRequirePmf);
257     }
258 
259     /** Verify WPA3 Enterprise 192-bit params creator. */
260     @Test
testWpa3Enterprise192BitCreator()261     public void testWpa3Enterprise192BitCreator() throws Exception {
262         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT;
263         int[] expectedAllowedKeyManagement = new int[] {
264                 KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X, KeyMgmt.SUITE_B_192};
265         int[] expectedAllowedProtocols = new int[] {Protocol.RSN};
266         int[] expectedAllowedAuthAlgorithms = new int[] {};
267         int[] expectedAllowedPairwiseCiphers = new int[] {
268                 PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256};
269         int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.GCMP_128, GroupCipher.GCMP_256};
270         boolean expectedRequirePmf = true;
271         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
272                                 expectedSecurityType);
273         verifySecurityParams(p, expectedSecurityType,
274                 expectedAllowedKeyManagement, expectedAllowedProtocols,
275                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
276                 expectedAllowedGroupCiphers, expectedRequirePmf);
277 
278         assertTrue(p.getAllowedGroupManagementCiphers().get(GroupMgmtCipher.BIP_GMAC_256));
279     }
280 
281     /** Verify WPA3 Enterprise params creator. */
282     @Test
testWpa3EnterpriseCreator()283     public void testWpa3EnterpriseCreator() throws Exception {
284         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE;
285         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_EAP, KeyMgmt.IEEE8021X};
286         int[] expectedAllowedProtocols = new int[] {Protocol.RSN};
287         int[] expectedAllowedAuthAlgorithms = new int[] {};
288         int[] expectedAllowedPairwiseCiphers = new int[] {
289                 PairwiseCipher.CCMP, PairwiseCipher.GCMP_256};
290         int[] expectedAllowedGroupCiphers = new int[] {GroupCipher.CCMP, GroupCipher.GCMP_256};
291         boolean expectedRequirePmf = true;
292         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
293                                 expectedSecurityType);
294         verifySecurityParams(p, expectedSecurityType,
295                 expectedAllowedKeyManagement, expectedAllowedProtocols,
296                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
297                 expectedAllowedGroupCiphers, expectedRequirePmf);
298     }
299 
300     /** Verify WPA3 Personal params creator. */
301     @Test
testWpa3PersonalCreator()302     public void testWpa3PersonalCreator() throws Exception {
303         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_SAE;
304         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.SAE};
305         int[] expectedAllowedProtocols = new int[] {Protocol.RSN};
306         int[] expectedAllowedAuthAlgorithms = new int[] {};
307         int[] expectedAllowedPairwiseCiphers = new int[] {
308                 PairwiseCipher.CCMP, PairwiseCipher.GCMP_128, PairwiseCipher.GCMP_256};
309         int[] expectedAllowedGroupCiphers = new int[] {
310                 GroupCipher.CCMP, GroupCipher.GCMP_128, GroupCipher.GCMP_256};
311         boolean expectedRequirePmf = true;
312         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
313                                 expectedSecurityType);
314         verifySecurityParams(p, expectedSecurityType,
315                 expectedAllowedKeyManagement, expectedAllowedProtocols,
316                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
317                 expectedAllowedGroupCiphers, expectedRequirePmf);
318     }
319 
320     /** Verify WPA2 Personal EAP params creator. */
321     @Test
testWpaWpa2PersonalCreator()322     public void testWpaWpa2PersonalCreator() throws Exception {
323         int expectedSecurityType = WifiConfiguration.SECURITY_TYPE_PSK;
324         int[] expectedAllowedKeyManagement = new int[] {KeyMgmt.WPA_PSK};
325         int[] expectedAllowedProtocols = new int[] {};
326         int[] expectedAllowedAuthAlgorithms = new int[] {};
327         int[] expectedAllowedPairwiseCiphers = new int[] {};
328         int[] expectedAllowedGroupCiphers = new int[] {};
329         boolean expectedRequirePmf = false;
330         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
331                                 expectedSecurityType);
332         verifySecurityParams(p, expectedSecurityType,
333                 expectedAllowedKeyManagement, expectedAllowedProtocols,
334                 expectedAllowedAuthAlgorithms, expectedAllowedPairwiseCiphers,
335                 expectedAllowedGroupCiphers, expectedRequirePmf);
336     }
337 
338     /** Verify setter/getter methods */
339     @Test
testCommonSetterGetter()340     public void testCommonSetterGetter() throws Exception {
341         SecurityParams params = SecurityParams.createSecurityParamsBySecurityType(
342                 WifiConfiguration.SECURITY_TYPE_PSK);
343 
344         // PSK setting
345         BitSet allowedKeyManagement = new BitSet();
346         allowedKeyManagement.set(KeyMgmt.WPA_PSK);
347 
348         BitSet allowedProtocols = new BitSet();
349         allowedProtocols.set(Protocol.RSN);
350         allowedProtocols.set(Protocol.WPA);
351 
352         BitSet allowedPairwiseCiphers = new BitSet();
353         allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
354         allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
355 
356         BitSet allowedGroupCiphers = new BitSet();
357         allowedGroupCiphers.set(GroupCipher.CCMP);
358         allowedGroupCiphers.set(GroupCipher.TKIP);
359         allowedGroupCiphers.set(GroupCipher.WEP40);
360         allowedGroupCiphers.set(GroupCipher.WEP104);
361 
362         assertEquals(allowedKeyManagement, params.getAllowedKeyManagement());
363         assertTrue(params.getAllowedKeyManagement().get(KeyMgmt.WPA_PSK));
364 
365         assertEquals(allowedProtocols, params.getAllowedProtocols());
366         assertTrue(params.getAllowedProtocols().get(Protocol.RSN));
367         assertTrue(params.getAllowedProtocols().get(Protocol.WPA));
368 
369         assertEquals(allowedPairwiseCiphers, params.getAllowedPairwiseCiphers());
370         assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.CCMP));
371         assertTrue(params.getAllowedPairwiseCiphers().get(PairwiseCipher.TKIP));
372 
373         assertEquals(allowedGroupCiphers, params.getAllowedGroupCiphers());
374         assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.CCMP));
375         assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.TKIP));
376         assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP40));
377         assertTrue(params.getAllowedGroupCiphers().get(GroupCipher.WEP104));
378 
379         params.setEnabled(false);
380         assertFalse(params.isEnabled());
381     }
382 
383     /** Verify SAE-specific methods */
384     @Test
testSaeMethods()385     public void testSaeMethods() throws Exception {
386         SecurityParams p = SecurityParams.createSecurityParamsBySecurityType(
387                 WifiConfiguration.SECURITY_TYPE_SAE);
388 
389         assertFalse(p.isAddedByAutoUpgrade());
390         p.setIsAddedByAutoUpgrade(true);
391         assertTrue(p.isAddedByAutoUpgrade());
392 
393         assertFalse(p.isSaeH2eOnlyMode());
394         p.enableSaeH2eOnlyMode(true);
395         assertTrue(p.isSaeH2eOnlyMode());
396 
397         assertFalse(p.isSaePkOnlyMode());
398         p.enableSaePkOnlyMode(true);
399         assertTrue(p.isSaePkOnlyMode());
400     }
401 
402     /** Verify copy constructor. */
403     @Test
testCopyConstructor()404     public void testCopyConstructor() throws Exception {
405         SecurityParams params = SecurityParams.createSecurityParamsBySecurityType(
406                 WifiConfiguration.SECURITY_TYPE_PSK);
407         params.setEnabled(false);
408         params.setIsAddedByAutoUpgrade(true);
409 
410         SecurityParams copiedParams = new SecurityParams(params);
411 
412         assertTrue(params.isSameSecurityType(copiedParams));
413         assertEquals(params.getAllowedKeyManagement(), copiedParams.getAllowedKeyManagement());
414         assertEquals(params.getAllowedProtocols(), copiedParams.getAllowedProtocols());
415         assertEquals(params.getAllowedAuthAlgorithms(), copiedParams.getAllowedAuthAlgorithms());
416         assertEquals(params.getAllowedPairwiseCiphers(), copiedParams.getAllowedPairwiseCiphers());
417         assertEquals(params.getAllowedGroupCiphers(), copiedParams.getAllowedGroupCiphers());
418         assertEquals(params.getAllowedGroupManagementCiphers(),
419                 copiedParams.getAllowedGroupManagementCiphers());
420         assertEquals(params.getAllowedSuiteBCiphers(), copiedParams.getAllowedSuiteBCiphers());
421         assertEquals(params.isRequirePmf(), copiedParams.isRequirePmf());
422         assertEquals(params.isEnabled(), copiedParams.isEnabled());
423         assertEquals(params.isSaeH2eOnlyMode(), copiedParams.isSaeH2eOnlyMode());
424         assertEquals(params.isSaePkOnlyMode(), copiedParams.isSaePkOnlyMode());
425         assertEquals(params.isAddedByAutoUpgrade(), copiedParams.isAddedByAutoUpgrade());
426     }
427 
428     /** Check that two params are equal if and only if their types are the same. */
429     @Test
testEquals()430     public void testEquals() {
431         SecurityParams saeParams1 = SecurityParams.createSecurityParamsBySecurityType(
432                 WifiConfiguration.SECURITY_TYPE_SAE);
433         SecurityParams saeParams2 = SecurityParams.createSecurityParamsBySecurityType(
434                 WifiConfiguration.SECURITY_TYPE_SAE);
435         SecurityParams pskParams = SecurityParams.createSecurityParamsBySecurityType(
436                 WifiConfiguration.SECURITY_TYPE_PSK);
437         assertEquals(saeParams1, saeParams2);
438         assertNotEquals(saeParams1, pskParams);
439     }
440 
441     /** Check that hash values are the same if and only if their types are the same. */
442     @Test
testHashCode()443     public void testHashCode() {
444         SecurityParams saeParams1 = SecurityParams.createSecurityParamsBySecurityType(
445                 WifiConfiguration.SECURITY_TYPE_SAE);
446         SecurityParams saeParams2 = SecurityParams.createSecurityParamsBySecurityType(
447                 WifiConfiguration.SECURITY_TYPE_SAE);
448         SecurityParams pskParams = SecurityParams.createSecurityParamsBySecurityType(
449                 WifiConfiguration.SECURITY_TYPE_PSK);
450         assertEquals(saeParams1.hashCode(), saeParams2.hashCode());
451         assertNotEquals(saeParams1.hashCode(), pskParams.hashCode());
452     }
453 
454     /** Verify open network check */
455     @Test
testIsOpenNetwork()456     public void testIsOpenNetwork() {
457         SecurityParams[] openSecurityParams = new SecurityParams[] {
458                 SecurityParams.createSecurityParamsBySecurityType(
459                         WifiConfiguration.SECURITY_TYPE_OWE),
460                 SecurityParams.createSecurityParamsBySecurityType(
461                         WifiConfiguration.SECURITY_TYPE_OPEN),
462         };
463         for (SecurityParams p: openSecurityParams) {
464             assertTrue(p.isOpenSecurityType());
465         }
466 
467         SecurityParams[] nonOpenSecurityParams = new SecurityParams[] {
468                 SecurityParams.createSecurityParamsBySecurityType(
469                         WifiConfiguration.SECURITY_TYPE_EAP),
470                 SecurityParams.createSecurityParamsBySecurityType(
471                         WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2),
472                 SecurityParams.createSecurityParamsBySecurityType(
473                         WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3),
474                 SecurityParams.createSecurityParamsBySecurityType(
475                         WifiConfiguration.SECURITY_TYPE_OSEN),
476                 SecurityParams.createSecurityParamsBySecurityType(
477                         WifiConfiguration.SECURITY_TYPE_WAPI_PSK),
478                 SecurityParams.createSecurityParamsBySecurityType(
479                         WifiConfiguration.SECURITY_TYPE_WAPI_CERT),
480                 SecurityParams.createSecurityParamsBySecurityType(
481                         WifiConfiguration.SECURITY_TYPE_WEP),
482                 SecurityParams.createSecurityParamsBySecurityType(
483                         WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT),
484                 SecurityParams.createSecurityParamsBySecurityType(
485                         WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE),
486                 SecurityParams.createSecurityParamsBySecurityType(
487                         WifiConfiguration.SECURITY_TYPE_SAE),
488                 SecurityParams.createSecurityParamsBySecurityType(
489                         WifiConfiguration.SECURITY_TYPE_PSK),
490         };
491         for (SecurityParams p: nonOpenSecurityParams) {
492             assertFalse(p.isOpenSecurityType());
493         }
494     }
495 
496     /** Verify enterprise network check */
497     @Test
testIsEnterpriseNetwork()498     public void testIsEnterpriseNetwork() {
499         SecurityParams[] enterpriseSecurityParams = new SecurityParams[] {
500                 SecurityParams.createSecurityParamsBySecurityType(
501                         WifiConfiguration.SECURITY_TYPE_EAP),
502                 SecurityParams.createSecurityParamsBySecurityType(
503                         WifiConfiguration.SECURITY_TYPE_PASSPOINT_R1_R2),
504                 SecurityParams.createSecurityParamsBySecurityType(
505                         WifiConfiguration.SECURITY_TYPE_PASSPOINT_R3),
506                 SecurityParams.createSecurityParamsBySecurityType(
507                         WifiConfiguration.SECURITY_TYPE_WAPI_CERT),
508                 SecurityParams.createSecurityParamsBySecurityType(
509                         WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE_192_BIT),
510                 SecurityParams.createSecurityParamsBySecurityType(
511                         WifiConfiguration.SECURITY_TYPE_EAP_WPA3_ENTERPRISE),
512         };
513         for (SecurityParams p: enterpriseSecurityParams) {
514             assertTrue(p.isEnterpriseSecurityType());
515         }
516 
517         SecurityParams[] nonEnterpriseSecurityParams = new SecurityParams[] {
518                 SecurityParams.createSecurityParamsBySecurityType(
519                         WifiConfiguration.SECURITY_TYPE_OWE),
520                 SecurityParams.createSecurityParamsBySecurityType(
521                         WifiConfiguration.SECURITY_TYPE_OPEN),
522                 SecurityParams.createSecurityParamsBySecurityType(
523                         WifiConfiguration.SECURITY_TYPE_OSEN),
524                 SecurityParams.createSecurityParamsBySecurityType(
525                         WifiConfiguration.SECURITY_TYPE_WAPI_PSK),
526                 SecurityParams.createSecurityParamsBySecurityType(
527                         WifiConfiguration.SECURITY_TYPE_WEP),
528                 SecurityParams.createSecurityParamsBySecurityType(
529                         WifiConfiguration.SECURITY_TYPE_SAE),
530                 SecurityParams.createSecurityParamsBySecurityType(
531                         WifiConfiguration.SECURITY_TYPE_PSK),
532         };
533         for (SecurityParams p: nonEnterpriseSecurityParams) {
534             assertFalse(p.isEnterpriseSecurityType());
535         }
536     }
537 
538     /** Check that parcel marshalling/unmarshalling works */
539     @Test
testParcelMethods()540     public void testParcelMethods() {
541         SecurityParams params = SecurityParams.createSecurityParamsBySecurityType(
542                 WifiConfiguration.SECURITY_TYPE_SAE);
543 
544         Parcel parcelW = Parcel.obtain();
545         params.writeToParcel(parcelW, 0);
546         byte[] bytes = parcelW.marshall();
547         parcelW.recycle();
548 
549         Parcel parcelR = Parcel.obtain();
550         parcelR.unmarshall(bytes, 0, bytes.length);
551         parcelR.setDataPosition(0);
552 
553         SecurityParams reParams = SecurityParams.createFromParcel(parcelR);
554         assertEquals(params, reParams);
555     }
556 }
557