• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.server.wifi;
17 
18 import static org.junit.Assert.assertArrayEquals;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assume.assumeTrue;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyByte;
27 import static org.mockito.Matchers.eq;
28 import static org.mockito.Mockito.any;
29 import static org.mockito.Mockito.anyString;
30 import static org.mockito.Mockito.doAnswer;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.app.test.MockAnswerUtil.AnswerWithArguments;
36 import android.content.Context;
37 import android.hardware.wifi.supplicant.ProtoMask;
38 import android.hardware.wifi.supplicant.V1_0.ISupplicantNetwork;
39 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetwork;
40 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback;
41 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback.NetworkRequestEapSimGsmAuthParams;
42 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetworkCallback.NetworkRequestEapSimUmtsAuthParams;
43 import android.hardware.wifi.supplicant.V1_0.SupplicantStatus;
44 import android.hardware.wifi.supplicant.V1_0.SupplicantStatusCode;
45 import android.net.wifi.SecurityParams;
46 import android.net.wifi.WifiConfiguration;
47 import android.net.wifi.WifiEnterpriseConfig;
48 import android.net.wifi.WifiManager;
49 import android.os.RemoteException;
50 import android.text.TextUtils;
51 
52 import androidx.test.filters.SmallTest;
53 
54 import com.android.modules.utils.build.SdkLevel;
55 import com.android.server.wifi.util.NativeUtil;
56 import com.android.wifi.resources.R;
57 
58 import org.junit.Before;
59 import org.junit.Test;
60 import org.mockito.Mock;
61 import org.mockito.MockitoAnnotations;
62 
63 import java.util.ArrayList;
64 import java.util.BitSet;
65 import java.util.HashMap;
66 import java.util.Map;
67 import java.util.Random;
68 
69 /**
70  * Unit tests for SupplicantStaNetworkHalHidlImpl
71  */
72 @SmallTest
73 public class SupplicantStaNetworkHalHidlImplTest extends WifiBaseTest {
74     private static final String IFACE_NAME = "wlan0";
75     private static final Map<String, String> NETWORK_EXTRAS_VALUES = new HashMap<>();
76     static {
77         NETWORK_EXTRAS_VALUES.put("key1", "value1");
78         NETWORK_EXTRAS_VALUES.put("key2", "value2");
79     }
80     private static final String NETWORK_EXTRAS_SERIALIZED =
81             "%7B%22key1%22%3A%22value1%22%2C%22key2%22%3A%22value2%22%7D";
82     private static final String ANONYMOUS_IDENTITY = "aaa@bbb.cc.ddd";
83 
84     private SupplicantStaNetworkHalHidlImpl mSupplicantNetwork;
85     private SupplicantStatus mStatusSuccess;
86     private SupplicantStatus mStatusFailure;
87     private android.hardware.wifi.supplicant.V1_4.SupplicantStatus mStatusSuccessV14;
88     private android.hardware.wifi.supplicant.V1_4.SupplicantStatus mStatusFailureV14;
89     @Mock private ISupplicantStaNetwork mISupplicantStaNetworkMock;
90     @Mock
91     private android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork mISupplicantStaNetworkV12;
92     @Mock
93     private android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork mISupplicantStaNetworkV13;
94     @Mock
95     private android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork mISupplicantStaNetworkV14;
96     @Mock private Context mContext;
97     @Mock private WifiMonitor mWifiMonitor;
98     @Mock private WifiGlobals mWifiGlobals;
99     private long mAdvanceKeyMgmtFeatures = 0;
100 
101     private SupplicantNetworkVariables mSupplicantVariables;
102     private MockResources mResources;
103     private ISupplicantStaNetworkCallback mISupplicantStaNetworkCallback;
104     private android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetworkCallback
105             mISupplicantStaNetworkCallbackV14;
106     private static final String TEST_DECORATED_IDENTITY_PREFIX = "androidwifi.dev!";
107 
108     enum SupplicantStaNetworkVersion {
109         V1_0,
110         V1_1,
111         V1_2,
112         V1_3,
113         V1_4,
114     }
115 
116     /**
117      * Spy used to return the V1_2 ISupplicantStaNetwork mock object to simulate the 1.2 HAL running
118      * on the device.
119      */
120     private class SupplicantStaNetworkHalSpyV1_2 extends SupplicantStaNetworkHalHidlImpl {
SupplicantStaNetworkHalSpyV1_2(ISupplicantStaNetwork iSupplicantStaNetwork, String ifaceName, Context context, WifiMonitor monitor, WifiGlobals wifiGlobals, long advanceKeyMgmtFeatures)121         SupplicantStaNetworkHalSpyV1_2(ISupplicantStaNetwork iSupplicantStaNetwork,
122                 String ifaceName,
123                 Context context, WifiMonitor monitor, WifiGlobals wifiGlobals,
124                 long advanceKeyMgmtFeatures) {
125             super(iSupplicantStaNetwork, ifaceName, context, monitor, wifiGlobals,
126                     advanceKeyMgmtFeatures);
127         }
128 
129         @Override
130         protected android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
getSupplicantStaNetworkForV1_2Mockable()131                 getSupplicantStaNetworkForV1_2Mockable() {
132             return mISupplicantStaNetworkV12;
133         }
134     }
135 
136     /**
137      * Spy used to return the V1_3 ISupplicantStaNetwork mock object to simulate the 1.3 HAL running
138      * on the device.
139      */
140     private class SupplicantStaNetworkHalSpyV1_3 extends SupplicantStaNetworkHalSpyV1_2 {
SupplicantStaNetworkHalSpyV1_3(ISupplicantStaNetwork iSupplicantStaNetwork, String ifaceName, Context context, WifiMonitor monitor, WifiGlobals wifiGlobals, long advanceKeyMgmtFeatures)141         SupplicantStaNetworkHalSpyV1_3(ISupplicantStaNetwork iSupplicantStaNetwork,
142                 String ifaceName,
143                 Context context, WifiMonitor monitor, WifiGlobals wifiGlobals,
144                 long advanceKeyMgmtFeatures) {
145             super(iSupplicantStaNetwork, ifaceName, context, monitor, wifiGlobals,
146                     advanceKeyMgmtFeatures);
147         }
148 
149         @Override
150         protected android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
getSupplicantStaNetworkForV1_3Mockable()151                 getSupplicantStaNetworkForV1_3Mockable() {
152             return mISupplicantStaNetworkV13;
153         }
154     }
155 
156     /**
157      * Spy used to return the V1_4 ISupplicantStaNetwork mock object to simulate the 1.4 HAL running
158      * on the device.
159      */
160     private class SupplicantStaNetworkHalSpyV1_4 extends SupplicantStaNetworkHalSpyV1_3 {
SupplicantStaNetworkHalSpyV1_4(ISupplicantStaNetwork iSupplicantStaNetwork, String ifaceName, Context context, WifiMonitor monitor, WifiGlobals wifiGlobals, long advanceKeyMgmtFeatures)161         SupplicantStaNetworkHalSpyV1_4(ISupplicantStaNetwork iSupplicantStaNetwork,
162                 String ifaceName,
163                 Context context, WifiMonitor monitor, WifiGlobals wifiGlobals,
164                 long advanceKeyMgmtFeatures) {
165             super(iSupplicantStaNetwork, ifaceName, context, monitor, wifiGlobals,
166                     advanceKeyMgmtFeatures);
167         }
168 
169         @Override
170         protected android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
getSupplicantStaNetworkForV1_4Mockable()171                 getSupplicantStaNetworkForV1_4Mockable() {
172             return mISupplicantStaNetworkV14;
173         }
174     }
175 
176     @Before
setUp()177     public void setUp() throws Exception {
178         MockitoAnnotations.initMocks(this);
179         mStatusSuccess = createSupplicantStatus(SupplicantStatusCode.SUCCESS);
180         mStatusFailure = createSupplicantStatus(SupplicantStatusCode.FAILURE_UNKNOWN);
181         mStatusSuccessV14 = createSupplicantStatusV1_4(
182                 android.hardware.wifi.supplicant.V1_4.SupplicantStatusCode.SUCCESS);
183         mStatusFailureV14 = createSupplicantStatusV1_4(
184                 android.hardware.wifi.supplicant.V1_4.SupplicantStatusCode.FAILURE_UNKNOWN);
185         mSupplicantVariables = new SupplicantNetworkVariables();
186         setupISupplicantNetworkMock();
187 
188         mResources = new MockResources();
189         when(mContext.getResources()).thenReturn(mResources);
190         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(true);
191         when(mWifiGlobals.isWpaPersonalDeprecated()).thenReturn(false);
192 
193         mAdvanceKeyMgmtFeatures |= WifiManager.WIFI_FEATURE_WPA3_SUITE_B;
194         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_0);
195     }
196 
197     /**
198      * Tests the saving of WifiConfiguration to wpa_supplicant.
199      */
200     @Test
testOweNetworkWifiConfigurationSaveLoad()201     public void testOweNetworkWifiConfigurationSaveLoad() throws Exception {
202         // Now expose the V1.2 ISupplicantStaNetwork
203         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
204 
205         WifiConfiguration config = WifiConfigurationTestUtil.createOweNetwork();
206         config.updateIdentifier = "46";
207         testWifiConfigurationSaveLoad(config);
208     }
209 
210     /**
211      * Tests the saving of WifiConfiguration to wpa_supplicant.
212      */
213     @Test
testOpenNetworkWifiConfigurationSaveLoad()214     public void testOpenNetworkWifiConfigurationSaveLoad() throws Exception {
215         WifiConfiguration config = WifiConfigurationTestUtil.createOpenHiddenNetwork();
216         config.updateIdentifier = "45";
217         testWifiConfigurationSaveLoad(config);
218     }
219 
220     /**
221      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with SAE password.
222      */
223     @Test
testSaePasswordNetworkWifiConfigurationSaveLoad()224     public void testSaePasswordNetworkWifiConfigurationSaveLoad() throws Exception {
225         // Now expose the V1.2 ISupplicantStaNetwork
226         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
227 
228         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
229         testWifiConfigurationSaveLoad(config);
230         verify(mISupplicantStaNetworkV12).setSaePassword(any(String.class));
231         verify(mISupplicantStaNetworkV12, never())
232                 .getSaePassword(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
233                         .getSaePasswordCallback.class));
234         verify(mISupplicantStaNetworkV12, never())
235                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
236         verify(mISupplicantStaNetworkV12, never()).setPsk(any(byte[].class));
237         verify(mISupplicantStaNetworkV12, never())
238                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
239     }
240 
241     /**
242      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with psk passphrase.
243      */
244     @Test
testPskPassphraseNetworkWifiConfigurationSaveLoad()245     public void testPskPassphraseNetworkWifiConfigurationSaveLoad() throws Exception {
246         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
247 
248         // Set the new defaults
249         testWifiConfigurationSaveLoad(config);
250         verify(mISupplicantStaNetworkMock).setPskPassphrase(anyString());
251         verify(mISupplicantStaNetworkMock)
252                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
253         verify(mISupplicantStaNetworkMock, never()).setPsk(any(byte[].class));
254         verify(mISupplicantStaNetworkMock, never())
255                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
256         verify(mISupplicantStaNetworkMock)
257                 .setPairwiseCipher(ISupplicantStaNetwork.PairwiseCipherMask.TKIP
258                         | ISupplicantStaNetwork.PairwiseCipherMask.CCMP);
259         verify(mISupplicantStaNetworkMock)
260                 .setGroupCipher(ISupplicantStaNetwork.GroupCipherMask.WEP40
261                         | ISupplicantStaNetwork.GroupCipherMask.WEP104
262                         | ISupplicantStaNetwork.GroupCipherMask.TKIP
263                         | ISupplicantStaNetwork.GroupCipherMask.CCMP);
264     }
265 
266     /**
267      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with raw psk.
268      */
269     @Test
testPskNetworkWifiConfigurationSaveLoad()270     public void testPskNetworkWifiConfigurationSaveLoad() throws Exception {
271         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
272         config.preSharedKey = "945ef00c463c2a7c2496376b13263d1531366b46377179a4b17b393687450779";
273         testWifiConfigurationSaveLoad(config);
274         verify(mISupplicantStaNetworkMock).setPsk(any(byte[].class));
275         verify(mISupplicantStaNetworkMock)
276                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
277         verify(mISupplicantStaNetworkMock, never()).setPskPassphrase(anyString());
278         verify(mISupplicantStaNetworkMock)
279                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
280         verify(mISupplicantStaNetworkMock).setProto(ProtoMask.RSN | ProtoMask.WPA);
281     }
282 
283     /**
284      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with raw psk.
285      */
286     @Test
testPskNetworkWifiConfigurationSaveLoadWpaDeprecated()287     public void testPskNetworkWifiConfigurationSaveLoadWpaDeprecated() throws Exception {
288         when(mWifiGlobals.isWpaPersonalDeprecated()).thenReturn(true);
289 
290         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
291         config.preSharedKey = "945ef00c463c2a7c2496376b13263d1531366b46377179a4b17b393687450779";
292         testWifiConfigurationSaveLoad(config);
293         verify(mISupplicantStaNetworkMock).setPsk(any(byte[].class));
294         verify(mISupplicantStaNetworkMock)
295                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
296         verify(mISupplicantStaNetworkMock, never()).setPskPassphrase(anyString());
297         verify(mISupplicantStaNetworkMock)
298                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
299         verify(mISupplicantStaNetworkMock).setProto(ProtoMask.RSN);
300     }
301 
302     /**
303      * Tests the saving of WifiConfiguration to wpa_supplicant removes enclosing quotes of psk
304      * passphrase
305      */
306     @Test
testPskNetworkWifiConfigurationSaveRemovesPskQuotes()307     public void testPskNetworkWifiConfigurationSaveRemovesPskQuotes() throws Exception {
308         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
309         config.preSharedKey = "\"quoted_psd\"";
310         // Assume that the default params is used for this test.
311         config.getNetworkSelectionStatus().setCandidateSecurityParams(
312                 config.getDefaultSecurityParams());
313         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
314         assertEquals(mSupplicantVariables.pskPassphrase,
315                 NativeUtil.removeEnclosingQuotes(config.preSharedKey));
316     }
317 
318     /**
319      * Tests the saving/loading of WifiConfiguration to wpa_supplicant.
320      */
321     @Test
testWepNetworkWifiConfigurationSaveLoad()322     public void testWepNetworkWifiConfigurationSaveLoad() throws Exception {
323         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
324         config.BSSID = " *NOT USED* "; // we want the other bssid!
325         config.getNetworkSelectionStatus().setNetworkSelectionBSSID("34:45:19:09:45:66");
326         testWifiConfigurationSaveLoad(config);
327     }
328 
329     /**
330      * Tests the saving of WifiConfiguration to wpa_supplicant.
331      */
332     @Test
testEapPeapGtcNetworkWifiConfigurationSaveLoad()333     public void testEapPeapGtcNetworkWifiConfigurationSaveLoad() throws Exception {
334         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
335         config.enterpriseConfig =
336                 WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
337         testWifiConfigurationSaveLoad(config);
338     }
339 
340     /**
341      * Tests the saving of WifiConfiguration to wpa_supplicant.
342      */
343     @Test
testEapTlsNoneNetworkWifiConfigurationSaveLoad()344     public void testEapTlsNoneNetworkWifiConfigurationSaveLoad() throws Exception {
345         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
346         config.enterpriseConfig =
347                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
348         testWifiConfigurationSaveLoad(config);
349     }
350 
351     /**
352      * Tests the saving of WifiConfiguration to wpa_supplicant.
353      */
354     @Test
testEapTlsNoneClientCertNetworkWifiConfigurationSaveLoad()355     public void testEapTlsNoneClientCertNetworkWifiConfigurationSaveLoad() throws Exception {
356         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
357         config.enterpriseConfig =
358                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
359         config.enterpriseConfig.setClientCertificateAlias("test_alias");
360         testWifiConfigurationSaveLoad(config);
361     }
362 
363     /**
364      * Tests the saving of WifiConfiguration to wpa_supplicant.
365      */
366     @Test
testEapTlsNoneClientCertNetworkWithOcspWifiConfigurationSaveLoad()367     public void testEapTlsNoneClientCertNetworkWithOcspWifiConfigurationSaveLoad()
368             throws Exception {
369         // Now expose the V1.3 ISupplicantStaNetwork
370         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
371 
372         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
373         config.enterpriseConfig =
374                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
375         config.enterpriseConfig.setClientCertificateAlias("test_alias");
376         config.enterpriseConfig.setOcsp(WifiEnterpriseConfig.OCSP_REQUIRE_CERT_STATUS);
377         testWifiConfigurationSaveLoad(config);
378     }
379 
380     /**
381      * Tests the saving of WifiConfiguration to wpa_supplicant.
382      */
383     @Test
testEapTlsAkaNetworkWifiConfigurationSaveLoad()384     public void testEapTlsAkaNetworkWifiConfigurationSaveLoad() throws Exception {
385         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
386         config.enterpriseConfig =
387                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithAkaPhase2();
388         testWifiConfigurationSaveLoad(config);
389     }
390 
391     /**
392      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with Suite-B-192
393      */
394     @Test
testEapSuiteBRsaNetworkWifiConfigurationSaveLoad()395     public void testEapSuiteBRsaNetworkWifiConfigurationSaveLoad() throws Exception {
396         // Now expose the V1.2 ISupplicantStaNetwork
397         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
398 
399         WifiConfiguration config = WifiConfigurationTestUtil.createEapSuiteBNetwork();
400         config.enableSuiteBCiphers(false, true);
401 
402         testWifiConfigurationSaveLoad(config);
403         verify(mISupplicantStaNetworkV12, never()).enableSuiteBEapOpenSslCiphers();
404         verify(mISupplicantStaNetworkV12).enableTlsSuiteBEapPhase1Param(anyBoolean());
405 
406         verify(mISupplicantStaNetworkV12, never()).setSaePassword(any(String.class));
407         verify(mISupplicantStaNetworkV12, never())
408                 .getSaePassword(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
409                         .getSaePasswordCallback.class));
410         verify(mISupplicantStaNetworkV12, never())
411                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
412         verify(mISupplicantStaNetworkV12, never()).setPsk(any(byte[].class));
413         verify(mISupplicantStaNetworkV12, never())
414                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
415     }
416 
417     /**
418      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with Suite-B-192
419      */
420     @Test
testEapSuiteBEcdsaNetworkWifiConfigurationSaveLoad()421     public void testEapSuiteBEcdsaNetworkWifiConfigurationSaveLoad() throws Exception {
422         // Now expose the V1.2 ISupplicantStaNetwork
423         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
424 
425         WifiConfiguration config = WifiConfigurationTestUtil.createEapSuiteBNetwork();
426         config.enableSuiteBCiphers(true, false);
427 
428         testWifiConfigurationSaveLoad(config);
429         verify(mISupplicantStaNetworkV12).enableSuiteBEapOpenSslCiphers();
430         verify(mISupplicantStaNetworkV12, never())
431                 .enableTlsSuiteBEapPhase1Param(any(boolean.class));
432 
433         verify(mISupplicantStaNetworkV12, never()).setSaePassword(any(String.class));
434         verify(mISupplicantStaNetworkV12, never())
435                 .getSaePassword(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
436                         .getSaePasswordCallback.class));
437         verify(mISupplicantStaNetworkV12, never())
438                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
439         verify(mISupplicantStaNetworkV12, never()).setPsk(any(byte[].class));
440         verify(mISupplicantStaNetworkV12, never())
441                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
442     }
443 
444     /**
445      * Tests the saving/loading of WifiConfiguration with FILS AKM
446      * to wpa_supplicant.
447      */
448     @Test
testTLSWifiEnterpriseConfigWithFilsEapErp()449     public void testTLSWifiEnterpriseConfigWithFilsEapErp() throws Exception {
450         // Now expose the V1.3 ISupplicantStaNetwork
451         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
452 
453         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
454         config.enterpriseConfig =
455                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
456         config.enableFils(true, false);
457         config.enterpriseConfig.setFieldValue(WifiEnterpriseConfig.EAP_ERP, "1");
458         testWifiConfigurationSaveLoad(config);
459         // Check the supplicant variables to ensure that we have added the FILS AKM.
460         assertTrue((mSupplicantVariables.keyMgmtMask & android.hardware.wifi.supplicant.V1_3
461                 .ISupplicantStaNetwork.KeyMgmtMask.FILS_SHA256)
462                 == android.hardware.wifi.supplicant.V1_3
463                 .ISupplicantStaNetwork.KeyMgmtMask.FILS_SHA256);
464         verify(mISupplicantStaNetworkV13).setEapErp(eq(true));
465     }
466 
467     /**
468      * Tests the saving of WifiConfiguration to wpa_supplicant.
469      */
470     @Test
testWapiPskNetworkWifiConfigurationSaveLoad()471     public void testWapiPskNetworkWifiConfigurationSaveLoad() throws Exception {
472         // Now expose the V1.3 ISupplicantStaNetwork
473         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
474 
475         WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
476         testWifiConfigurationSaveLoad(config);
477     }
478 
479     /**
480      * Tests the saving of WifiConfiguration to wpa_supplicant.
481      */
482     @Test
testWapiPskHexNetworkWifiConfigurationSaveLoad()483     public void testWapiPskHexNetworkWifiConfigurationSaveLoad() throws Exception {
484         // Now expose the V1.3 ISupplicantStaNetwork
485         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
486 
487         WifiConfiguration config = WifiConfigurationTestUtil.createWapiPskNetwork();
488 
489         config.preSharedKey =
490                 "1234567890abcdef0"
491                 + "1234567890abcdef0";
492         // WAPI should accept a hex bytes whose length is not exact 32.
493         testWifiConfigurationSaveLoad(config);
494     }
495 
496     /**
497      * Tests the saving of WifiConfiguration to wpa_supplicant.
498      */
499     @Test
testWapiCertNetworkWifiConfigurationSaveLoad()500     public void testWapiCertNetworkWifiConfigurationSaveLoad() throws Exception {
501         // Now expose the V1.3 ISupplicantStaNetwork
502         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
503 
504         WifiConfiguration config = WifiConfigurationTestUtil.createWapiCertNetwork();
505         testWifiConfigurationSaveLoad(config);
506     }
507 
508     /**
509      * Tests the failure to save ssid.
510      */
511     @Test
testSsidSaveFailure()512     public void testSsidSaveFailure() throws Exception {
513         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
514 
515         doAnswer(new AnswerWithArguments() {
516             public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
517                 return mStatusFailure;
518             }
519         }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
520 
521         // Assume that the default params is used for this test.
522         config.getNetworkSelectionStatus().setCandidateSecurityParams(
523                 config.getDefaultSecurityParams());
524         assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
525     }
526 
527     /**
528      * Tests the failure to save invalid bssid (less than 6 bytes in the
529      * {@link WifiConfiguration#BSSID} being saved).
530      */
531     @Test
testInvalidBssidSaveFailure()532     public void testInvalidBssidSaveFailure() throws Exception {
533         WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
534         config.getNetworkSelectionStatus().setNetworkSelectionBSSID("45:34:23:12");
535         // Assume that the default params is used for this test.
536         config.getNetworkSelectionStatus().setCandidateSecurityParams(
537                 config.getDefaultSecurityParams());
538         try {
539             assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
540         } catch (IllegalArgumentException e) {
541             return;
542         }
543         assertTrue(false);
544     }
545 
546     /**
547      * Tests the parsing of GSM auth response parameters.
548      */
549     @Test
testSendNetworkEapSimGsmAuthResponseWith2KcSresPair()550     public void testSendNetworkEapSimGsmAuthResponseWith2KcSresPair() throws Exception {
551         final byte[] kc = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
552         final byte[] sres = new byte[]{0x12, 0x10, 0x32, 0x23};
553         // Send 2 kc/sres pair for this request.
554         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc)
555                 + ":" + NativeUtil.hexStringFromByteArray(sres)
556                 + ":" + NativeUtil.hexStringFromByteArray(kc)
557                 + ":" + NativeUtil.hexStringFromByteArray(sres);
558 
559         doAnswer(new AnswerWithArguments() {
560             public SupplicantStatus answer(
561                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
562                     throws RemoteException {
563                 assertEquals(2, params.size());
564                 assertArrayEquals(kc, params.get(0).kc);
565                 assertArrayEquals(sres, params.get(0).sres);
566                 assertArrayEquals(kc, params.get(1).kc);
567                 assertArrayEquals(sres, params.get(1).sres);
568                 return mStatusSuccess;
569             }
570         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
571 
572         assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
573     }
574 
575     /**
576      * Tests the parsing of GSM auth response parameters.
577      */
578     @Test
testSendNetworkEapSimGsmAuthResponseWith3KcSresPair()579     public void testSendNetworkEapSimGsmAuthResponseWith3KcSresPair() throws Exception {
580         final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
581         final byte[] sres1 = new byte[]{0x12, 0x10, 0x32, 0x23};
582         final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
583         final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
584         final byte[] kc3 = new byte[]{0x25, 0x34, 0x12, 0x14, 0x45, 0x10, 0x34, 0x12};
585         final byte[] sres3 = new byte[]{0x42, 0x23, 0x22, 0x23};
586         // Send 3 kc/sres pair for this request.
587         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc1)
588                 + ":" + NativeUtil.hexStringFromByteArray(sres1)
589                 + ":" + NativeUtil.hexStringFromByteArray(kc2)
590                 + ":" + NativeUtil.hexStringFromByteArray(sres2)
591                 + ":" + NativeUtil.hexStringFromByteArray(kc3)
592                 + ":" + NativeUtil.hexStringFromByteArray(sres3);
593 
594         doAnswer(new AnswerWithArguments() {
595             public SupplicantStatus answer(
596                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
597                     throws RemoteException {
598                 assertEquals(3, params.size());
599                 assertArrayEquals(kc1, params.get(0).kc);
600                 assertArrayEquals(sres1, params.get(0).sres);
601                 assertArrayEquals(kc2, params.get(1).kc);
602                 assertArrayEquals(sres2, params.get(1).sres);
603                 assertArrayEquals(kc3, params.get(2).kc);
604                 assertArrayEquals(sres3, params.get(2).sres);
605                 return mStatusSuccess;
606             }
607         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
608 
609         assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
610     }
611 
612     /**
613      * Tests the parsing of invalid GSM auth response parameters (invalid kc & sres lengths).
614      */
615     @Test
testSendInvalidKcSresLenNetworkEapSimGsmAuthResponse()616     public void testSendInvalidKcSresLenNetworkEapSimGsmAuthResponse() throws Exception {
617         final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34};
618         final byte[] sres1 = new byte[]{0x12, 0x10, 0x23};
619         final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
620         final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
621         // Send 2 kc/sres pair for this request.
622         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc1)
623                 + ":" + NativeUtil.hexStringFromByteArray(sres1)
624                 + ":" + NativeUtil.hexStringFromByteArray(kc2)
625                 + ":" + NativeUtil.hexStringFromByteArray(sres2);
626 
627         doAnswer(new AnswerWithArguments() {
628             public SupplicantStatus answer(
629                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
630                     throws RemoteException {
631                 return mStatusSuccess;
632             }
633         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
634 
635         assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
636     }
637 
638     /**
639      * Tests the parsing of invalid GSM auth response parameters (invalid number of kc/sres pairs).
640      */
641     @Test
testSendInvalidKcSresPairNumNetworkEapSimGsmAuthResponse()642     public void testSendInvalidKcSresPairNumNetworkEapSimGsmAuthResponse() throws Exception {
643         final byte[] kc = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
644         final byte[] sres = new byte[]{0x12, 0x23, 0x12, 0x23};
645         // Send 1 kc/sres pair for this request.
646         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(kc)
647                 + ":" + NativeUtil.hexStringFromByteArray(sres);
648 
649         doAnswer(new AnswerWithArguments() {
650             public SupplicantStatus answer(
651                     ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
652                     throws RemoteException {
653                 return mStatusSuccess;
654             }
655         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
656 
657         assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
658     }
659 
660     /**
661      * Tests the parsing of UMTS auth response parameters.
662      */
663     @Test
testSendNetworkEapSimUmtsAuthResponse()664     public void testSendNetworkEapSimUmtsAuthResponse() throws Exception {
665         final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
666                 0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
667         final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
668                 0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
669         final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
670         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(ik)
671                 + ":" + NativeUtil.hexStringFromByteArray(ck)
672                 + ":" + NativeUtil.hexStringFromByteArray(res);
673 
674         doAnswer(new AnswerWithArguments() {
675             public SupplicantStatus answer(
676                     ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
677                     throws RemoteException {
678                 assertArrayEquals(ik, params.ik);
679                 assertArrayEquals(ck, params.ck);
680                 // Convert to arraylist before comparison.
681                 ArrayList<Byte> resList = new ArrayList<>();
682                 for (byte b : res) {
683                     resList.add(b);
684                 }
685                 assertEquals(resList, params.res);
686                 return mStatusSuccess;
687             }
688         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
689                 any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
690 
691         assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
692     }
693 
694     /**
695      * Tests the parsing of invalid UMTS auth response parameters (invalid ik, ck lengths).
696      */
697     @Test
testSendInvalidNetworkEapSimUmtsAuthResponse()698     public void testSendInvalidNetworkEapSimUmtsAuthResponse() throws Exception {
699         final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
700         final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
701                 0x33, 0x23, 0x34, 0x10, 0x40};
702         final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
703         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(ik)
704                 + ":" + NativeUtil.hexStringFromByteArray(ck)
705                 + ":" + NativeUtil.hexStringFromByteArray(res);
706 
707         doAnswer(new AnswerWithArguments() {
708             public SupplicantStatus answer(
709                     ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
710                     throws RemoteException {
711                 return mStatusSuccess;
712             }
713         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
714                 any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
715 
716         assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
717     }
718 
719     /**
720      * Tests the parsing of UMTS auts response parameters.
721      */
722     @Test
testSendNetworkEapSimUmtsAutsResponse()723     public void testSendNetworkEapSimUmtsAutsResponse() throws Exception {
724         final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
725                 0x33, 0x23, 0x34, 0x10};
726         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
727 
728         doAnswer(new AnswerWithArguments() {
729             public SupplicantStatus answer(byte[] params)
730                     throws RemoteException {
731                 assertArrayEquals(auts, params);
732                 return mStatusSuccess;
733             }
734         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
735 
736         assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
737     }
738 
739     /**
740      * Tests the parsing of invalid UMTS auts response parameters (invalid auts length).
741      */
742     @Test
testSendInvalidNetworkEapSimUmtsAutsResponse()743     public void testSendInvalidNetworkEapSimUmtsAutsResponse() throws Exception {
744         final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23};
745         String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
746 
747         doAnswer(new AnswerWithArguments() {
748             public SupplicantStatus answer(byte[] params)
749                     throws RemoteException {
750                 assertArrayEquals(auts, params);
751                 return mStatusSuccess;
752             }
753         }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
754 
755         assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
756     }
757 
758     /**
759      * Tests the parsing of identity string.
760      */
761     @Test
testSendNetworkEapIdentityResponse()762     public void testSendNetworkEapIdentityResponse() throws Exception {
763         final String identityStr = "test@test.com";
764         final String encryptedIdentityStr = "test2@test.com";
765         doAnswer(new AnswerWithArguments() {
766             public SupplicantStatus answer(ArrayList<Byte> identity)
767                     throws RemoteException {
768                 assertEquals(identityStr, NativeUtil.stringFromByteArrayList(identity));
769                 return mStatusSuccess;
770             }
771         }).when(mISupplicantStaNetworkMock).sendNetworkEapIdentityResponse(any(ArrayList.class));
772 
773         assertTrue(mSupplicantNetwork.sendNetworkEapIdentityResponse(identityStr,
774                 encryptedIdentityStr));
775         verify(mISupplicantStaNetworkV12, never()).sendNetworkEapIdentityResponse_1_1(
776                 any(ArrayList.class), any(ArrayList.class));
777 
778         // Now expose the V1.2 ISupplicantStaNetwork
779         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
780         doAnswer(new AnswerWithArguments() {
781             public SupplicantStatus answer(ArrayList<Byte> identity,
782                     ArrayList<Byte> encryptedIdentity)
783                     throws RemoteException {
784                 assertEquals(identityStr, NativeUtil.stringFromByteArrayList(identity));
785                 assertEquals(encryptedIdentityStr,
786                         NativeUtil.stringFromByteArrayList(encryptedIdentity));
787                 return mStatusSuccess;
788             }
789         }).when(mISupplicantStaNetworkV12).sendNetworkEapIdentityResponse_1_1(any(ArrayList.class),
790                 any(ArrayList.class));
791         assertTrue(mSupplicantNetwork.sendNetworkEapIdentityResponse(identityStr,
792                 encryptedIdentityStr));
793     }
794 
795     /**
796      * Tests the addition of FT flags when the device supports it.
797      */
798     @Test
testAddFtPskFlags()799     public void testAddFtPskFlags() throws Exception {
800         mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
801         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_0);
802 
803         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
804         // Assume that the default params is used for this test.
805         config.getNetworkSelectionStatus().setCandidateSecurityParams(
806                 config.getDefaultSecurityParams());
807         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
808 
809         // Check the supplicant variables to ensure that we have added the FT flags.
810         assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_PSK)
811                 == ISupplicantStaNetwork.KeyMgmtMask.FT_PSK);
812 
813         WifiConfiguration loadConfig = new WifiConfiguration();
814         Map<String, String> networkExtras = new HashMap<>();
815         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
816         // The FT flags should be stripped out when reading it back.
817         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
818     }
819 
820     /**
821      * Tests the addition of FT flags when the device supports it.
822      */
823     @Test
testAddFtEapFlags()824     public void testAddFtEapFlags() throws Exception {
825         mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
826         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_0);
827 
828         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
829         // Assume that the default params is used for this test.
830         config.getNetworkSelectionStatus().setCandidateSecurityParams(
831                 config.getDefaultSecurityParams());
832         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
833 
834         // Check the supplicant variables to ensure that we have added the FT flags.
835         assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_EAP)
836                 == ISupplicantStaNetwork.KeyMgmtMask.FT_EAP);
837 
838         WifiConfiguration loadConfig = new WifiConfiguration();
839         Map<String, String> networkExtras = new HashMap<>();
840         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
841         // The FT flags should be stripped out when reading it back.
842         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
843     }
844 
845     /**
846      * Tests the addition of SHA256 flags (WPA_PSK_SHA256)
847      */
848     @Test
testAddPskSha256Flags()849     public void testAddPskSha256Flags() throws Exception {
850         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
851         // Now expose the V1.2 ISupplicantStaNetwork
852         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
853         // Assume that the default params is used for this test.
854         config.getNetworkSelectionStatus().setCandidateSecurityParams(
855                 config.getDefaultSecurityParams());
856         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
857 
858         // Check the supplicant variables to ensure that we have added the SHA256 flags.
859         assertTrue((mSupplicantVariables.keyMgmtMask
860                 & android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask
861                 .WPA_PSK_SHA256) == android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
862                 .KeyMgmtMask.WPA_PSK_SHA256);
863 
864         WifiConfiguration loadConfig = new WifiConfiguration();
865         Map<String, String> networkExtras = new HashMap<>();
866         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
867         // The SHA256 flags should be stripped out when reading it back.
868         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
869     }
870 
871     /**
872      * Tests the addition of SHA256 flags (WPA_EAP_SHA256)
873      */
874     @Test
testAddEapSha256Flags()875     public void testAddEapSha256Flags() throws Exception {
876         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
877         // Now expose the V1.2 ISupplicantStaNetwork
878         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
879         // Assume that the default params is used for this test.
880         config.getNetworkSelectionStatus().setCandidateSecurityParams(
881                 config.getDefaultSecurityParams());
882         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
883 
884         // Check the supplicant variables to ensure that we have added the SHA256 flags.
885         assertTrue((mSupplicantVariables.keyMgmtMask
886                 & android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask
887                 .WPA_EAP_SHA256) == android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
888                 .KeyMgmtMask.WPA_EAP_SHA256);
889 
890         WifiConfiguration loadConfig = new WifiConfiguration();
891         Map<String, String> networkExtras = new HashMap<>();
892         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
893         // The SHA256 flags should be stripped out when reading it back.
894         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
895     }
896 
897     /**
898      * Tests the addition of SHA256 flags (WPA_PSK_SHA256) is ignored on HAL v1.1 or lower
899      */
900     @Test
testAddPskSha256FlagsHal1_1OrLower()901     public void testAddPskSha256FlagsHal1_1OrLower() throws Exception {
902         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
903         // Assume that the default params is used for this test.
904         config.getNetworkSelectionStatus().setCandidateSecurityParams(
905                 config.getDefaultSecurityParams());
906         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
907 
908         // Check the supplicant variables to ensure that we have NOT added the SHA256 flags.
909         assertFalse((mSupplicantVariables.keyMgmtMask
910                 & android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask
911                 .WPA_PSK_SHA256) == android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
912                 .KeyMgmtMask.WPA_PSK_SHA256);
913     }
914 
915     /**
916      * Tests the addition of SHA256 flags (WPA_EAP_SHA256) is ignored on HAL v1.1 or lower
917      */
918     @Test
testAddEapSha256FlagsHal1_1OrLower()919     public void testAddEapSha256FlagsHal1_1OrLower() throws Exception {
920         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
921         // Assume that the default params is used for this test.
922         config.getNetworkSelectionStatus().setCandidateSecurityParams(
923                 config.getDefaultSecurityParams());
924         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
925 
926         // Check the supplicant variables to ensure that we have NOT added the SHA256 flags.
927         assertFalse((mSupplicantVariables.keyMgmtMask
928                 & android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask
929                 .WPA_EAP_SHA256) == android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
930                 .KeyMgmtMask.WPA_EAP_SHA256);
931     }
932 
933     /**
934      * Tests OCSP status is ignored on HAL v1.2 or lower
935      */
936     @Test
testOcspStatusHal1_2OrLower()937     public void testOcspStatusHal1_2OrLower() throws Exception {
938         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
939         config.enterpriseConfig =
940                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
941         config.enterpriseConfig.setClientCertificateAlias("test_alias");
942         config.enterpriseConfig.setOcsp(WifiEnterpriseConfig.OCSP_REQUIRE_CERT_STATUS);
943 
944         // Assume that the default params is used for this test.
945         config.getNetworkSelectionStatus().setCandidateSecurityParams(
946                 config.getDefaultSecurityParams());
947         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
948 
949         // Check the supplicant variables to ensure that we have NOT change the OCSP status.
950         assertEquals(WifiEnterpriseConfig.OCSP_NONE, mSupplicantVariables.ocsp);
951     }
952 
953     /**
954      * Tests the addition of multiple AKM when the device supports it.
955      */
956     @Test
testAddPskSaeAkmWhenAutoUpgradeOffloadIsSupported()957     public void testAddPskSaeAkmWhenAutoUpgradeOffloadIsSupported() throws Exception {
958         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
959 
960         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
961         config.getNetworkSelectionStatus().setCandidateSecurityParams(
962                 config.getDefaultSecurityParams());
963         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
964 
965         // Check the supplicant variables to ensure that we have added the FT flags.
966         assertEquals(ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK,
967                 (mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK));
968         assertEquals(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask.SAE,
969                 (mSupplicantVariables.keyMgmtMask & android.hardware.wifi.supplicant.V1_2
970                 .ISupplicantStaNetwork.KeyMgmtMask.SAE));
971 
972         WifiConfiguration loadConfig = new WifiConfiguration();
973         Map<String, String> networkExtras = new HashMap<>();
974         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
975         // The additional SAE AMK should be stripped out when reading it back.
976         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
977     }
978 
979     /**
980      * Tests the addition of multiple AKM when the device does not support it.
981      */
982     @Test
testAddPskSaeAkmWhenAutoUpgradeOffloadIsNotSupported()983     public void testAddPskSaeAkmWhenAutoUpgradeOffloadIsNotSupported() throws Exception {
984         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(false);
985         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
986 
987         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
988         config.getNetworkSelectionStatus().setCandidateSecurityParams(
989                 config.getDefaultSecurityParams());
990         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
991 
992         // Check the supplicant variables to ensure that we have added the FT flags.
993         assertEquals(ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK,
994                 (mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK));
995         assertEquals(0,
996                 (mSupplicantVariables.keyMgmtMask & android.hardware.wifi.supplicant.V1_2
997                 .ISupplicantStaNetwork.KeyMgmtMask.SAE));
998 
999         WifiConfiguration loadConfig = new WifiConfiguration();
1000         Map<String, String> networkExtras = new HashMap<>();
1001         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
1002         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
1003     }
1004 
1005     /**
1006      * Tests the PMF is disabled when the device supports multiple AKMs.
1007      */
1008     @Test
testPmfDisabledWhenAutoUpgradeOffloadIsSupportedAndSaeSelected()1009     public void testPmfDisabledWhenAutoUpgradeOffloadIsSupportedAndSaeSelected() throws Exception {
1010         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1011         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(true);
1012 
1013         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1014         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1015                 SecurityParams.createSecurityParamsBySecurityType(
1016                         WifiConfiguration.SECURITY_TYPE_SAE));
1017         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1018 
1019         // PSK and SAE is set and PMF is disable when SAE is selected.
1020         assertEquals(ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK,
1021                 (mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK));
1022         assertEquals(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask.SAE,
1023                 (mSupplicantVariables.keyMgmtMask & android.hardware.wifi.supplicant.V1_2
1024                 .ISupplicantStaNetwork.KeyMgmtMask.SAE));
1025         assertEquals(false, mSupplicantVariables.requirePmf);
1026     }
1027 
1028     /**
1029      * Tests the PMF is kept when the device does not support multiple AKMs.
1030      */
1031     @Test
testPmfEnabledWhenAutoUpgradeOffloadNotSupportedAndSaeSelected()1032     public void testPmfEnabledWhenAutoUpgradeOffloadNotSupportedAndSaeSelected() throws Exception {
1033         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1034         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(false);
1035 
1036         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1037         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1038                 SecurityParams.createSecurityParamsBySecurityType(
1039                         WifiConfiguration.SECURITY_TYPE_SAE));
1040         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1041 
1042         // Only SAE is set and PMF is enabled when SAE is selected.
1043         assertEquals(0,
1044                 (mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.WPA_PSK));
1045         assertEquals(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork.KeyMgmtMask.SAE,
1046                 (mSupplicantVariables.keyMgmtMask & android.hardware.wifi.supplicant.V1_2
1047                 .ISupplicantStaNetwork.KeyMgmtMask.SAE));
1048         assertEquals(true, mSupplicantVariables.requirePmf);
1049     }
1050 
1051     /**
1052      * Tests ciphers are merged when the device supports auto upgrade offload feature
1053      * and when candidate security type is PSK.
1054      */
1055     @Test
testCiphersMergedWhenAutoUpgradeOffloadIsSupportedAndPskSelected()1056     public void testCiphersMergedWhenAutoUpgradeOffloadIsSupportedAndPskSelected()
1057             throws Exception {
1058         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1059         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(true);
1060 
1061         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1062         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1063                 SecurityParams.createSecurityParamsBySecurityType(
1064                         WifiConfiguration.SECURITY_TYPE_PSK));
1065         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1066 
1067         assertEquals(ISupplicantStaNetwork.PairwiseCipherMask.CCMP
1068                 | ISupplicantStaNetwork.PairwiseCipherMask.TKIP
1069                 | android.hardware.wifi.supplicant.V1_4
1070                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_128
1071                 | android.hardware.wifi.supplicant.V1_2
1072                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_256,
1073                 mSupplicantVariables.pairwiseCipherMask);
1074         assertEquals(ISupplicantStaNetwork.GroupCipherMask.CCMP
1075                 | ISupplicantStaNetwork.GroupCipherMask.TKIP
1076                 | ISupplicantStaNetwork.GroupCipherMask.WEP40
1077                 | ISupplicantStaNetwork.GroupCipherMask.WEP104
1078                 | android.hardware.wifi.supplicant.V1_4
1079                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_128
1080                 | android.hardware.wifi.supplicant.V1_2
1081                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_256,
1082                 mSupplicantVariables.groupCipherMask);
1083     }
1084 
1085     /**
1086      * Tests ciphers are not changed when the device does not supports auto upgrade offload feature
1087      * and when candidate security type is PSK.
1088      */
1089     @Test
testCiphersNotChangedWhenAutoUpgradeOffloadNotSupportedAndPskSelected()1090     public void testCiphersNotChangedWhenAutoUpgradeOffloadNotSupportedAndPskSelected()
1091             throws Exception {
1092         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1093         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(false);
1094 
1095         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1096         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1097                 SecurityParams.createSecurityParamsBySecurityType(
1098                         WifiConfiguration.SECURITY_TYPE_PSK));
1099         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1100 
1101         assertEquals(ISupplicantStaNetwork.PairwiseCipherMask.CCMP
1102                 | ISupplicantStaNetwork.PairwiseCipherMask.TKIP,
1103                 mSupplicantVariables.pairwiseCipherMask);
1104         assertEquals(ISupplicantStaNetwork.GroupCipherMask.CCMP
1105                 | ISupplicantStaNetwork.GroupCipherMask.TKIP
1106                 | ISupplicantStaNetwork.GroupCipherMask.WEP40
1107                 | ISupplicantStaNetwork.GroupCipherMask.WEP104,
1108                 mSupplicantVariables.groupCipherMask);
1109     }
1110 
1111     /**
1112      * Tests ciphers are merged when the device supports auto upgrade offload feature
1113      * and when candidate security type is SAE.
1114      */
1115     @Test
testCiphersMergedWhenAutoUpgradeOffloadIsSupportedAndSaeSelected()1116     public void testCiphersMergedWhenAutoUpgradeOffloadIsSupportedAndSaeSelected()
1117             throws Exception {
1118         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1119         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(true);
1120 
1121         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1122         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1123                 SecurityParams.createSecurityParamsBySecurityType(
1124                         WifiConfiguration.SECURITY_TYPE_SAE));
1125         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1126 
1127         assertEquals(ISupplicantStaNetwork.PairwiseCipherMask.CCMP
1128                 | ISupplicantStaNetwork.PairwiseCipherMask.TKIP
1129                 | android.hardware.wifi.supplicant.V1_4
1130                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_128
1131                 | android.hardware.wifi.supplicant.V1_2
1132                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_256,
1133                 mSupplicantVariables.pairwiseCipherMask);
1134         assertEquals(ISupplicantStaNetwork.GroupCipherMask.CCMP
1135                 | ISupplicantStaNetwork.GroupCipherMask.TKIP
1136                 | ISupplicantStaNetwork.GroupCipherMask.WEP40
1137                 | ISupplicantStaNetwork.GroupCipherMask.WEP104
1138                 | android.hardware.wifi.supplicant.V1_4
1139                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_128
1140                 | android.hardware.wifi.supplicant.V1_2
1141                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_256,
1142                 mSupplicantVariables.groupCipherMask);
1143     }
1144 
1145     /**
1146      * Tests ciphers are not changed when the device does not supports auto upgrade offload feature
1147      * and when candidate security type is SAE.
1148      */
1149     @Test
testCiphersNotChangedWhenAutoUpgradeOffloadNotSupportedAndSaeSelected()1150     public void testCiphersNotChangedWhenAutoUpgradeOffloadNotSupportedAndSaeSelected()
1151             throws Exception {
1152         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1153         when(mWifiGlobals.isWpa3SaeUpgradeOffloadEnabled()).thenReturn(false);
1154 
1155         WifiConfiguration config = WifiConfigurationTestUtil.createPskSaeNetwork();
1156         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1157                 SecurityParams.createSecurityParamsBySecurityType(
1158                         WifiConfiguration.SECURITY_TYPE_SAE));
1159         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1160 
1161         assertEquals(ISupplicantStaNetwork.PairwiseCipherMask.CCMP
1162                 | android.hardware.wifi.supplicant.V1_4
1163                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_128
1164                 | android.hardware.wifi.supplicant.V1_2
1165                         .ISupplicantStaNetwork.PairwiseCipherMask.GCMP_256,
1166                 mSupplicantVariables.pairwiseCipherMask);
1167         assertEquals(ISupplicantStaNetwork.GroupCipherMask.CCMP
1168                 | android.hardware.wifi.supplicant.V1_4
1169                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_128
1170                 | android.hardware.wifi.supplicant.V1_2
1171                         .ISupplicantStaNetwork.GroupCipherMask.GCMP_256,
1172                 mSupplicantVariables.groupCipherMask);
1173     }
1174 
1175     /**
1176      * Tests the retrieval of WPS NFC token.
1177      */
1178     @Test
testGetWpsNfcConfigurationToken()1179     public void testGetWpsNfcConfigurationToken() throws Exception {
1180         final ArrayList<Byte> token = new ArrayList<>();
1181         token.add(Byte.valueOf((byte) 0x45));
1182         token.add(Byte.valueOf((byte) 0x34));
1183 
1184         doAnswer(new AnswerWithArguments() {
1185             public void answer(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback cb)
1186                     throws RemoteException {
1187                 cb.onValues(mStatusSuccess, token);
1188             }
1189         }).when(mISupplicantStaNetworkMock)
1190                 .getWpsNfcConfigurationToken(
1191                         any(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback.class));
1192 
1193         assertEquals("4534", mSupplicantNetwork.getWpsNfcConfigurationToken());
1194     }
1195 
1196     /**
1197      * Tests that callback registration failure triggers a failure in saving network config.
1198      */
1199     @Test
testSaveFailureDueToCallbackReg()1200     public void testSaveFailureDueToCallbackReg() throws Exception {
1201         when(mISupplicantStaNetworkMock.registerCallback(any(ISupplicantStaNetworkCallback.class)))
1202                 .thenReturn(mStatusFailure);
1203         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
1204         // Assume that the default params is used for this test.
1205         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1206                 config.getDefaultSecurityParams());
1207         assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
1208     }
1209 
1210     /**
1211      * Tests that callback registration failure triggers a failure in saving network config.
1212      */
1213     @Test
testSaveFailureDueToCallbackRegV1_4()1214     public void testSaveFailureDueToCallbackRegV1_4() throws Exception {
1215         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1216         when(mISupplicantStaNetworkV14.registerCallback_1_4(any(
1217                         android.hardware.wifi.supplicant.V1_4
1218                         .ISupplicantStaNetworkCallback.class)))
1219                 .thenReturn(mStatusFailureV14);
1220         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
1221         // Assume that the default params is used for this test.
1222         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1223                 config.getDefaultSecurityParams());
1224         assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
1225     }
1226 
1227     /**
1228      * Tests the network gsm auth callback.
1229      */
1230     @Test
testNetworkEapGsmAuthCallback()1231     public void testNetworkEapGsmAuthCallback() throws Exception {
1232         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
1233         // Assume that the default params is used for this test.
1234         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1235                 config.getDefaultSecurityParams());
1236         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1237         assertNotNull(mISupplicantStaNetworkCallback);
1238 
1239         // Now trigger eap gsm callback and ensure that the event is broadcast via WifiMonitor.
1240         NetworkRequestEapSimGsmAuthParams params = new NetworkRequestEapSimGsmAuthParams();
1241         Random random = new Random();
1242         byte[] rand1 = new byte[16];
1243         byte[] rand2 = new byte[16];
1244         byte[] rand3 = new byte[16];
1245         random.nextBytes(rand1);
1246         random.nextBytes(rand2);
1247         random.nextBytes(rand3);
1248         params.rands.add(rand1);
1249         params.rands.add(rand2);
1250         params.rands.add(rand3);
1251 
1252         String[] expectedRands = {
1253                 NativeUtil.hexStringFromByteArray(rand1), NativeUtil.hexStringFromByteArray(rand2),
1254                 NativeUtil.hexStringFromByteArray(rand3)
1255         };
1256 
1257         mISupplicantStaNetworkCallback.onNetworkEapSimGsmAuthRequest(params);
1258         verify(mWifiMonitor).broadcastNetworkGsmAuthRequestEvent(
1259                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID), eq(expectedRands));
1260     }
1261 
1262     /**
1263      * Tests the network umts auth callback.
1264      */
1265     @Test
testNetworkEapUmtsAuthCallback()1266     public void testNetworkEapUmtsAuthCallback() throws Exception {
1267         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
1268         // Assume that the default params is used for this test.
1269         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1270                 config.getDefaultSecurityParams());
1271         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1272         assertNotNull(mISupplicantStaNetworkCallback);
1273 
1274         // Now trigger eap gsm callback and ensure that the event is broadcast via WifiMonitor.
1275         NetworkRequestEapSimUmtsAuthParams params = new NetworkRequestEapSimUmtsAuthParams();
1276         Random random = new Random();
1277         random.nextBytes(params.autn);
1278         random.nextBytes(params.rand);
1279 
1280         String[] expectedRands = {
1281                 NativeUtil.hexStringFromByteArray(params.rand),
1282                 NativeUtil.hexStringFromByteArray(params.autn)
1283         };
1284 
1285         mISupplicantStaNetworkCallback.onNetworkEapSimUmtsAuthRequest(params);
1286         verify(mWifiMonitor).broadcastNetworkUmtsAuthRequestEvent(
1287                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID), eq(expectedRands));
1288     }
1289 
1290     /**
1291      * Tests the network identity callback.
1292      */
1293     @Test
testNetworkIdentityCallback()1294     public void testNetworkIdentityCallback() throws Exception {
1295         WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
1296         // Assume that the default params is used for this test.
1297         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1298                 config.getDefaultSecurityParams());
1299         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1300         assertNotNull(mISupplicantStaNetworkCallback);
1301 
1302         // Now trigger identity request callback and ensure that the event is broadcast via
1303         // WifiMonitor.
1304         mISupplicantStaNetworkCallback.onNetworkEapIdentityRequest();
1305         verify(mWifiMonitor).broadcastNetworkIdentityRequestEvent(
1306                 eq(IFACE_NAME), eq(config.networkId), eq(config.SSID));
1307     }
1308 
testWifiConfigurationSaveLoad(WifiConfiguration config)1309     private void testWifiConfigurationSaveLoad(WifiConfiguration config) {
1310         if (mSupplicantNetwork.getSupplicantStaNetworkForV1_2Mockable() == null) {
1311             // Clear unsupported settings in HAL v1.0
1312             config.allowedPairwiseCiphers.clear(WifiConfiguration.PairwiseCipher.GCMP_256);
1313             config.allowedGroupCiphers.clear(WifiConfiguration.GroupCipher.GCMP_256);
1314         }
1315         if (mSupplicantNetwork.getSupplicantStaNetworkForV1_3Mockable() == null) {
1316             // Clear unsupported settings in HAL v1.0
1317             config.allowedPairwiseCiphers.clear(WifiConfiguration.PairwiseCipher.SMS4);
1318             config.allowedGroupCiphers.clear(WifiConfiguration.GroupCipher.SMS4);
1319         }
1320         if (mSupplicantNetwork.getSupplicantStaNetworkForV1_4Mockable() == null) {
1321             // Clear unsupported settings in HAL v1.0
1322             config.allowedPairwiseCiphers.clear(WifiConfiguration.PairwiseCipher.GCMP_128);
1323             config.allowedGroupCiphers.clear(WifiConfiguration.GroupCipher.GCMP_128);
1324         }
1325         // Assume that the default params is used for this test.
1326         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1327                 config.getDefaultSecurityParams());
1328         // Save the configuration using the default supplicant network HAL v1.0
1329         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1330         WifiConfiguration loadConfig = new WifiConfiguration();
1331         Map<String, String> networkExtras = new HashMap<>();
1332         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
1333         WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
1334         assertEquals(config.getProfileKey(),
1335                 networkExtras.get(SupplicantStaNetworkHalHidlImpl.ID_STRING_KEY_CONFIG_KEY));
1336         assertEquals(
1337                 config.creatorUid,
1338                 Integer.parseInt(networkExtras.get(
1339                         SupplicantStaNetworkHalHidlImpl.ID_STRING_KEY_CREATOR_UID)));
1340         // There is no getter for this one, so check the supplicant variable.
1341         if (!TextUtils.isEmpty(config.updateIdentifier)) {
1342             assertEquals(Integer.parseInt(config.updateIdentifier),
1343                     mSupplicantVariables.updateIdentifier);
1344         }
1345         // There is no getter for this one, so check the supplicant variable.
1346         String oppKeyCaching =
1347                 config.enterpriseConfig.getFieldValue(WifiEnterpriseConfig.OPP_KEY_CACHING);
1348         if (!TextUtils.isEmpty(oppKeyCaching)) {
1349             assertEquals(
1350                     Integer.parseInt(oppKeyCaching) == 1 ? true : false,
1351                     mSupplicantVariables.eapProactiveKeyCaching);
1352         }
1353         // There is no getter for this one, so check the supplicant variable.
1354         String eapErp =
1355                 config.enterpriseConfig.getFieldValue(WifiEnterpriseConfig.EAP_ERP);
1356         if (!TextUtils.isEmpty(eapErp)) {
1357             assertEquals(
1358                     Integer.parseInt(eapErp) == 1 ? true : false,
1359                     mSupplicantVariables.eapErp);
1360         }
1361     }
1362 
1363     /**
1364      * Verifies that createNetworkExtra() & parseNetworkExtra correctly writes a serialized and
1365      * URL-encoded JSON object.
1366      */
1367     @Test
testNetworkExtra()1368     public void testNetworkExtra() {
1369         assertEquals(NETWORK_EXTRAS_SERIALIZED,
1370                 SupplicantStaNetworkHalHidlImpl.createNetworkExtra(NETWORK_EXTRAS_VALUES));
1371         assertEquals(NETWORK_EXTRAS_VALUES,
1372                 SupplicantStaNetworkHalHidlImpl.parseNetworkExtra(NETWORK_EXTRAS_SERIALIZED));
1373     }
1374 
1375     /**
1376      * Verifies that fetchEapAnonymousIdentity() can get the anonymous identity from supplicant.
1377      */
1378     @Test
testFetchEapAnonymousIdentity()1379     public void testFetchEapAnonymousIdentity() {
1380         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
1381         config.enterpriseConfig.setAnonymousIdentity(ANONYMOUS_IDENTITY);
1382         // Assume that the default params is used for this test.
1383         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1384                 config.getDefaultSecurityParams());
1385         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1386         assertEquals(ANONYMOUS_IDENTITY, mSupplicantNetwork.fetchEapAnonymousIdentity());
1387     }
1388 
1389     /**
1390      * Verifies that setPmkCache can set PMK cache
1391      */
1392     @Test
testSetPmkCache()1393     public void testSetPmkCache() {
1394         // Now expose the V1.3 ISupplicantStaNetwork
1395         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
1396 
1397         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
1398         config.enterpriseConfig =
1399                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
1400         // Assume that the default params is used for this test.
1401         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1402                 config.getDefaultSecurityParams());
1403         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1404 
1405         ArrayList<Byte> serializedData = new ArrayList<>();
1406         assertTrue(mSupplicantNetwork.setPmkCache(serializedData));
1407         assertEquals(serializedData, mSupplicantVariables.serializedPmkCache);
1408     }
1409 
1410     /**
1411      * Tests PMK cache is not set on HAL v1.2 or lower
1412      */
1413     @Test
testSetPmkCacheHal1_2OrLower()1414     public void testSetPmkCacheHal1_2OrLower() throws Exception {
1415         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
1416         config.enterpriseConfig =
1417                 WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
1418         // Assume that the default params is used for this test.
1419         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1420                 config.getDefaultSecurityParams());
1421         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1422 
1423         ArrayList<Byte> serializedData = new ArrayList<>();
1424         assertFalse(mSupplicantNetwork.setPmkCache(serializedData));
1425         assertNull(mSupplicantVariables.serializedPmkCache);
1426     }
1427 
1428     /** Verifies that setSaeH2eMode works on HAL 1.4 or newer */
1429     @Test
testEnableSaeH2eOnlyMode()1430     public void testEnableSaeH2eOnlyMode() throws Exception {
1431         when(mWifiGlobals.isWpa3SaeH2eSupported()).thenReturn(true);
1432         // Now expose the V1.4 ISupplicantStaNetwork
1433         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1434 
1435         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1436         config.enableSaeH2eOnlyMode(true);
1437         // Assume that the default params is used for this test.
1438         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1439                 config.getDefaultSecurityParams());
1440         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1441         verify(mISupplicantStaNetworkV14).setSaeH2eMode(
1442                 eq(android.hardware.wifi.supplicant.V1_4
1443                         .ISupplicantStaNetwork.SaeH2eMode.H2E_MANDATORY));
1444     }
1445 
1446     /** Verifies that setSaeH2eMode works on HAL 1.4 or newer */
1447     @Test
testDisableSaeH2eOnlyMode()1448     public void testDisableSaeH2eOnlyMode() throws Exception {
1449         when(mWifiGlobals.isWpa3SaeH2eSupported()).thenReturn(true);
1450         // Now expose the V1.4 ISupplicantStaNetwork
1451         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1452 
1453         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1454         config.enableSaeH2eOnlyMode(false);
1455         // Assume that the default params is used for this test.
1456         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1457                 config.getDefaultSecurityParams());
1458         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1459         verify(mISupplicantStaNetworkV14).setSaeH2eMode(
1460                 eq(android.hardware.wifi.supplicant.V1_4
1461                         .ISupplicantStaNetwork.SaeH2eMode.H2E_OPTIONAL));
1462     }
1463 
1464     /** Verifies that setSaeH2eMode works on HAL 1.4 or newer */
1465     @Test
testDisableSaeH2eOnlyModeWhenH2eNotSupported()1466     public void testDisableSaeH2eOnlyModeWhenH2eNotSupported() throws Exception {
1467         when(mWifiGlobals.isWpa3SaeH2eSupported()).thenReturn(false);
1468         // Now expose the V1.4 ISupplicantStaNetwork
1469         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1470 
1471         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1472         config.enableSaeH2eOnlyMode(false);
1473         // Assume that the default params is used for this test.
1474         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1475                 config.getDefaultSecurityParams());
1476         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1477         verify(mISupplicantStaNetworkV14).setSaeH2eMode(
1478                 eq(android.hardware.wifi.supplicant.V1_4
1479                         .ISupplicantStaNetwork.SaeH2eMode.DISABLED));
1480     }
1481 
1482     /** Verifies that setSaeH2eMode won't break 1.3 or older HAL. */
1483     @Test
testSaeH2eOnlyModeWithHal1_3OrLower()1484     public void testSaeH2eOnlyModeWithHal1_3OrLower() throws Exception {
1485         when(mWifiGlobals.isWpa3SaeH2eSupported()).thenReturn(true);
1486         // Now expose the V1.3 ISupplicantStaNetwork
1487         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_3);
1488 
1489         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1490         config.enableSaeH2eOnlyMode(true);
1491         // Assume that the default params is used for this test.
1492         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1493                 config.getDefaultSecurityParams());
1494         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1495         verify(mISupplicantStaNetworkV14, never()).setSaeH2eMode(anyByte());
1496     }
1497 
1498     /**
1499      * Tests the saving/loading of WifiConfiguration to wpa_supplicant with psk passphrase for
1500      * HAL v1.2 or higher
1501      */
1502     @Test
testSaeNetworkWifiConfigurationSaveLoad1_4OrHigher()1503     public void testSaeNetworkWifiConfigurationSaveLoad1_4OrHigher() throws Exception {
1504         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1505         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1506 
1507         // Set the new defaults
1508         testWifiConfigurationSaveLoad(config);
1509         verify(mISupplicantStaNetworkV12).setSaePassword(anyString());
1510         verify(mISupplicantStaNetworkV12, never())
1511                 .getSaePassword(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1512                         .getSaePasswordCallback.class));
1513         verify(mISupplicantStaNetworkMock, never()).setPsk(any(byte[].class));
1514         verify(mISupplicantStaNetworkMock, never())
1515                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
1516 
1517         verify(mISupplicantStaNetworkV14)
1518                 .setPairwiseCipher_1_4(ISupplicantStaNetwork.PairwiseCipherMask.CCMP
1519                         | android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
1520                         .PairwiseCipherMask.GCMP_128
1521                         | android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1522                         .PairwiseCipherMask.GCMP_256);
1523         verify(mISupplicantStaNetworkV14)
1524                 .setGroupCipher_1_4(ISupplicantStaNetwork.GroupCipherMask.CCMP
1525                         | android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
1526                         .GroupCipherMask.GCMP_128
1527                         | android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1528                         .GroupCipherMask.GCMP_256);
1529     }
1530 
putAllSupportingPairwiseCiphersAndReturnExpectedHalCiphersValue( WifiConfiguration config, SupplicantStaNetworkVersion version)1531     private int putAllSupportingPairwiseCiphersAndReturnExpectedHalCiphersValue(
1532             WifiConfiguration config,
1533             SupplicantStaNetworkVersion version) {
1534         int halMaskValue = 0;
1535 
1536         // The default security params is used in the test.
1537         BitSet allowedPairwiseCiphers = config.getDefaultSecurityParams()
1538                 .getAllowedPairwiseCiphers();
1539         // These are supported from v1.4
1540         if (allowedPairwiseCiphers.get(WifiConfiguration.PairwiseCipher.GCMP_128)
1541                 && version.ordinal() >= SupplicantStaNetworkVersion.V1_4.ordinal()) {
1542             halMaskValue |= android.hardware.wifi.supplicant
1543                     .V1_4.ISupplicantStaNetwork
1544                     .PairwiseCipherMask.GCMP_128;
1545         }
1546 
1547         // These are supported from v1.3
1548         if (allowedPairwiseCiphers.get(WifiConfiguration.PairwiseCipher.SMS4)
1549                 && version.ordinal() >= SupplicantStaNetworkVersion.V1_3.ordinal()) {
1550             halMaskValue |= android.hardware.wifi.supplicant
1551                     .V1_3.ISupplicantStaNetwork
1552                     .PairwiseCipherMask.SMS4;
1553         }
1554 
1555         // These are supported from v1.2
1556         if (allowedPairwiseCiphers.get(WifiConfiguration.PairwiseCipher.GCMP_256)
1557                 && version.ordinal() >= SupplicantStaNetworkVersion.V1_2.ordinal()) {
1558             halMaskValue |= android.hardware.wifi.supplicant
1559                     .V1_2.ISupplicantStaNetwork
1560                     .PairwiseCipherMask.GCMP_256;
1561         }
1562 
1563         // There are supported from v1.0
1564         if (allowedPairwiseCiphers.get(WifiConfiguration.PairwiseCipher.CCMP)) {
1565             halMaskValue |= ISupplicantStaNetwork.PairwiseCipherMask.CCMP;
1566         }
1567 
1568         return halMaskValue;
1569     }
1570 
putAllSupportingGroupCiphersAndReturnExpectedHalCiphersValue( WifiConfiguration config, SupplicantStaNetworkVersion version)1571     private int putAllSupportingGroupCiphersAndReturnExpectedHalCiphersValue(
1572             WifiConfiguration config,
1573             SupplicantStaNetworkVersion version) {
1574         int halMaskValue = 0;
1575         // The default security params is used in the test.
1576         BitSet allowedGroupCiphers = config.getDefaultSecurityParams().getAllowedGroupCiphers();
1577 
1578         // These are supported from v1.4
1579         if (allowedGroupCiphers.get(WifiConfiguration.GroupCipher.GCMP_128)
1580                 && version.ordinal() >= SupplicantStaNetworkVersion.V1_4.ordinal()) {
1581             halMaskValue |= android.hardware.wifi.supplicant
1582                     .V1_4.ISupplicantStaNetwork
1583                     .GroupCipherMask.GCMP_128;
1584         }
1585 
1586         // These are supported from v1.2
1587         if (allowedGroupCiphers.get(WifiConfiguration.GroupCipher.GCMP_256)
1588                 && version.ordinal() >= SupplicantStaNetworkVersion.V1_2.ordinal()) {
1589             halMaskValue |= android.hardware.wifi.supplicant
1590                     .V1_2.ISupplicantStaNetwork
1591                     .GroupCipherMask.GCMP_256;
1592         }
1593 
1594         // There are supported from v1.0
1595         if (allowedGroupCiphers.get(WifiConfiguration.GroupCipher.CCMP)) {
1596             halMaskValue |= ISupplicantStaNetwork.GroupCipherMask.CCMP;
1597         }
1598 
1599         return halMaskValue;
1600     }
1601 
1602     /**
1603      * Tests the saving/loading of WifiConfiguration with
1604      * unsupporting GCMP-256 ciphers for V1.2 HAL.
1605      *
1606      * GCMP-256 is supported only if WPA3 SUITE-B is supported.
1607      */
1608     @Test
testUnsupportingGcmp256Ciphers1_2OrHigher()1609     public void testUnsupportingGcmp256Ciphers1_2OrHigher()
1610             throws Exception {
1611         mAdvanceKeyMgmtFeatures = 0;
1612         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_2);
1613         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1614         int expectedHalPairwiseCiphers =
1615                 putAllSupportingPairwiseCiphersAndReturnExpectedHalCiphersValue(config,
1616                         SupplicantStaNetworkVersion.V1_2);
1617         expectedHalPairwiseCiphers &= ~android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1618                 .PairwiseCipherMask.GCMP_256;
1619         int expectedHalGroupCiphers =
1620                 putAllSupportingGroupCiphersAndReturnExpectedHalCiphersValue(config,
1621                         SupplicantStaNetworkVersion.V1_2);
1622         expectedHalGroupCiphers &= ~android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1623                 .GroupCipherMask.GCMP_256;
1624 
1625         // Assume that the default params is used for this test.
1626         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1627                 config.getDefaultSecurityParams());
1628         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1629         WifiConfiguration loadConfig = new WifiConfiguration();
1630         Map<String, String> networkExtras = new HashMap<>();
1631         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
1632 
1633         verify(mISupplicantStaNetworkV12).setPairwiseCipher_1_2(expectedHalPairwiseCiphers);
1634         verify(mISupplicantStaNetworkV12).setGroupCipher_1_2(expectedHalGroupCiphers);
1635     }
1636 
testUnsupportingCiphers(SupplicantStaNetworkVersion version)1637     private void testUnsupportingCiphers(SupplicantStaNetworkVersion version) throws Exception {
1638         createSupplicantStaNetwork(version);
1639         WifiConfiguration config = WifiConfigurationTestUtil.createSaeNetwork();
1640         int expectedHalPairwiseCiphers =
1641                 putAllSupportingPairwiseCiphersAndReturnExpectedHalCiphersValue(config, version);
1642         int expectedHalGroupCiphers =
1643                 putAllSupportingGroupCiphersAndReturnExpectedHalCiphersValue(config, version);
1644 
1645         // Assume that the default params is used for this test.
1646         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1647                 config.getDefaultSecurityParams());
1648         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1649         WifiConfiguration loadConfig = new WifiConfiguration();
1650         Map<String, String> networkExtras = new HashMap<>();
1651         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
1652 
1653         switch (version) {
1654             case V1_0:
1655             // No new cipher added in V1.1
1656             case V1_1:
1657                 verify(mISupplicantStaNetworkMock)
1658                         .setPairwiseCipher(expectedHalPairwiseCiphers);
1659                 verify(mISupplicantStaNetworkMock)
1660                         .setGroupCipher(expectedHalGroupCiphers);
1661                 break;
1662             case V1_2:
1663                 verify(mISupplicantStaNetworkV12)
1664                         .setPairwiseCipher_1_2(expectedHalPairwiseCiphers);
1665                 verify(mISupplicantStaNetworkV12)
1666                         .setGroupCipher_1_2(expectedHalGroupCiphers);
1667                 break;
1668             case V1_3:
1669                 verify(mISupplicantStaNetworkV13)
1670                         .setPairwiseCipher_1_3(expectedHalPairwiseCiphers);
1671                 verify(mISupplicantStaNetworkV13)
1672                         .setGroupCipher_1_3(expectedHalGroupCiphers);
1673                 break;
1674             case V1_4:
1675                 verify(mISupplicantStaNetworkV14)
1676                         .setPairwiseCipher_1_4(expectedHalPairwiseCiphers);
1677                 verify(mISupplicantStaNetworkV14)
1678                         .setGroupCipher_1_4(expectedHalGroupCiphers);
1679                 break;
1680         }
1681     }
1682 
1683     /**
1684      * Tests the saving/loading of WifiConfiguration with unsupporting ciphers for V1.2 HAL.
1685      */
1686     @Test
testUnsupportingCiphers1_2()1687     public void testUnsupportingCiphers1_2() throws Exception {
1688         testUnsupportingCiphers(SupplicantStaNetworkVersion.V1_2);
1689     }
1690 
1691     /**
1692      * Tests the saving/loading of WifiConfiguration with unsupporting ciphers for V1.3 HAL.
1693      */
1694     @Test
testUnsupportingCiphers1_3()1695     public void testUnsupportingCiphers1_3() throws Exception {
1696         testUnsupportingCiphers(SupplicantStaNetworkVersion.V1_3);
1697     }
1698 
1699     /**
1700      * Tests the saving/loading of WifiConfiguration with unsupporting ciphers for V1.4 HAL.
1701      */
1702     @Test
testUnsupportingCiphers1_4()1703     public void testUnsupportingCiphers1_4() throws Exception {
1704         testUnsupportingCiphers(SupplicantStaNetworkVersion.V1_4);
1705     }
1706 
1707     /**
1708      * Tests the appending decorated identity prefix to anonymous identity and saving to
1709      * wpa_supplicant.
1710      */
1711     @Test
testEapNetworkSetsDecoratedIdentityPrefix()1712     public void testEapNetworkSetsDecoratedIdentityPrefix() throws Exception {
1713         assumeTrue(SdkLevel.isAtLeastS());
1714         createSupplicantStaNetwork(SupplicantStaNetworkVersion.V1_4);
1715         WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
1716         config.enterpriseConfig.setAnonymousIdentity(ANONYMOUS_IDENTITY);
1717         config.enterpriseConfig.setDecoratedIdentityPrefix(TEST_DECORATED_IDENTITY_PREFIX);
1718         // Assume that the default params is used for this test.
1719         config.getNetworkSelectionStatus().setCandidateSecurityParams(
1720                 config.getDefaultSecurityParams());
1721         assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
1722         WifiConfiguration loadConfig = new WifiConfiguration();
1723         Map<String, String> networkExtras = new HashMap<>();
1724         assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
1725         assertEquals(TEST_DECORATED_IDENTITY_PREFIX
1726                 + config.enterpriseConfig.getAnonymousIdentity(),
1727                 loadConfig.enterpriseConfig.getAnonymousIdentity());
1728         assertEquals(TEST_DECORATED_IDENTITY_PREFIX + ANONYMOUS_IDENTITY,
1729                 mSupplicantNetwork.fetchEapAnonymousIdentity());
1730     }
1731 
1732     /**
1733      * Sets up the HIDL interface mock with all the setters/getter values.
1734      * Note: This only sets up the mock to return success on all methods.
1735      */
setupISupplicantNetworkMock()1736     private void setupISupplicantNetworkMock() throws Exception {
1737         /** SSID */
1738         doAnswer(new AnswerWithArguments() {
1739             public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
1740                 mSupplicantVariables.ssid = ssid;
1741                 return mStatusSuccess;
1742             }
1743         }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
1744         doAnswer(new AnswerWithArguments() {
1745             public void answer(ISupplicantStaNetwork.getSsidCallback cb) throws RemoteException {
1746                 cb.onValues(mStatusSuccess, mSupplicantVariables.ssid);
1747             }
1748         }).when(mISupplicantStaNetworkMock)
1749                 .getSsid(any(ISupplicantStaNetwork.getSsidCallback.class));
1750 
1751         /** Network Id */
1752         doAnswer(new AnswerWithArguments() {
1753             public void answer(ISupplicantNetwork.getIdCallback cb) throws RemoteException {
1754                 cb.onValues(mStatusSuccess, mSupplicantVariables.networkId);
1755             }
1756         }).when(mISupplicantStaNetworkMock).getId(any(ISupplicantNetwork.getIdCallback.class));
1757 
1758         /** BSSID */
1759         doAnswer(new AnswerWithArguments() {
1760             public SupplicantStatus answer(byte[] bssid) throws RemoteException {
1761                 mSupplicantVariables.bssid = bssid;
1762                 return mStatusSuccess;
1763             }
1764         }).when(mISupplicantStaNetworkMock).setBssid(any(byte[].class));
1765         doAnswer(new AnswerWithArguments() {
1766             public void answer(ISupplicantStaNetwork.getBssidCallback cb) throws RemoteException {
1767                 cb.onValues(mStatusSuccess, mSupplicantVariables.bssid);
1768             }
1769         }).when(mISupplicantStaNetworkMock)
1770                 .getBssid(any(ISupplicantStaNetwork.getBssidCallback.class));
1771 
1772         /** Scan SSID (Is Hidden Network?) */
1773         doAnswer(new AnswerWithArguments() {
1774             public SupplicantStatus answer(boolean enable) throws RemoteException {
1775                 mSupplicantVariables.scanSsid = enable;
1776                 return mStatusSuccess;
1777             }
1778         }).when(mISupplicantStaNetworkMock).setScanSsid(any(boolean.class));
1779         doAnswer(new AnswerWithArguments() {
1780             public void answer(ISupplicantStaNetwork.getScanSsidCallback cb)
1781                     throws RemoteException {
1782                 cb.onValues(mStatusSuccess, mSupplicantVariables.scanSsid);
1783             }
1784         }).when(mISupplicantStaNetworkMock)
1785                 .getScanSsid(any(ISupplicantStaNetwork.getScanSsidCallback.class));
1786 
1787         /** Require PMF*/
1788         doAnswer(new AnswerWithArguments() {
1789             public SupplicantStatus answer(boolean enable) throws RemoteException {
1790                 mSupplicantVariables.requirePmf = enable;
1791                 return mStatusSuccess;
1792             }
1793         }).when(mISupplicantStaNetworkMock).setRequirePmf(any(boolean.class));
1794         doAnswer(new AnswerWithArguments() {
1795             public void answer(ISupplicantStaNetwork.getRequirePmfCallback cb)
1796                     throws RemoteException {
1797                 cb.onValues(mStatusSuccess, mSupplicantVariables.requirePmf);
1798             }
1799         }).when(mISupplicantStaNetworkMock)
1800                 .getRequirePmf(any(ISupplicantStaNetwork.getRequirePmfCallback.class));
1801 
1802         /** SAE password */
1803         doAnswer(new AnswerWithArguments() {
1804             public SupplicantStatus answer(String saePassword) throws RemoteException {
1805                 mSupplicantVariables.pskPassphrase = saePassword;
1806                 return mStatusSuccess;
1807             }
1808         }).when(mISupplicantStaNetworkV12).setSaePassword(any(String.class));
1809         doAnswer(new AnswerWithArguments() {
1810             public void answer(ISupplicantStaNetwork.getPskPassphraseCallback cb)
1811                     throws RemoteException {
1812                 cb.onValues(mStatusSuccess, mSupplicantVariables.pskPassphrase);
1813             }
1814         }).when(mISupplicantStaNetworkV12)
1815                 .getSaePassword(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1816                         .getSaePasswordCallback.class));
1817 
1818         /** PSK passphrase */
1819         doAnswer(new AnswerWithArguments() {
1820             public SupplicantStatus answer(String pskPassphrase) throws RemoteException {
1821                 mSupplicantVariables.pskPassphrase = pskPassphrase;
1822                 return mStatusSuccess;
1823             }
1824         }).when(mISupplicantStaNetworkMock).setPskPassphrase(any(String.class));
1825         doAnswer(new AnswerWithArguments() {
1826             public void answer(ISupplicantStaNetwork.getPskPassphraseCallback cb)
1827                     throws RemoteException {
1828                 cb.onValues(mStatusSuccess, mSupplicantVariables.pskPassphrase);
1829             }
1830         }).when(mISupplicantStaNetworkMock)
1831                 .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
1832 
1833         /** PSK */
1834         doAnswer(new AnswerWithArguments() {
1835             public SupplicantStatus answer(byte[] psk) throws RemoteException {
1836                 mSupplicantVariables.psk = psk;
1837                 return mStatusSuccess;
1838             }
1839         }).when(mISupplicantStaNetworkMock).setPsk(any(byte[].class));
1840         doAnswer(new AnswerWithArguments() {
1841             public void answer(ISupplicantStaNetwork.getPskCallback cb)
1842                     throws RemoteException {
1843                 cb.onValues(mStatusSuccess, mSupplicantVariables.psk);
1844             }
1845         }).when(mISupplicantStaNetworkMock)
1846                 .getPsk(any(ISupplicantStaNetwork.getPskCallback.class));
1847 
1848         /** WEP keys **/
1849         doAnswer(new AnswerWithArguments() {
1850             public SupplicantStatus answer(int keyIdx, ArrayList<Byte> key) throws RemoteException {
1851                 mSupplicantVariables.wepKey[keyIdx] = key;
1852                 return mStatusSuccess;
1853             }
1854         }).when(mISupplicantStaNetworkMock).setWepKey(any(int.class), any(ArrayList.class));
1855         doAnswer(new AnswerWithArguments() {
1856             public void answer(int keyIdx, ISupplicantStaNetwork.getWepKeyCallback cb)
1857                     throws RemoteException {
1858                 cb.onValues(mStatusSuccess, mSupplicantVariables.wepKey[keyIdx]);
1859             }
1860         }).when(mISupplicantStaNetworkMock)
1861                 .getWepKey(any(int.class), any(ISupplicantStaNetwork.getWepKeyCallback.class));
1862 
1863         doAnswer(new AnswerWithArguments() {
1864             public SupplicantStatus answer(int keyIdx) throws RemoteException {
1865                 mSupplicantVariables.wepTxKeyIdx = keyIdx;
1866                 return mStatusSuccess;
1867             }
1868         }).when(mISupplicantStaNetworkMock).setWepTxKeyIdx(any(int.class));
1869         doAnswer(new AnswerWithArguments() {
1870             public void answer(ISupplicantStaNetwork.getWepTxKeyIdxCallback cb)
1871                     throws RemoteException {
1872                 cb.onValues(mStatusSuccess, mSupplicantVariables.wepTxKeyIdx);
1873             }
1874         }).when(mISupplicantStaNetworkMock)
1875                 .getWepTxKeyIdx(any(ISupplicantStaNetwork.getWepTxKeyIdxCallback.class));
1876 
1877         /** allowedKeyManagement */
1878         doAnswer(new AnswerWithArguments() {
1879             public SupplicantStatus answer(int mask) throws RemoteException {
1880                 mSupplicantVariables.keyMgmtMask = mask;
1881                 return mStatusSuccess;
1882             }
1883         }).when(mISupplicantStaNetworkMock).setKeyMgmt(any(int.class));
1884         doAnswer(new AnswerWithArguments() {
1885             public void answer(ISupplicantStaNetwork.getKeyMgmtCallback cb) throws RemoteException {
1886                 cb.onValues(mStatusSuccess, mSupplicantVariables.keyMgmtMask);
1887             }
1888         }).when(mISupplicantStaNetworkMock)
1889                 .getKeyMgmt(any(ISupplicantStaNetwork.getKeyMgmtCallback.class));
1890 
1891         /** allowedKeyManagement v1.2 */
1892         doAnswer(new AnswerWithArguments() {
1893             public SupplicantStatus answer(int mask) throws RemoteException {
1894                 mSupplicantVariables.keyMgmtMask = mask;
1895                 return mStatusSuccess;
1896             }
1897         }).when(mISupplicantStaNetworkV12).setKeyMgmt_1_2(any(int.class));
1898         doAnswer(new AnswerWithArguments() {
1899             public void answer(ISupplicantStaNetwork.getKeyMgmtCallback cb) throws RemoteException {
1900                 cb.onValues(mStatusSuccess, mSupplicantVariables.keyMgmtMask);
1901             }
1902         }).when(mISupplicantStaNetworkV12)
1903                 .getKeyMgmt_1_2(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1904                         .getKeyMgmt_1_2Callback.class));
1905 
1906         /** allowedKeyManagement v1.3 */
1907         doAnswer(new AnswerWithArguments() {
1908             public SupplicantStatus answer(int mask) throws RemoteException {
1909                 mSupplicantVariables.keyMgmtMask = mask;
1910                 return mStatusSuccess;
1911             }
1912         }).when(mISupplicantStaNetworkV13).setKeyMgmt_1_3(any(int.class));
1913         doAnswer(new AnswerWithArguments() {
1914             public void answer(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
1915                     .getKeyMgmt_1_3Callback cb) throws RemoteException {
1916                 cb.onValues(mStatusSuccess, mSupplicantVariables.keyMgmtMask);
1917             }
1918         }).when(mISupplicantStaNetworkV13)
1919                 .getKeyMgmt_1_3(any(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
1920                         .getKeyMgmt_1_3Callback.class));
1921 
1922         /** allowedProtocols */
1923         doAnswer(new AnswerWithArguments() {
1924             public SupplicantStatus answer(int mask) throws RemoteException {
1925                 mSupplicantVariables.protoMask = mask;
1926                 return mStatusSuccess;
1927             }
1928         }).when(mISupplicantStaNetworkMock).setProto(any(int.class));
1929         doAnswer(new AnswerWithArguments() {
1930             public void answer(ISupplicantStaNetwork.getProtoCallback cb) throws RemoteException {
1931                 cb.onValues(mStatusSuccess, mSupplicantVariables.protoMask);
1932             }
1933         }).when(mISupplicantStaNetworkMock)
1934                 .getProto(any(ISupplicantStaNetwork.getProtoCallback.class));
1935 
1936         /** allowedProtocols v1.3*/
1937         doAnswer(new AnswerWithArguments() {
1938             public SupplicantStatus answer(int mask) throws RemoteException {
1939                 mSupplicantVariables.protoMask = mask;
1940                 return mStatusSuccess;
1941             }
1942         }).when(mISupplicantStaNetworkV13).setProto_1_3(any(int.class));
1943         doAnswer(new AnswerWithArguments() {
1944             public void answer(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
1945                     .getProto_1_3Callback cb) throws RemoteException {
1946                 cb.onValues(mStatusSuccess, mSupplicantVariables.protoMask);
1947             }
1948         }).when(mISupplicantStaNetworkV13)
1949                 .getProto_1_3(any(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
1950                         .getProto_1_3Callback.class));
1951 
1952         /** allowedAuthAlgorithms */
1953         doAnswer(new AnswerWithArguments() {
1954             public SupplicantStatus answer(int mask) throws RemoteException {
1955                 mSupplicantVariables.authAlgMask = mask;
1956                 return mStatusSuccess;
1957             }
1958         }).when(mISupplicantStaNetworkMock).setAuthAlg(any(int.class));
1959         doAnswer(new AnswerWithArguments() {
1960             public void answer(ISupplicantStaNetwork.getAuthAlgCallback cb) throws RemoteException {
1961                 cb.onValues(mStatusSuccess, mSupplicantVariables.authAlgMask);
1962             }
1963         }).when(mISupplicantStaNetworkMock)
1964                 .getAuthAlg(any(ISupplicantStaNetwork.getAuthAlgCallback.class));
1965 
1966         /** allowedGroupCiphers */
1967         doAnswer(new AnswerWithArguments() {
1968             public SupplicantStatus answer(int mask) throws RemoteException {
1969                 mSupplicantVariables.groupCipherMask = mask;
1970                 return mStatusSuccess;
1971             }
1972         }).when(mISupplicantStaNetworkMock).setGroupCipher(any(int.class));
1973         doAnswer(new AnswerWithArguments() {
1974             public void answer(ISupplicantStaNetwork.getGroupCipherCallback cb)
1975                     throws RemoteException {
1976                 cb.onValues(mStatusSuccess, mSupplicantVariables.groupCipherMask);
1977             }
1978         }).when(mISupplicantStaNetworkMock)
1979                 .getGroupCipher(any(ISupplicantStaNetwork.getGroupCipherCallback.class));
1980 
1981         /** allowedGroupCiphers v1.2*/
1982         doAnswer(new AnswerWithArguments() {
1983             public SupplicantStatus answer(int mask) throws RemoteException {
1984                 mSupplicantVariables.groupCipherMask = mask;
1985                 return mStatusSuccess;
1986             }
1987         }).when(mISupplicantStaNetworkV12).setGroupCipher_1_2(any(int.class));
1988         doAnswer(new AnswerWithArguments() {
1989             public void answer(ISupplicantStaNetwork.getGroupCipherCallback cb)
1990                     throws RemoteException {
1991                 cb.onValues(mStatusSuccess, mSupplicantVariables.groupCipherMask);
1992             }
1993         }).when(mISupplicantStaNetworkV12)
1994                 .getGroupCipher_1_2(any(android.hardware.wifi.supplicant.V1_2.ISupplicantStaNetwork
1995                         .getGroupCipher_1_2Callback.class));
1996 
1997         /** allowedGroupCiphers v1.3*/
1998         doAnswer(new AnswerWithArguments() {
1999             public SupplicantStatus answer(int mask) throws RemoteException {
2000                 mSupplicantVariables.groupCipherMask = mask;
2001                 return mStatusSuccess;
2002             }
2003         }).when(mISupplicantStaNetworkV13).setGroupCipher_1_3(any(int.class));
2004         doAnswer(new AnswerWithArguments() {
2005             public void answer(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
2006                     .getGroupCipher_1_3Callback cb)
2007                     throws RemoteException {
2008                 cb.onValues(mStatusSuccess, mSupplicantVariables.groupCipherMask);
2009             }
2010         }).when(mISupplicantStaNetworkV13)
2011                 .getGroupCipher_1_3(any(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
2012                         .getGroupCipher_1_3Callback.class));
2013 
2014         /** allowedGroupCiphers v1.4 */
2015         doAnswer(new AnswerWithArguments() {
2016             public android.hardware.wifi.supplicant.V1_4.SupplicantStatus
2017                     answer(int mask) throws RemoteException {
2018                 mSupplicantVariables.groupCipherMask = mask;
2019                 return mStatusSuccessV14;
2020             }
2021         }).when(mISupplicantStaNetworkV14).setGroupCipher_1_4(any(int.class));
2022         doAnswer(new AnswerWithArguments() {
2023             public void answer(android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
2024                     .getGroupCipher_1_4Callback cb)
2025                     throws RemoteException {
2026                 cb.onValues(mStatusSuccessV14, mSupplicantVariables.groupCipherMask);
2027             }
2028         }).when(mISupplicantStaNetworkV14)
2029                 .getGroupCipher_1_4(any(android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
2030                         .getGroupCipher_1_4Callback.class));
2031 
2032         /** allowedPairwiseCiphers */
2033         doAnswer(new AnswerWithArguments() {
2034             public SupplicantStatus answer(int mask) throws RemoteException {
2035                 mSupplicantVariables.pairwiseCipherMask = mask;
2036                 return mStatusSuccess;
2037             }
2038         }).when(mISupplicantStaNetworkMock).setPairwiseCipher(any(int.class));
2039         doAnswer(new AnswerWithArguments() {
2040             public void answer(ISupplicantStaNetwork.getPairwiseCipherCallback cb)
2041                     throws RemoteException {
2042                 cb.onValues(mStatusSuccess, mSupplicantVariables.pairwiseCipherMask);
2043             }
2044         }).when(mISupplicantStaNetworkMock)
2045                 .getPairwiseCipher(any(ISupplicantStaNetwork.getPairwiseCipherCallback.class));
2046 
2047         /** allowedPairwiseCiphers v1.2 */
2048         doAnswer(new AnswerWithArguments() {
2049             public SupplicantStatus answer(int mask) throws RemoteException {
2050                 mSupplicantVariables.pairwiseCipherMask = mask;
2051                 return mStatusSuccess;
2052             }
2053         }).when(mISupplicantStaNetworkV12).setPairwiseCipher_1_2(any(int.class));
2054         doAnswer(new AnswerWithArguments() {
2055             public void answer(ISupplicantStaNetwork.getPairwiseCipherCallback cb)
2056                     throws RemoteException {
2057                 cb.onValues(mStatusSuccess, mSupplicantVariables.pairwiseCipherMask);
2058             }
2059         }).when(mISupplicantStaNetworkV12)
2060                 .getPairwiseCipher_1_2(any(android.hardware.wifi.supplicant.V1_2
2061                         .ISupplicantStaNetwork.getPairwiseCipher_1_2Callback.class));
2062 
2063         /** allowedPairwiseCiphers v1.3 */
2064         doAnswer(new AnswerWithArguments() {
2065             public SupplicantStatus answer(int mask) throws RemoteException {
2066                 mSupplicantVariables.pairwiseCipherMask = mask;
2067                 return mStatusSuccess;
2068             }
2069         }).when(mISupplicantStaNetworkV13).setPairwiseCipher_1_3(any(int.class));
2070         doAnswer(new AnswerWithArguments() {
2071             public void answer(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
2072                     .getPairwiseCipher_1_3Callback cb)
2073                     throws RemoteException {
2074                 cb.onValues(mStatusSuccess, mSupplicantVariables.pairwiseCipherMask);
2075             }
2076         }).when(mISupplicantStaNetworkV13)
2077                 .getPairwiseCipher_1_3(any(android.hardware.wifi.supplicant.V1_3
2078                         .ISupplicantStaNetwork.getPairwiseCipher_1_3Callback.class));
2079 
2080         /** allowedPairwiseCiphers v1.4 */
2081         doAnswer(new AnswerWithArguments() {
2082             public android.hardware.wifi.supplicant.V1_4.SupplicantStatus
2083                     answer(int mask) throws RemoteException {
2084                 mSupplicantVariables.pairwiseCipherMask = mask;
2085                 return mStatusSuccessV14;
2086             }
2087         }).when(mISupplicantStaNetworkV14).setPairwiseCipher_1_4(any(int.class));
2088         doAnswer(new AnswerWithArguments() {
2089             public void answer(android.hardware.wifi.supplicant.V1_4.ISupplicantStaNetwork
2090                     .getPairwiseCipher_1_4Callback cb)
2091                     throws RemoteException {
2092                 cb.onValues(mStatusSuccessV14, mSupplicantVariables.pairwiseCipherMask);
2093             }
2094         }).when(mISupplicantStaNetworkV14)
2095                 .getPairwiseCipher_1_4(any(android.hardware.wifi.supplicant.V1_4
2096                         .ISupplicantStaNetwork.getPairwiseCipher_1_4Callback.class));
2097 
2098         /** allowedGroupManagementCiphers v1.2 */
2099         doAnswer(new AnswerWithArguments() {
2100             public SupplicantStatus answer(int mask) throws RemoteException {
2101                 mSupplicantVariables.groupManagementCipherMask = mask;
2102                 return mStatusSuccess;
2103             }
2104         }).when(mISupplicantStaNetworkV12).setGroupMgmtCipher(any(int.class));
2105         doAnswer(new AnswerWithArguments() {
2106             public void answer(android.hardware.wifi.supplicant.V1_2
2107                     .ISupplicantStaNetwork.getGroupMgmtCipherCallback cb)
2108                     throws RemoteException {
2109                 cb.onValues(mStatusSuccess, mSupplicantVariables.groupManagementCipherMask);
2110             }
2111         }).when(mISupplicantStaNetworkV12)
2112                 .getGroupMgmtCipher(any(android.hardware.wifi.supplicant.V1_2
2113                         .ISupplicantStaNetwork.getGroupMgmtCipherCallback.class));
2114 
2115         /** metadata: idstr */
2116         doAnswer(new AnswerWithArguments() {
2117             public SupplicantStatus answer(String idStr) throws RemoteException {
2118                 mSupplicantVariables.idStr = idStr;
2119                 return mStatusSuccess;
2120             }
2121         }).when(mISupplicantStaNetworkMock).setIdStr(any(String.class));
2122         doAnswer(new AnswerWithArguments() {
2123             public void answer(ISupplicantStaNetwork.getIdStrCallback cb) throws RemoteException {
2124                 cb.onValues(mStatusSuccess, mSupplicantVariables.idStr);
2125             }
2126         }).when(mISupplicantStaNetworkMock)
2127                 .getIdStr(any(ISupplicantStaNetwork.getIdStrCallback.class));
2128 
2129         /** UpdateIdentifier */
2130         doAnswer(new AnswerWithArguments() {
2131             public SupplicantStatus answer(int identifier) throws RemoteException {
2132                 mSupplicantVariables.updateIdentifier = identifier;
2133                 return mStatusSuccess;
2134             }
2135         }).when(mISupplicantStaNetworkMock).setUpdateIdentifier(any(int.class));
2136 
2137         /** EAP method */
2138         doAnswer(new AnswerWithArguments() {
2139             public SupplicantStatus answer(int method) throws RemoteException {
2140                 mSupplicantVariables.eapMethod = method;
2141                 return mStatusSuccess;
2142             }
2143         }).when(mISupplicantStaNetworkMock).setEapMethod(any(int.class));
2144         doAnswer(new AnswerWithArguments() {
2145             public void answer(ISupplicantStaNetwork.getEapMethodCallback cb)
2146                     throws RemoteException {
2147                 // When not set, return failure.
2148                 if (mSupplicantVariables.eapMethod == -1) {
2149                     cb.onValues(mStatusFailure, mSupplicantVariables.eapMethod);
2150                 } else {
2151                     cb.onValues(mStatusSuccess, mSupplicantVariables.eapMethod);
2152                 }
2153             }
2154         }).when(mISupplicantStaNetworkMock)
2155                 .getEapMethod(any(ISupplicantStaNetwork.getEapMethodCallback.class));
2156 
2157         /** EAP Phase 2 method */
2158         doAnswer(new AnswerWithArguments() {
2159             public SupplicantStatus answer(int method) throws RemoteException {
2160                 mSupplicantVariables.eapPhase2Method = method;
2161                 return mStatusSuccess;
2162             }
2163         }).when(mISupplicantStaNetworkMock).setEapPhase2Method(any(int.class));
2164         doAnswer(new AnswerWithArguments() {
2165             public void answer(ISupplicantStaNetwork.getEapPhase2MethodCallback cb)
2166                     throws RemoteException {
2167                 // When not set, return failure.
2168                 if (mSupplicantVariables.eapPhase2Method == -1) {
2169                     cb.onValues(mStatusFailure, mSupplicantVariables.eapPhase2Method);
2170                 } else {
2171                     cb.onValues(mStatusSuccess, mSupplicantVariables.eapPhase2Method);
2172                 }
2173             }
2174         }).when(mISupplicantStaNetworkMock)
2175                 .getEapPhase2Method(any(ISupplicantStaNetwork.getEapPhase2MethodCallback.class));
2176 
2177         /** EAP Identity */
2178         doAnswer(new AnswerWithArguments() {
2179             public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
2180                 mSupplicantVariables.eapIdentity = identity;
2181                 return mStatusSuccess;
2182             }
2183         }).when(mISupplicantStaNetworkMock).setEapIdentity(any(ArrayList.class));
2184         doAnswer(new AnswerWithArguments() {
2185             public void answer(ISupplicantStaNetwork.getEapIdentityCallback cb)
2186                     throws RemoteException {
2187                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapIdentity);
2188             }
2189         }).when(mISupplicantStaNetworkMock)
2190                 .getEapIdentity(any(ISupplicantStaNetwork.getEapIdentityCallback.class));
2191 
2192         /** EAP Anonymous Identity */
2193         doAnswer(new AnswerWithArguments() {
2194             public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
2195                 mSupplicantVariables.eapAnonymousIdentity = identity;
2196                 return mStatusSuccess;
2197             }
2198         }).when(mISupplicantStaNetworkMock).setEapAnonymousIdentity(any(ArrayList.class));
2199         doAnswer(new AnswerWithArguments() {
2200             public void answer(ISupplicantStaNetwork.getEapAnonymousIdentityCallback cb)
2201                     throws RemoteException {
2202                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapAnonymousIdentity);
2203             }
2204         }).when(mISupplicantStaNetworkMock)
2205                 .getEapAnonymousIdentity(
2206                         any(ISupplicantStaNetwork.getEapAnonymousIdentityCallback.class));
2207 
2208         /** EAP Password */
2209         doAnswer(new AnswerWithArguments() {
2210             public SupplicantStatus answer(ArrayList<Byte> password) throws RemoteException {
2211                 mSupplicantVariables.eapPassword = password;
2212                 return mStatusSuccess;
2213             }
2214         }).when(mISupplicantStaNetworkMock).setEapPassword(any(ArrayList.class));
2215         doAnswer(new AnswerWithArguments() {
2216             public void answer(ISupplicantStaNetwork.getEapPasswordCallback cb)
2217                     throws RemoteException {
2218                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapPassword);
2219             }
2220         }).when(mISupplicantStaNetworkMock)
2221                 .getEapPassword(any(ISupplicantStaNetwork.getEapPasswordCallback.class));
2222 
2223         /** EAP Client Cert */
2224         doAnswer(new AnswerWithArguments() {
2225             public SupplicantStatus answer(String cert) throws RemoteException {
2226                 mSupplicantVariables.eapClientCert = cert;
2227                 return mStatusSuccess;
2228             }
2229         }).when(mISupplicantStaNetworkMock).setEapClientCert(any(String.class));
2230         doAnswer(new AnswerWithArguments() {
2231             public void answer(ISupplicantStaNetwork.getEapClientCertCallback cb)
2232                     throws RemoteException {
2233                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapClientCert);
2234             }
2235         }).when(mISupplicantStaNetworkMock)
2236                 .getEapClientCert(any(ISupplicantStaNetwork.getEapClientCertCallback.class));
2237 
2238         /** EAP CA Cert */
2239         doAnswer(new AnswerWithArguments() {
2240             public SupplicantStatus answer(String cert) throws RemoteException {
2241                 mSupplicantVariables.eapCACert = cert;
2242                 return mStatusSuccess;
2243             }
2244         }).when(mISupplicantStaNetworkMock).setEapCACert(any(String.class));
2245         doAnswer(new AnswerWithArguments() {
2246             public void answer(ISupplicantStaNetwork.getEapCACertCallback cb)
2247                     throws RemoteException {
2248                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapCACert);
2249             }
2250         }).when(mISupplicantStaNetworkMock)
2251                 .getEapCACert(any(ISupplicantStaNetwork.getEapCACertCallback.class));
2252 
2253         /** EAP Subject Match */
2254         doAnswer(new AnswerWithArguments() {
2255             public SupplicantStatus answer(String match) throws RemoteException {
2256                 mSupplicantVariables.eapSubjectMatch = match;
2257                 return mStatusSuccess;
2258             }
2259         }).when(mISupplicantStaNetworkMock).setEapSubjectMatch(any(String.class));
2260         doAnswer(new AnswerWithArguments() {
2261             public void answer(ISupplicantStaNetwork.getEapSubjectMatchCallback cb)
2262                     throws RemoteException {
2263                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapSubjectMatch);
2264             }
2265         }).when(mISupplicantStaNetworkMock)
2266                 .getEapSubjectMatch(any(ISupplicantStaNetwork.getEapSubjectMatchCallback.class));
2267 
2268         /** EAP Engine */
2269         doAnswer(new AnswerWithArguments() {
2270             public SupplicantStatus answer(boolean enable) throws RemoteException {
2271                 mSupplicantVariables.eapEngine = enable;
2272                 return mStatusSuccess;
2273             }
2274         }).when(mISupplicantStaNetworkMock).setEapEngine(any(boolean.class));
2275         doAnswer(new AnswerWithArguments() {
2276             public void answer(ISupplicantStaNetwork.getEapEngineCallback cb)
2277                     throws RemoteException {
2278                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngine);
2279             }
2280         }).when(mISupplicantStaNetworkMock)
2281                 .getEapEngine(any(ISupplicantStaNetwork.getEapEngineCallback.class));
2282 
2283         /** EAP Engine ID */
2284         doAnswer(new AnswerWithArguments() {
2285             public SupplicantStatus answer(String id) throws RemoteException {
2286                 mSupplicantVariables.eapEngineID = id;
2287                 return mStatusSuccess;
2288             }
2289         }).when(mISupplicantStaNetworkMock).setEapEngineID(any(String.class));
2290         doAnswer(new AnswerWithArguments() {
2291             public void answer(ISupplicantStaNetwork.getEapEngineIDCallback cb)
2292                     throws RemoteException {
2293                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngineID);
2294             }
2295         }).when(mISupplicantStaNetworkMock)
2296                 .getEapEngineID(any(ISupplicantStaNetwork.getEapEngineIDCallback.class));
2297 
2298         /** EAP Private Key */
2299         doAnswer(new AnswerWithArguments() {
2300             public SupplicantStatus answer(String key) throws RemoteException {
2301                 mSupplicantVariables.eapPrivateKeyId = key;
2302                 return mStatusSuccess;
2303             }
2304         }).when(mISupplicantStaNetworkMock).setEapPrivateKeyId(any(String.class));
2305         doAnswer(new AnswerWithArguments() {
2306             public void answer(ISupplicantStaNetwork.getEapPrivateKeyIdCallback cb)
2307                     throws RemoteException {
2308                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapPrivateKeyId);
2309             }
2310         }).when(mISupplicantStaNetworkMock)
2311                 .getEapPrivateKeyId(any(ISupplicantStaNetwork.getEapPrivateKeyIdCallback.class));
2312 
2313         /** EAP Alt Subject Match */
2314         doAnswer(new AnswerWithArguments() {
2315             public SupplicantStatus answer(String match) throws RemoteException {
2316                 mSupplicantVariables.eapAltSubjectMatch = match;
2317                 return mStatusSuccess;
2318             }
2319         }).when(mISupplicantStaNetworkMock).setEapAltSubjectMatch(any(String.class));
2320         doAnswer(new AnswerWithArguments() {
2321             public void answer(ISupplicantStaNetwork.getEapAltSubjectMatchCallback cb)
2322                     throws RemoteException {
2323                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapAltSubjectMatch);
2324             }
2325         }).when(mISupplicantStaNetworkMock)
2326                 .getEapAltSubjectMatch(
2327                         any(ISupplicantStaNetwork.getEapAltSubjectMatchCallback.class));
2328 
2329         /** EAP Domain Suffix Match */
2330         doAnswer(new AnswerWithArguments() {
2331             public SupplicantStatus answer(String match) throws RemoteException {
2332                 mSupplicantVariables.eapDomainSuffixMatch = match;
2333                 return mStatusSuccess;
2334             }
2335         }).when(mISupplicantStaNetworkMock).setEapDomainSuffixMatch(any(String.class));
2336         doAnswer(new AnswerWithArguments() {
2337             public void answer(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback cb)
2338                     throws RemoteException {
2339                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapDomainSuffixMatch);
2340             }
2341         }).when(mISupplicantStaNetworkMock)
2342                 .getEapDomainSuffixMatch(
2343                         any(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback.class));
2344 
2345         /** EAP CA Path*/
2346         doAnswer(new AnswerWithArguments() {
2347             public SupplicantStatus answer(String path) throws RemoteException {
2348                 mSupplicantVariables.eapCAPath = path;
2349                 return mStatusSuccess;
2350             }
2351         }).when(mISupplicantStaNetworkMock).setEapCAPath(any(String.class));
2352         doAnswer(new AnswerWithArguments() {
2353             public void answer(ISupplicantStaNetwork.getEapCAPathCallback cb)
2354                     throws RemoteException {
2355                 cb.onValues(mStatusSuccess, mSupplicantVariables.eapCAPath);
2356             }
2357         }).when(mISupplicantStaNetworkMock)
2358                 .getEapCAPath(any(ISupplicantStaNetwork.getEapCAPathCallback.class));
2359 
2360         /** EAP Proactive Key Caching */
2361         doAnswer(new AnswerWithArguments() {
2362             public SupplicantStatus answer(boolean enable) throws RemoteException {
2363                 mSupplicantVariables.eapProactiveKeyCaching = enable;
2364                 return mStatusSuccess;
2365             }
2366         }).when(mISupplicantStaNetworkMock).setProactiveKeyCaching(any(boolean.class));
2367 
2368         /** Callback registration */
2369         doAnswer(new AnswerWithArguments() {
2370             public SupplicantStatus answer(ISupplicantStaNetworkCallback cb)
2371                     throws RemoteException {
2372                 mISupplicantStaNetworkCallback = cb;
2373                 return mStatusSuccess;
2374             }
2375         }).when(mISupplicantStaNetworkMock)
2376                 .registerCallback(any(ISupplicantStaNetworkCallback.class));
2377 
2378         /** Callback registration */
2379         doAnswer(new AnswerWithArguments() {
2380             public android.hardware.wifi.supplicant.V1_4.SupplicantStatus answer(
2381                     android.hardware.wifi.supplicant.V1_4
2382                     .ISupplicantStaNetworkCallback cb)
2383                     throws RemoteException {
2384                 mISupplicantStaNetworkCallbackV14 = cb;
2385                 return mStatusSuccessV14;
2386             }
2387         }).when(mISupplicantStaNetworkV14)
2388                 .registerCallback_1_4(any(
2389                             android.hardware.wifi.supplicant.V1_4
2390                             .ISupplicantStaNetworkCallback.class));
2391 
2392         /** Suite-B*/
2393         doAnswer(new AnswerWithArguments() {
2394             public SupplicantStatus answer(boolean enable) throws RemoteException {
2395                 return mStatusSuccess;
2396             }
2397         }).when(mISupplicantStaNetworkV12).enableTlsSuiteBEapPhase1Param(any(boolean.class));
2398 
2399         doAnswer(new AnswerWithArguments() {
2400             public SupplicantStatus answer() throws RemoteException {
2401                 return mStatusSuccess;
2402             }
2403         }).when(mISupplicantStaNetworkV12).enableSuiteBEapOpenSslCiphers();
2404 
2405         /** OCSP */
2406         doAnswer(new AnswerWithArguments() {
2407             public SupplicantStatus answer(int ocsp) throws RemoteException {
2408                 mSupplicantVariables.ocsp = ocsp;
2409                 return mStatusSuccess;
2410             }
2411         }).when(mISupplicantStaNetworkV13).setOcsp(any(int.class));
2412         doAnswer(new AnswerWithArguments() {
2413             public void answer(
2414                     android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork.getOcspCallback cb)
2415                     throws RemoteException {
2416                 cb.onValues(mStatusSuccess, mSupplicantVariables.ocsp);
2417             }
2418         }).when(mISupplicantStaNetworkV13)
2419                 .getOcsp(any(android.hardware.wifi.supplicant.V1_3.ISupplicantStaNetwork
2420                         .getOcspCallback.class));
2421 
2422         /** PMK cache */
2423         doAnswer(new AnswerWithArguments() {
2424             public SupplicantStatus answer(ArrayList<Byte> serializedData) throws RemoteException {
2425                 mSupplicantVariables.serializedPmkCache = serializedData;
2426                 return mStatusSuccess;
2427             }
2428         }).when(mISupplicantStaNetworkV13).setPmkCache(any(ArrayList.class));
2429 
2430         /** WAPI Cert */
2431         doAnswer(new AnswerWithArguments() {
2432             public SupplicantStatus answer(String cert) throws RemoteException {
2433                 mSupplicantVariables.wapiCertSuite = cert;
2434                 return mStatusSuccess;
2435             }
2436         }).when(mISupplicantStaNetworkV13).setWapiCertSuite(any(String.class));
2437         doAnswer(new AnswerWithArguments() {
2438             public void answer(android.hardware.wifi.supplicant.V1_3
2439                     .ISupplicantStaNetwork.getWapiCertSuiteCallback cb)
2440                     throws RemoteException {
2441                 cb.onValues(mStatusSuccess, mSupplicantVariables.wapiCertSuite);
2442             }
2443         }).when(mISupplicantStaNetworkV13)
2444                 .getWapiCertSuite(any(android.hardware.wifi.supplicant.V1_3
2445                         .ISupplicantStaNetwork.getWapiCertSuiteCallback.class));
2446 
2447         /** EAP ERP */
2448         doAnswer(new AnswerWithArguments() {
2449             public SupplicantStatus answer(boolean enable) throws RemoteException {
2450                 mSupplicantVariables.eapErp = enable;
2451                 return mStatusSuccess;
2452             }
2453         }).when(mISupplicantStaNetworkV13).setEapErp(any(boolean.class));
2454 
2455         /** setSaeH2eMode */
2456         doAnswer(new AnswerWithArguments() {
2457             public android.hardware.wifi.supplicant.V1_4.SupplicantStatus
2458                     answer(byte mode) throws RemoteException {
2459                 mSupplicantVariables.saeH2eMode = mode;
2460                 return mStatusSuccessV14;
2461             }
2462         }).when(mISupplicantStaNetworkV14).setSaeH2eMode(any(byte.class));
2463     }
2464 
createSupplicantStatus(int code)2465     private SupplicantStatus createSupplicantStatus(int code) {
2466         SupplicantStatus status = new SupplicantStatus();
2467         status.code = code;
2468         return status;
2469     }
2470 
2471     private android.hardware.wifi.supplicant.V1_4.SupplicantStatus
createSupplicantStatusV1_4(int code)2472             createSupplicantStatusV1_4(int code) {
2473         android.hardware.wifi.supplicant.V1_4.SupplicantStatus status =
2474                 new android.hardware.wifi.supplicant.V1_4.SupplicantStatus();
2475         status.code = code;
2476         return status;
2477     }
2478 
2479     /**
2480      * Need this for tests which wants to manipulate context before creating the instance.
2481      */
createSupplicantStaNetwork(SupplicantStaNetworkVersion version)2482     private void createSupplicantStaNetwork(SupplicantStaNetworkVersion version) {
2483         switch (version) {
2484             case V1_0:
2485                 mSupplicantNetwork = new SupplicantStaNetworkHalHidlImpl(
2486                         mISupplicantStaNetworkMock, IFACE_NAME, mContext, mWifiMonitor,
2487                         mWifiGlobals, mAdvanceKeyMgmtFeatures);
2488                 break;
2489             case V1_2:
2490                 mSupplicantNetwork = new SupplicantStaNetworkHalSpyV1_2(
2491                         mISupplicantStaNetworkMock, IFACE_NAME, mContext, mWifiMonitor,
2492                         mWifiGlobals, mAdvanceKeyMgmtFeatures);
2493                 break;
2494             case V1_3:
2495                 mSupplicantNetwork = new SupplicantStaNetworkHalSpyV1_3(
2496                         mISupplicantStaNetworkMock, IFACE_NAME, mContext, mWifiMonitor,
2497                         mWifiGlobals, mAdvanceKeyMgmtFeatures);
2498                 break;
2499             case V1_4:
2500                 mSupplicantNetwork = new SupplicantStaNetworkHalSpyV1_4(
2501                         mISupplicantStaNetworkMock, IFACE_NAME, mContext, mWifiMonitor,
2502                         mWifiGlobals, mAdvanceKeyMgmtFeatures);
2503                 break;
2504         }
2505         mSupplicantNetwork.enableVerboseLogging(true, false);
2506     }
2507 
2508     // Private class to to store/inspect values set via the HIDL mock.
2509     private class SupplicantNetworkVariables {
2510         public ArrayList<Byte> ssid;
2511         public int networkId;
2512         public byte[/* 6 */] bssid;
2513         public int keyMgmtMask;
2514         public int protoMask;
2515         public int authAlgMask;
2516         public int groupCipherMask;
2517         public int pairwiseCipherMask;
2518         public int groupManagementCipherMask;
2519         public boolean scanSsid;
2520         public boolean requirePmf;
2521         public String idStr;
2522         public int updateIdentifier;
2523         public String pskPassphrase;
2524         public byte[] psk;
2525         public ArrayList<Byte>[] wepKey = new ArrayList[4];
2526         public int wepTxKeyIdx;
2527         public int eapMethod = -1;
2528         public int eapPhase2Method = -1;
2529         public ArrayList<Byte> eapIdentity;
2530         public ArrayList<Byte> eapAnonymousIdentity;
2531         public ArrayList<Byte> eapPassword;
2532         public String eapCACert;
2533         public String eapCAPath;
2534         public String eapClientCert;
2535         public String eapPrivateKeyId;
2536         public String eapSubjectMatch;
2537         public String eapAltSubjectMatch;
2538         public boolean eapEngine;
2539         public String eapEngineID;
2540         public String eapDomainSuffixMatch;
2541         public boolean eapProactiveKeyCaching;
2542         public int ocsp;
2543         public ArrayList<Byte> serializedPmkCache;
2544         public String wapiCertSuite;
2545         public boolean eapErp;
2546         public byte saeH2eMode;
2547     }
2548 }
2549