SupplicantStaNetworkHalTest.java revision cb9565f0cb8fa92346549bcacdfbf91cdf8e6bd3
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 */
16package com.android.server.wifi;
17
18import static org.junit.Assert.*;
19import static org.mockito.Mockito.any;
20import static org.mockito.Mockito.doAnswer;
21import static org.mockito.Mockito.when;
22
23import android.app.test.MockAnswerUtil.AnswerWithArguments;
24import android.content.Context;
25import android.hardware.wifi.supplicant.V1_0.ISupplicantNetwork;
26import android.hardware.wifi.supplicant.V1_0.ISupplicantStaNetwork;
27import android.hardware.wifi.supplicant.V1_0.SupplicantStatus;
28import android.hardware.wifi.supplicant.V1_0.SupplicantStatusCode;
29import android.net.wifi.WifiConfiguration;
30import android.net.wifi.WifiEnterpriseConfig;
31import android.os.RemoteException;
32import android.text.TextUtils;
33
34import com.android.internal.R;
35import com.android.server.wifi.util.NativeUtil;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41
42import java.util.ArrayList;
43import java.util.HashMap;
44import java.util.Map;
45
46/**
47 * Unit tests for SupplicantStaNetworkHal
48 */
49public class SupplicantStaNetworkHalTest {
50    private static final String TAG = "SupplicantStaNetworkHalTest";
51
52    private SupplicantStaNetworkHal mSupplicantNetwork;
53    private SupplicantStatus mStatusSuccess;
54    private SupplicantStatus mStatusFailure;
55    @Mock private ISupplicantStaNetwork mISupplicantStaNetworkMock;
56    @Mock private Context mContext;
57    @Mock private WifiMonitor mWifiMonitor;
58    private SupplicantNetworkVariables mSupplicantVariables;
59    private MockResources mResources;
60
61    @Before
62    public void setUp() throws Exception {
63        MockitoAnnotations.initMocks(this);
64        mStatusSuccess = createSupplicantStatus(SupplicantStatusCode.SUCCESS);
65        mStatusFailure = createSupplicantStatus(SupplicantStatusCode.FAILURE_UNKNOWN);
66        mSupplicantVariables = new SupplicantNetworkVariables();
67        setupISupplicantNetworkMock();
68
69        mResources = new MockResources();
70        when(mContext.getResources()).thenReturn(mResources);
71        createSupplicantStaNetwork();
72    }
73
74    /**
75     * Tests the saving of WifiConfiguration to wpa_supplicant.
76     */
77    @Test
78    public void testOpenNetworkWifiConfigurationSaveLoad() throws Exception {
79        WifiConfiguration config = WifiConfigurationTestUtil.createOpenHiddenNetwork();
80        config.updateIdentifier = "45";
81        testWifiConfigurationSaveLoad(config);
82    }
83
84    /**
85     * Tests the saving/loading of WifiConfiguration to wpa_supplicant.
86     */
87    @Test
88    public void testPskNetworkWifiConfigurationSaveLoad() throws Exception {
89        WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
90        config.requirePMF = true;
91        testWifiConfigurationSaveLoad(config);
92    }
93
94    /**
95     * Tests the saving/loading of WifiConfiguration to wpa_supplicant.
96     */
97    @Test
98    public void testWepNetworkWifiConfigurationSaveLoad() throws Exception {
99        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
100        config.BSSID = "34:45:19:09:45";
101        testWifiConfigurationSaveLoad(config);
102    }
103
104    /**
105     * Tests the saving of WifiConfiguration to wpa_supplicant.
106     */
107    @Test
108    public void testEapPeapGtcNetworkWifiConfigurationSaveLoad() throws Exception {
109        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
110        config.enterpriseConfig =
111                WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
112        testWifiConfigurationSaveLoad(config);
113    }
114
115    /**
116     * Tests the saving of WifiConfiguration to wpa_supplicant.
117     */
118    @Test
119    public void testEapTlsNoneNetworkWifiConfigurationSaveLoad() throws Exception {
120        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
121        config.enterpriseConfig =
122                WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
123        testWifiConfigurationSaveLoad(config);
124    }
125
126    /**
127     * Tests the saving of WifiConfiguration to wpa_supplicant.
128     */
129    @Test
130    public void testEapTlsNoneClientCertNetworkWifiConfigurationSaveLoad() throws Exception {
131        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
132        config.enterpriseConfig =
133                WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithNonePhase2();
134        config.enterpriseConfig.setClientCertificateAlias("test_alias");
135        testWifiConfigurationSaveLoad(config);
136    }
137
138    /**
139     * Tests the saving of WifiConfiguration to wpa_supplicant.
140     */
141    @Test
142    public void testEapTlsAkaNetworkWifiConfigurationSaveLoad() throws Exception {
143        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
144        config.enterpriseConfig =
145                WifiConfigurationTestUtil.createTLSWifiEnterpriseConfigWithAkaPhase2();
146        testWifiConfigurationSaveLoad(config);
147    }
148
149    /**
150     * Tests the loading of network ID.
151     */
152    @Test
153    public void testNetworkIdLoad() throws Exception {
154        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
155        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
156
157        // Modify the supplicant variable directly.
158        mSupplicantVariables.networkId = 5;
159        WifiConfiguration loadConfig = new WifiConfiguration();
160        Map<String, String> networkExtras = new HashMap<>();
161        assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
162        assertEquals(mSupplicantVariables.networkId, loadConfig.networkId);
163    }
164
165    /**
166     * Tests the failure to load ssid aborts the loading of network variables.
167     */
168    @Test
169    public void testSsidLoadFailure() throws Exception {
170        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
171        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
172
173        doAnswer(new AnswerWithArguments() {
174            public void answer(ISupplicantStaNetwork.getSsidCallback cb) throws RemoteException {
175                cb.onValues(mStatusFailure, mSupplicantVariables.ssid);
176            }
177        }).when(mISupplicantStaNetworkMock)
178                .getSsid(any(ISupplicantStaNetwork.getSsidCallback.class));
179
180        WifiConfiguration loadConfig = new WifiConfiguration();
181        Map<String, String> networkExtras = new HashMap<>();
182        assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
183    }
184
185    /**
186     * Tests the failure to save ssid.
187     */
188    @Test
189    public void testSsidSaveFailure() throws Exception {
190        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
191
192        doAnswer(new AnswerWithArguments() {
193            public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
194                return mStatusFailure;
195            }
196        }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
197
198        assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
199    }
200
201    /**
202     * Tests the failure to save invalid key mgmt (unknown bit set in the
203     * {@link WifiConfiguration#allowedKeyManagement} being saved).
204     */
205    @Test
206    public void testInvalidKeyMgmtSaveFailure() throws Exception {
207        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
208        config.allowedKeyManagement.set(10);
209        try {
210            assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
211        } catch (IllegalArgumentException e) {
212            return;
213        }
214        assertTrue(false);
215    }
216
217    /**
218     * Tests the failure to load invalid key mgmt (unkown bit set in the supplicant network key_mgmt
219     * variable read).
220     */
221    @Test
222    public void testInvalidKeyMgmtLoadFailure() throws Exception {
223        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
224        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
225
226        // Modify the supplicant variable directly.
227        mSupplicantVariables.keyMgmtMask = 0xFFFFF;
228        WifiConfiguration loadConfig = new WifiConfiguration();
229        Map<String, String> networkExtras = new HashMap<>();
230        try {
231            assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
232        } catch (IllegalArgumentException e) {
233            return;
234        }
235        assertTrue(false);
236    }
237
238    /**
239     * Tests the failure to save invalid bssid (less than 6 bytes in the
240     * {@link WifiConfiguration#BSSID} being saved).
241     */
242    @Test
243    public void testInvalidBssidSaveFailure() throws Exception {
244        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
245        config.getNetworkSelectionStatus().setNetworkSelectionBSSID("45:34:23:12");
246        try {
247            assertFalse(mSupplicantNetwork.saveWifiConfiguration(config));
248        } catch (IllegalArgumentException e) {
249            return;
250        }
251        assertTrue(false);
252    }
253
254    /**
255     * Tests the failure to load invalid bssid (less than 6 bytes in the supplicant bssid variable
256     * read).
257     */
258    @Test
259    public void testInvalidBssidLoadFailure() throws Exception {
260        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
261        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
262
263        // Modify the supplicant variable directly.
264        mSupplicantVariables.bssid = new byte[3];
265        WifiConfiguration loadConfig = new WifiConfiguration();
266        Map<String, String> networkExtras = new HashMap<>();
267        try {
268            assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
269        } catch (IllegalArgumentException e) {
270            return;
271        }
272        assertTrue(false);
273    }
274
275    /**
276     * Tests the loading of invalid ssid from wpa_supplicant.
277     */
278    @Test
279    public void testInvalidSsidLoadFailure() throws Exception {
280        WifiConfiguration config = WifiConfigurationTestUtil.createWepHiddenNetwork();
281        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
282
283        // Modify the supplicant variable directly.
284        mSupplicantVariables.ssid = null;
285
286        WifiConfiguration loadConfig = new WifiConfiguration();
287        Map<String, String> networkExtras = new HashMap<>();
288        assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
289    }
290
291    /**
292     * Tests the loading of invalid eap method from wpa_supplicant.
293     * Invalid eap method is assumed to be a non enterprise network. So, the loading should
294     * succeed as a non-enterprise network.
295     */
296    @Test
297    public void testInvalidEapMethodLoadFailure() throws Exception {
298        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
299        config.enterpriseConfig =
300                WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
301        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
302
303        // Modify the supplicant variable directly.
304        mSupplicantVariables.eapMethod = -1;
305
306        WifiConfiguration loadConfig = new WifiConfiguration();
307        Map<String, String> networkExtras = new HashMap<>();
308        assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
309    }
310
311    /**
312     * Tests the loading of invalid eap phase2 method from wpa_supplicant.
313     */
314    @Test
315    public void testInvalidEapPhase2MethodLoadFailure() throws Exception {
316        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
317        config.enterpriseConfig =
318                WifiConfigurationTestUtil.createPEAPWifiEnterpriseConfigWithGTCPhase2();
319        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
320
321        // Modify the supplicant variable directly.
322        mSupplicantVariables.eapPhase2Method = -1;
323
324        WifiConfiguration loadConfig = new WifiConfiguration();
325        Map<String, String> networkExtras = new HashMap<>();
326        assertFalse(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
327    }
328
329    /**
330     * Tests the parsing of GSM auth response parameters.
331     */
332    @Test
333    public void testSendNetworkEapSimGsmAuthResponseWith2KcSresPair() throws Exception {
334        final byte[] kc = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
335        final byte[] sres = new byte[]{0x12, 0x10, 0x32, 0x23};
336        // Send 2 kc/sres pair for this request.
337        String paramsStr = ":kc:" + NativeUtil.hexStringFromByteArray(kc)
338                + ":sres:" + NativeUtil.hexStringFromByteArray(sres)
339                + ":kc:" + NativeUtil.hexStringFromByteArray(kc)
340                + ":sres:" + NativeUtil.hexStringFromByteArray(sres);
341
342        doAnswer(new AnswerWithArguments() {
343            public SupplicantStatus answer(
344                    ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
345                    throws RemoteException {
346                assertEquals(2, params.size());
347                assertArrayEquals(kc, params.get(0).kc);
348                assertArrayEquals(sres, params.get(0).sres);
349                assertArrayEquals(kc, params.get(1).kc);
350                assertArrayEquals(sres, params.get(1).sres);
351                return mStatusSuccess;
352            }
353        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
354
355        assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
356    }
357
358    /**
359     * Tests the parsing of GSM auth response parameters.
360     */
361    @Test
362    public void testSendNetworkEapSimGsmAuthResponseWith3KcSresPair() throws Exception {
363        final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
364        final byte[] sres1 = new byte[]{0x12, 0x10, 0x32, 0x23};
365        final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
366        final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
367        final byte[] kc3 = new byte[]{0x25, 0x34, 0x12, 0x14, 0x45, 0x10, 0x34, 0x12};
368        final byte[] sres3 = new byte[]{0x42, 0x23, 0x22, 0x23};
369        // Send 3 kc/sres pair for this request.
370        String paramsStr = ":kc:" + NativeUtil.hexStringFromByteArray(kc1)
371                + ":sres:" + NativeUtil.hexStringFromByteArray(sres1)
372                + ":kc:" + NativeUtil.hexStringFromByteArray(kc2)
373                + ":sres:" + NativeUtil.hexStringFromByteArray(sres2)
374                + ":kc:" + NativeUtil.hexStringFromByteArray(kc3)
375                + ":sres:" + NativeUtil.hexStringFromByteArray(sres3);
376
377        doAnswer(new AnswerWithArguments() {
378            public SupplicantStatus answer(
379                    ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
380                    throws RemoteException {
381                assertEquals(3, params.size());
382                assertArrayEquals(kc1, params.get(0).kc);
383                assertArrayEquals(sres1, params.get(0).sres);
384                assertArrayEquals(kc2, params.get(1).kc);
385                assertArrayEquals(sres2, params.get(1).sres);
386                assertArrayEquals(kc3, params.get(2).kc);
387                assertArrayEquals(sres3, params.get(2).sres);
388                return mStatusSuccess;
389            }
390        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
391
392        assertTrue(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
393    }
394
395    /**
396     * Tests the parsing of invalid GSM auth response parameters (invalid kc & sres lengths).
397     */
398    @Test
399    public void testSendInvalidKcSresLenNetworkEapSimGsmAuthResponse() throws Exception {
400        final byte[] kc1 = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34};
401        final byte[] sres1 = new byte[]{0x12, 0x10, 0x23};
402        final byte[] kc2 = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
403        final byte[] sres2 = new byte[]{0x12, 0x23, 0x12, 0x23};
404        // Send 2 kc/sres pair for this request.
405        String paramsStr = ":kc:" + NativeUtil.hexStringFromByteArray(kc1)
406                + ":sres:" + NativeUtil.hexStringFromByteArray(sres1)
407                + ":kc:" + NativeUtil.hexStringFromByteArray(kc2)
408                + ":sres:" + NativeUtil.hexStringFromByteArray(sres2);
409
410        doAnswer(new AnswerWithArguments() {
411            public SupplicantStatus answer(
412                    ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
413                    throws RemoteException {
414                return mStatusSuccess;
415            }
416        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
417
418        assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
419    }
420
421    /**
422     * Tests the parsing of invalid GSM auth response parameters (invalid number of kc/sres pairs).
423     */
424    @Test
425    public void testSendInvalidKcSresPairNumNetworkEapSimGsmAuthResponse() throws Exception {
426        final byte[] kc = new byte[]{0x45, 0x34, 0x12, 0x34, 0x45, 0x10, 0x34, 0x12};
427        final byte[] sres = new byte[]{0x12, 0x23, 0x12, 0x23};
428        // Send 1 kc/sres pair for this request.
429        String paramsStr = ":kc:" + NativeUtil.hexStringFromByteArray(kc)
430                + ":sres:" + NativeUtil.hexStringFromByteArray(sres);
431
432        doAnswer(new AnswerWithArguments() {
433            public SupplicantStatus answer(
434                    ArrayList<ISupplicantStaNetwork.NetworkResponseEapSimGsmAuthParams> params)
435                    throws RemoteException {
436                return mStatusSuccess;
437            }
438        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimGsmAuthResponse(any(ArrayList.class));
439
440        assertFalse(mSupplicantNetwork.sendNetworkEapSimGsmAuthResponse(paramsStr));
441    }
442
443    /**
444     * Tests the parsing of UMTS auth response parameters.
445     */
446    @Test
447    public void testSendNetworkEapSimUmtsAuthResponse() throws Exception {
448        final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
449                0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
450        final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
451                0x33, 0x23, 0x34, 0x10, 0x40, 0x34};
452        final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
453        String paramsStr = ":ik:" + NativeUtil.hexStringFromByteArray(ik)
454                + ":ck:" + NativeUtil.hexStringFromByteArray(ck)
455                + ":res:" + NativeUtil.hexStringFromByteArray(res);
456
457        doAnswer(new AnswerWithArguments() {
458            public SupplicantStatus answer(
459                    ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
460                    throws RemoteException {
461                assertArrayEquals(ik, params.ik);
462                assertArrayEquals(ck, params.ck);
463                // Convert to arraylist before comparison.
464                ArrayList<Byte> resList = new ArrayList<>();
465                for (byte b : res) {
466                    resList.add(b);
467                }
468                assertEquals(resList, params.res);
469                return mStatusSuccess;
470            }
471        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
472                any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
473
474        assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
475    }
476
477    /**
478     * Tests the parsing of invalid UMTS auth response parameters (invalid ik, ck lengths).
479     */
480    @Test
481    public void testSendInvalidNetworkEapSimUmtsAuthResponse() throws Exception {
482        final byte[] ik = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12};
483        final byte[] ck = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
484                0x33, 0x23, 0x34, 0x10, 0x40};
485        final byte[] res = new byte[]{0x12, 0x10, 0x32, 0x23, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34};
486        String paramsStr = ":ik:" + NativeUtil.hexStringFromByteArray(ik)
487                + ":ck:" + NativeUtil.hexStringFromByteArray(ck)
488                + ":res:" + NativeUtil.hexStringFromByteArray(res);
489
490        doAnswer(new AnswerWithArguments() {
491            public SupplicantStatus answer(
492                    ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams params)
493                    throws RemoteException {
494                return mStatusSuccess;
495            }
496        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAuthResponse(
497                any(ISupplicantStaNetwork.NetworkResponseEapSimUmtsAuthParams.class));
498
499        assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAuthResponse(paramsStr));
500    }
501
502    /**
503     * Tests the parsing of UMTS auts response parameters.
504     */
505    @Test
506    public void testSendNetworkEapSimUmtsAutsResponse() throws Exception {
507        final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23, 0x34,
508                0x33, 0x23, 0x34, 0x10};
509        String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
510
511        doAnswer(new AnswerWithArguments() {
512            public SupplicantStatus answer(byte[] params)
513                    throws RemoteException {
514                assertArrayEquals(auts, params);
515                return mStatusSuccess;
516            }
517        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
518
519        assertTrue(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
520    }
521
522    /**
523     * Tests the parsing of invalid UMTS auts response parameters (invalid auts length).
524     */
525    @Test
526    public void testSendInvalidNetworkEapSimUmtsAutsResponse() throws Exception {
527        final byte[] auts = new byte[]{0x45, 0x45, 0x32, 0x34, 0x45, 0x10, 0x34, 0x12, 0x23};
528        String paramsStr = ":" + NativeUtil.hexStringFromByteArray(auts);
529
530        doAnswer(new AnswerWithArguments() {
531            public SupplicantStatus answer(byte[] params)
532                    throws RemoteException {
533                assertArrayEquals(auts, params);
534                return mStatusSuccess;
535            }
536        }).when(mISupplicantStaNetworkMock).sendNetworkEapSimUmtsAutsResponse(any(byte[].class));
537
538        assertFalse(mSupplicantNetwork.sendNetworkEapSimUmtsAutsResponse(paramsStr));
539    }
540
541    /**
542     * Tests the parsing of identity string.
543     */
544    @Test
545    public void testSendNetworkEapIdentityResponse() throws Exception {
546        final String identityStr = "test@test.com";
547
548        doAnswer(new AnswerWithArguments() {
549            public SupplicantStatus answer(ArrayList<Byte> identity)
550                    throws RemoteException {
551                assertEquals(identityStr, NativeUtil.stringFromByteArrayList(identity));
552                return mStatusSuccess;
553            }
554        }).when(mISupplicantStaNetworkMock).sendNetworkEapIdentityResponse(any(ArrayList.class));
555
556        assertTrue(mSupplicantNetwork.sendNetworkEapIdentityResponse(identityStr));
557    }
558
559    /**
560     * Tests the addition of FT flags when the device supports it.
561     */
562    @Test
563    public void testAddFtPskFlags() throws Exception {
564        mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
565        createSupplicantStaNetwork();
566
567        WifiConfiguration config = WifiConfigurationTestUtil.createPskNetwork();
568        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
569
570        // Check the supplicant variables to ensure that we have added the FT flags.
571        assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_PSK)
572                == ISupplicantStaNetwork.KeyMgmtMask.FT_PSK);
573
574        WifiConfiguration loadConfig = new WifiConfiguration();
575        Map<String, String> networkExtras = new HashMap<>();
576        assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
577        // The FT flags should be stripped out when reading it back.
578        WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
579    }
580
581    /**
582     * Tests the addition of FT flags when the device supports it.
583     */
584    @Test
585    public void testAddFtEapFlags() throws Exception {
586        mResources.setBoolean(R.bool.config_wifi_fast_bss_transition_enabled, true);
587        createSupplicantStaNetwork();
588
589        WifiConfiguration config = WifiConfigurationTestUtil.createEapNetwork();
590        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
591
592        // Check the supplicant variables to ensure that we have added the FT flags.
593        assertTrue((mSupplicantVariables.keyMgmtMask & ISupplicantStaNetwork.KeyMgmtMask.FT_EAP)
594                == ISupplicantStaNetwork.KeyMgmtMask.FT_EAP);
595
596        WifiConfiguration loadConfig = new WifiConfiguration();
597        Map<String, String> networkExtras = new HashMap<>();
598        assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
599        // The FT flags should be stripped out when reading it back.
600        WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
601    }
602
603    private void testWifiConfigurationSaveLoad(WifiConfiguration config) {
604        assertTrue(mSupplicantNetwork.saveWifiConfiguration(config));
605        WifiConfiguration loadConfig = new WifiConfiguration();
606        Map<String, String> networkExtras = new HashMap<>();
607        assertTrue(mSupplicantNetwork.loadWifiConfiguration(loadConfig, networkExtras));
608        WifiConfigurationTestUtil.assertConfigurationEqualForSupplicant(config, loadConfig);
609        assertEquals(config.configKey(),
610                networkExtras.get(SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY));
611        assertEquals(
612                config.creatorUid,
613                Integer.parseInt(networkExtras.get(
614                        SupplicantStaNetworkHal.ID_STRING_KEY_CREATOR_UID)));
615        // There is no getter for this one, so check the supplicant variable.
616        if (!TextUtils.isEmpty(config.updateIdentifier)) {
617            assertEquals(Integer.parseInt(config.updateIdentifier),
618                    mSupplicantVariables.updateIdentifier);
619        }
620        // There is no getter for this one, so check the supplicant variable.
621        String oppKeyCaching =
622                config.enterpriseConfig.getFieldValue(WifiEnterpriseConfig.OPP_KEY_CACHING);
623        if (!TextUtils.isEmpty(oppKeyCaching)) {
624            assertEquals(
625                    Integer.parseInt(oppKeyCaching) == 1 ? true : false,
626                    mSupplicantVariables.eapProactiveKeyCaching);
627        }
628    }
629
630    /**
631     * Tests the retrieval of WPS NFC token.
632     */
633    @Test
634    public void testGetWpsNfcConfigurationToken() throws Exception {
635        final ArrayList<Byte> token = new ArrayList<>();
636        token.add(Byte.valueOf((byte) 0x45));
637        token.add(Byte.valueOf((byte) 0x34));
638
639        doAnswer(new AnswerWithArguments() {
640            public void answer(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback cb)
641                    throws RemoteException {
642                cb.onValues(mStatusSuccess, token);
643            }
644        }).when(mISupplicantStaNetworkMock)
645                .getWpsNfcConfigurationToken(
646                        any(ISupplicantStaNetwork.getWpsNfcConfigurationTokenCallback.class));
647
648        assertEquals("4534", mSupplicantNetwork.getWpsNfcConfigurationToken());
649    }
650
651    /**
652     * Sets up the HIDL interface mock with all the setters/getter values.
653     * Note: This only sets up the mock to return success on all methods.
654     */
655    private void setupISupplicantNetworkMock() throws Exception {
656        /** SSID */
657        doAnswer(new AnswerWithArguments() {
658            public SupplicantStatus answer(ArrayList<Byte> ssid) throws RemoteException {
659                mSupplicantVariables.ssid = ssid;
660                return mStatusSuccess;
661            }
662        }).when(mISupplicantStaNetworkMock).setSsid(any(ArrayList.class));
663        doAnswer(new AnswerWithArguments() {
664            public void answer(ISupplicantStaNetwork.getSsidCallback cb) throws RemoteException {
665                cb.onValues(mStatusSuccess, mSupplicantVariables.ssid);
666            }
667        }).when(mISupplicantStaNetworkMock)
668                .getSsid(any(ISupplicantStaNetwork.getSsidCallback.class));
669
670        /** Network Id */
671        doAnswer(new AnswerWithArguments() {
672            public void answer(ISupplicantNetwork.getIdCallback cb) throws RemoteException {
673                cb.onValues(mStatusSuccess, mSupplicantVariables.networkId);
674            }
675        }).when(mISupplicantStaNetworkMock).getId(any(ISupplicantNetwork.getIdCallback.class));
676
677        /** BSSID */
678        doAnswer(new AnswerWithArguments() {
679            public SupplicantStatus answer(byte[] bssid) throws RemoteException {
680                mSupplicantVariables.bssid = bssid;
681                return mStatusSuccess;
682            }
683        }).when(mISupplicantStaNetworkMock).setBssid(any(byte[].class));
684        doAnswer(new AnswerWithArguments() {
685            public void answer(ISupplicantStaNetwork.getBssidCallback cb) throws RemoteException {
686                cb.onValues(mStatusSuccess, mSupplicantVariables.bssid);
687            }
688        }).when(mISupplicantStaNetworkMock)
689                .getBssid(any(ISupplicantStaNetwork.getBssidCallback.class));
690
691        /** Scan SSID (Is Hidden Network?) */
692        doAnswer(new AnswerWithArguments() {
693            public SupplicantStatus answer(boolean enable) throws RemoteException {
694                mSupplicantVariables.scanSsid = enable;
695                return mStatusSuccess;
696            }
697        }).when(mISupplicantStaNetworkMock).setScanSsid(any(boolean.class));
698        doAnswer(new AnswerWithArguments() {
699            public void answer(ISupplicantStaNetwork.getScanSsidCallback cb)
700                    throws RemoteException {
701                cb.onValues(mStatusSuccess, mSupplicantVariables.scanSsid);
702            }
703        }).when(mISupplicantStaNetworkMock)
704                .getScanSsid(any(ISupplicantStaNetwork.getScanSsidCallback.class));
705
706        /** Require PMF*/
707        doAnswer(new AnswerWithArguments() {
708            public SupplicantStatus answer(boolean enable) throws RemoteException {
709                mSupplicantVariables.requirePmf = enable;
710                return mStatusSuccess;
711            }
712        }).when(mISupplicantStaNetworkMock).setRequirePmf(any(boolean.class));
713        doAnswer(new AnswerWithArguments() {
714            public void answer(ISupplicantStaNetwork.getRequirePmfCallback cb)
715                    throws RemoteException {
716                cb.onValues(mStatusSuccess, mSupplicantVariables.requirePmf);
717            }
718        }).when(mISupplicantStaNetworkMock)
719                .getRequirePmf(any(ISupplicantStaNetwork.getRequirePmfCallback.class));
720
721        /** PSK pass phrase*/
722        doAnswer(new AnswerWithArguments() {
723            public SupplicantStatus answer(String pskPassphrase) throws RemoteException {
724                mSupplicantVariables.pskPassphrase = pskPassphrase;
725                return mStatusSuccess;
726            }
727        }).when(mISupplicantStaNetworkMock).setPskPassphrase(any(String.class));
728        doAnswer(new AnswerWithArguments() {
729            public void answer(ISupplicantStaNetwork.getPskPassphraseCallback cb)
730                    throws RemoteException {
731                cb.onValues(mStatusSuccess, mSupplicantVariables.pskPassphrase);
732            }
733        }).when(mISupplicantStaNetworkMock)
734                .getPskPassphrase(any(ISupplicantStaNetwork.getPskPassphraseCallback.class));
735
736        /** WEP keys **/
737        doAnswer(new AnswerWithArguments() {
738            public SupplicantStatus answer(int keyIdx, ArrayList<Byte> key) throws RemoteException {
739                mSupplicantVariables.wepKey[keyIdx] = key;
740                return mStatusSuccess;
741            }
742        }).when(mISupplicantStaNetworkMock).setWepKey(any(int.class), any(ArrayList.class));
743        doAnswer(new AnswerWithArguments() {
744            public void answer(int keyIdx, ISupplicantStaNetwork.getWepKeyCallback cb)
745                    throws RemoteException {
746                cb.onValues(mStatusSuccess, mSupplicantVariables.wepKey[keyIdx]);
747            }
748        }).when(mISupplicantStaNetworkMock)
749                .getWepKey(any(int.class), any(ISupplicantStaNetwork.getWepKeyCallback.class));
750
751        doAnswer(new AnswerWithArguments() {
752            public SupplicantStatus answer(int keyIdx) throws RemoteException {
753                mSupplicantVariables.wepTxKeyIdx = keyIdx;
754                return mStatusSuccess;
755            }
756        }).when(mISupplicantStaNetworkMock).setWepTxKeyIdx(any(int.class));
757        doAnswer(new AnswerWithArguments() {
758            public void answer(ISupplicantStaNetwork.getWepTxKeyIdxCallback cb)
759                    throws RemoteException {
760                cb.onValues(mStatusSuccess, mSupplicantVariables.wepTxKeyIdx);
761            }
762        }).when(mISupplicantStaNetworkMock)
763                .getWepTxKeyIdx(any(ISupplicantStaNetwork.getWepTxKeyIdxCallback.class));
764
765        /** allowedKeyManagement */
766        doAnswer(new AnswerWithArguments() {
767            public SupplicantStatus answer(int mask) throws RemoteException {
768                mSupplicantVariables.keyMgmtMask = mask;
769                return mStatusSuccess;
770            }
771        }).when(mISupplicantStaNetworkMock).setKeyMgmt(any(int.class));
772        doAnswer(new AnswerWithArguments() {
773            public void answer(ISupplicantStaNetwork.getKeyMgmtCallback cb) throws RemoteException {
774                cb.onValues(mStatusSuccess, mSupplicantVariables.keyMgmtMask);
775            }
776        }).when(mISupplicantStaNetworkMock)
777                .getKeyMgmt(any(ISupplicantStaNetwork.getKeyMgmtCallback.class));
778
779        /** allowedProtocols */
780        doAnswer(new AnswerWithArguments() {
781            public SupplicantStatus answer(int mask) throws RemoteException {
782                mSupplicantVariables.protoMask = mask;
783                return mStatusSuccess;
784            }
785        }).when(mISupplicantStaNetworkMock).setProto(any(int.class));
786        doAnswer(new AnswerWithArguments() {
787            public void answer(ISupplicantStaNetwork.getProtoCallback cb) throws RemoteException {
788                cb.onValues(mStatusSuccess, mSupplicantVariables.protoMask);
789            }
790        }).when(mISupplicantStaNetworkMock)
791                .getProto(any(ISupplicantStaNetwork.getProtoCallback.class));
792
793        /** allowedAuthAlgorithms */
794        doAnswer(new AnswerWithArguments() {
795            public SupplicantStatus answer(int mask) throws RemoteException {
796                mSupplicantVariables.authAlgMask = mask;
797                return mStatusSuccess;
798            }
799        }).when(mISupplicantStaNetworkMock).setAuthAlg(any(int.class));
800        doAnswer(new AnswerWithArguments() {
801            public void answer(ISupplicantStaNetwork.getAuthAlgCallback cb) throws RemoteException {
802                cb.onValues(mStatusSuccess, mSupplicantVariables.authAlgMask);
803            }
804        }).when(mISupplicantStaNetworkMock)
805                .getAuthAlg(any(ISupplicantStaNetwork.getAuthAlgCallback.class));
806
807        /** allowedGroupCiphers */
808        doAnswer(new AnswerWithArguments() {
809            public SupplicantStatus answer(int mask) throws RemoteException {
810                mSupplicantVariables.groupCipherMask = mask;
811                return mStatusSuccess;
812            }
813        }).when(mISupplicantStaNetworkMock).setGroupCipher(any(int.class));
814        doAnswer(new AnswerWithArguments() {
815            public void answer(ISupplicantStaNetwork.getGroupCipherCallback cb)
816                    throws RemoteException {
817                cb.onValues(mStatusSuccess, mSupplicantVariables.groupCipherMask);
818            }
819        }).when(mISupplicantStaNetworkMock)
820                .getGroupCipher(any(ISupplicantStaNetwork.getGroupCipherCallback.class));
821
822        /** allowedPairwiseCiphers */
823        doAnswer(new AnswerWithArguments() {
824            public SupplicantStatus answer(int mask) throws RemoteException {
825                mSupplicantVariables.pairwiseCipherMask = mask;
826                return mStatusSuccess;
827            }
828        }).when(mISupplicantStaNetworkMock).setPairwiseCipher(any(int.class));
829        doAnswer(new AnswerWithArguments() {
830            public void answer(ISupplicantStaNetwork.getPairwiseCipherCallback cb)
831                    throws RemoteException {
832                cb.onValues(mStatusSuccess, mSupplicantVariables.pairwiseCipherMask);
833            }
834        }).when(mISupplicantStaNetworkMock)
835                .getPairwiseCipher(any(ISupplicantStaNetwork.getPairwiseCipherCallback.class));
836
837        /** metadata: idstr */
838        doAnswer(new AnswerWithArguments() {
839            public SupplicantStatus answer(String idStr) throws RemoteException {
840                mSupplicantVariables.idStr = idStr;
841                return mStatusSuccess;
842            }
843        }).when(mISupplicantStaNetworkMock).setIdStr(any(String.class));
844        doAnswer(new AnswerWithArguments() {
845            public void answer(ISupplicantStaNetwork.getIdStrCallback cb) throws RemoteException {
846                cb.onValues(mStatusSuccess, mSupplicantVariables.idStr);
847            }
848        }).when(mISupplicantStaNetworkMock)
849                .getIdStr(any(ISupplicantStaNetwork.getIdStrCallback.class));
850
851        /** UpdateIdentifier */
852        doAnswer(new AnswerWithArguments() {
853            public SupplicantStatus answer(int identifier) throws RemoteException {
854                mSupplicantVariables.updateIdentifier = identifier;
855                return mStatusSuccess;
856            }
857        }).when(mISupplicantStaNetworkMock).setUpdateIdentifier(any(int.class));
858
859        /** EAP method */
860        doAnswer(new AnswerWithArguments() {
861            public SupplicantStatus answer(int method) throws RemoteException {
862                mSupplicantVariables.eapMethod = method;
863                return mStatusSuccess;
864            }
865        }).when(mISupplicantStaNetworkMock).setEapMethod(any(int.class));
866        doAnswer(new AnswerWithArguments() {
867            public void answer(ISupplicantStaNetwork.getEapMethodCallback cb)
868                    throws RemoteException {
869                // When not set, return failure.
870                if (mSupplicantVariables.eapMethod == -1) {
871                    cb.onValues(mStatusFailure, mSupplicantVariables.eapMethod);
872                } else {
873                    cb.onValues(mStatusSuccess, mSupplicantVariables.eapMethod);
874                }
875            }
876        }).when(mISupplicantStaNetworkMock)
877                .getEapMethod(any(ISupplicantStaNetwork.getEapMethodCallback.class));
878
879        /** EAP Phase 2 method */
880        doAnswer(new AnswerWithArguments() {
881            public SupplicantStatus answer(int method) throws RemoteException {
882                mSupplicantVariables.eapPhase2Method = method;
883                return mStatusSuccess;
884            }
885        }).when(mISupplicantStaNetworkMock).setEapPhase2Method(any(int.class));
886        doAnswer(new AnswerWithArguments() {
887            public void answer(ISupplicantStaNetwork.getEapPhase2MethodCallback cb)
888                    throws RemoteException {
889                // When not set, return failure.
890                if (mSupplicantVariables.eapPhase2Method == -1) {
891                    cb.onValues(mStatusFailure, mSupplicantVariables.eapPhase2Method);
892                } else {
893                    cb.onValues(mStatusSuccess, mSupplicantVariables.eapPhase2Method);
894                }
895            }
896        }).when(mISupplicantStaNetworkMock)
897                .getEapPhase2Method(any(ISupplicantStaNetwork.getEapPhase2MethodCallback.class));
898
899        /** EAP Identity */
900        doAnswer(new AnswerWithArguments() {
901            public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
902                mSupplicantVariables.eapIdentity = identity;
903                return mStatusSuccess;
904            }
905        }).when(mISupplicantStaNetworkMock).setEapIdentity(any(ArrayList.class));
906        doAnswer(new AnswerWithArguments() {
907            public void answer(ISupplicantStaNetwork.getEapIdentityCallback cb)
908                    throws RemoteException {
909                cb.onValues(mStatusSuccess, mSupplicantVariables.eapIdentity);
910            }
911        }).when(mISupplicantStaNetworkMock)
912                .getEapIdentity(any(ISupplicantStaNetwork.getEapIdentityCallback.class));
913
914        /** EAP Anonymous Identity */
915        doAnswer(new AnswerWithArguments() {
916            public SupplicantStatus answer(ArrayList<Byte> identity) throws RemoteException {
917                mSupplicantVariables.eapAnonymousIdentity = identity;
918                return mStatusSuccess;
919            }
920        }).when(mISupplicantStaNetworkMock).setEapAnonymousIdentity(any(ArrayList.class));
921        doAnswer(new AnswerWithArguments() {
922            public void answer(ISupplicantStaNetwork.getEapAnonymousIdentityCallback cb)
923                    throws RemoteException {
924                cb.onValues(mStatusSuccess, mSupplicantVariables.eapAnonymousIdentity);
925            }
926        }).when(mISupplicantStaNetworkMock)
927                .getEapAnonymousIdentity(
928                        any(ISupplicantStaNetwork.getEapAnonymousIdentityCallback.class));
929
930        /** EAP Password */
931        doAnswer(new AnswerWithArguments() {
932            public SupplicantStatus answer(ArrayList<Byte> password) throws RemoteException {
933                mSupplicantVariables.eapPassword = password;
934                return mStatusSuccess;
935            }
936        }).when(mISupplicantStaNetworkMock).setEapPassword(any(ArrayList.class));
937        doAnswer(new AnswerWithArguments() {
938            public void answer(ISupplicantStaNetwork.getEapPasswordCallback cb)
939                    throws RemoteException {
940                cb.onValues(mStatusSuccess, mSupplicantVariables.eapPassword);
941            }
942        }).when(mISupplicantStaNetworkMock)
943                .getEapPassword(any(ISupplicantStaNetwork.getEapPasswordCallback.class));
944
945        /** EAP Client Cert */
946        doAnswer(new AnswerWithArguments() {
947            public SupplicantStatus answer(String cert) throws RemoteException {
948                mSupplicantVariables.eapClientCert = cert;
949                return mStatusSuccess;
950            }
951        }).when(mISupplicantStaNetworkMock).setEapClientCert(any(String.class));
952        doAnswer(new AnswerWithArguments() {
953            public void answer(ISupplicantStaNetwork.getEapClientCertCallback cb)
954                    throws RemoteException {
955                cb.onValues(mStatusSuccess, mSupplicantVariables.eapClientCert);
956            }
957        }).when(mISupplicantStaNetworkMock)
958                .getEapClientCert(any(ISupplicantStaNetwork.getEapClientCertCallback.class));
959
960        /** EAP CA Cert */
961        doAnswer(new AnswerWithArguments() {
962            public SupplicantStatus answer(String cert) throws RemoteException {
963                mSupplicantVariables.eapCACert = cert;
964                return mStatusSuccess;
965            }
966        }).when(mISupplicantStaNetworkMock).setEapCACert(any(String.class));
967        doAnswer(new AnswerWithArguments() {
968            public void answer(ISupplicantStaNetwork.getEapCACertCallback cb)
969                    throws RemoteException {
970                cb.onValues(mStatusSuccess, mSupplicantVariables.eapCACert);
971            }
972        }).when(mISupplicantStaNetworkMock)
973                .getEapCACert(any(ISupplicantStaNetwork.getEapCACertCallback.class));
974
975        /** EAP Subject Match */
976        doAnswer(new AnswerWithArguments() {
977            public SupplicantStatus answer(String match) throws RemoteException {
978                mSupplicantVariables.eapSubjectMatch = match;
979                return mStatusSuccess;
980            }
981        }).when(mISupplicantStaNetworkMock).setEapSubjectMatch(any(String.class));
982        doAnswer(new AnswerWithArguments() {
983            public void answer(ISupplicantStaNetwork.getEapSubjectMatchCallback cb)
984                    throws RemoteException {
985                cb.onValues(mStatusSuccess, mSupplicantVariables.eapSubjectMatch);
986            }
987        }).when(mISupplicantStaNetworkMock)
988                .getEapSubjectMatch(any(ISupplicantStaNetwork.getEapSubjectMatchCallback.class));
989
990        /** EAP Engine */
991        doAnswer(new AnswerWithArguments() {
992            public SupplicantStatus answer(boolean enable) throws RemoteException {
993                mSupplicantVariables.eapEngine = enable;
994                return mStatusSuccess;
995            }
996        }).when(mISupplicantStaNetworkMock).setEapEngine(any(boolean.class));
997        doAnswer(new AnswerWithArguments() {
998            public void answer(ISupplicantStaNetwork.getEapEngineCallback cb)
999                    throws RemoteException {
1000                cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngine);
1001            }
1002        }).when(mISupplicantStaNetworkMock)
1003                .getEapEngine(any(ISupplicantStaNetwork.getEapEngineCallback.class));
1004
1005        /** EAP Engine ID */
1006        doAnswer(new AnswerWithArguments() {
1007            public SupplicantStatus answer(String id) throws RemoteException {
1008                mSupplicantVariables.eapEngineID = id;
1009                return mStatusSuccess;
1010            }
1011        }).when(mISupplicantStaNetworkMock).setEapEngineID(any(String.class));
1012        doAnswer(new AnswerWithArguments() {
1013            public void answer(ISupplicantStaNetwork.getEapEngineIDCallback cb)
1014                    throws RemoteException {
1015                cb.onValues(mStatusSuccess, mSupplicantVariables.eapEngineID);
1016            }
1017        }).when(mISupplicantStaNetworkMock)
1018                .getEapEngineID(any(ISupplicantStaNetwork.getEapEngineIDCallback.class));
1019
1020        /** EAP Private Key */
1021        doAnswer(new AnswerWithArguments() {
1022            public SupplicantStatus answer(String key) throws RemoteException {
1023                mSupplicantVariables.eapPrivateKey = key;
1024                return mStatusSuccess;
1025            }
1026        }).when(mISupplicantStaNetworkMock).setEapPrivateKey(any(String.class));
1027        doAnswer(new AnswerWithArguments() {
1028            public void answer(ISupplicantStaNetwork.getEapPrivateKeyCallback cb)
1029                    throws RemoteException {
1030                cb.onValues(mStatusSuccess, mSupplicantVariables.eapPrivateKey);
1031            }
1032        }).when(mISupplicantStaNetworkMock)
1033                .getEapPrivateKey(any(ISupplicantStaNetwork.getEapPrivateKeyCallback.class));
1034
1035        /** EAP Alt Subject Match */
1036        doAnswer(new AnswerWithArguments() {
1037            public SupplicantStatus answer(String match) throws RemoteException {
1038                mSupplicantVariables.eapAltSubjectMatch = match;
1039                return mStatusSuccess;
1040            }
1041        }).when(mISupplicantStaNetworkMock).setEapAltSubjectMatch(any(String.class));
1042        doAnswer(new AnswerWithArguments() {
1043            public void answer(ISupplicantStaNetwork.getEapAltSubjectMatchCallback cb)
1044                    throws RemoteException {
1045                cb.onValues(mStatusSuccess, mSupplicantVariables.eapAltSubjectMatch);
1046            }
1047        }).when(mISupplicantStaNetworkMock)
1048                .getEapAltSubjectMatch(
1049                        any(ISupplicantStaNetwork.getEapAltSubjectMatchCallback.class));
1050
1051        /** EAP Domain Suffix Match */
1052        doAnswer(new AnswerWithArguments() {
1053            public SupplicantStatus answer(String match) throws RemoteException {
1054                mSupplicantVariables.eapDomainSuffixMatch = match;
1055                return mStatusSuccess;
1056            }
1057        }).when(mISupplicantStaNetworkMock).setEapDomainSuffixMatch(any(String.class));
1058        doAnswer(new AnswerWithArguments() {
1059            public void answer(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback cb)
1060                    throws RemoteException {
1061                cb.onValues(mStatusSuccess, mSupplicantVariables.eapDomainSuffixMatch);
1062            }
1063        }).when(mISupplicantStaNetworkMock)
1064                .getEapDomainSuffixMatch(
1065                        any(ISupplicantStaNetwork.getEapDomainSuffixMatchCallback.class));
1066
1067        /** EAP CA Path*/
1068        doAnswer(new AnswerWithArguments() {
1069            public SupplicantStatus answer(String path) throws RemoteException {
1070                mSupplicantVariables.eapCAPath = path;
1071                return mStatusSuccess;
1072            }
1073        }).when(mISupplicantStaNetworkMock).setEapCAPath(any(String.class));
1074        doAnswer(new AnswerWithArguments() {
1075            public void answer(ISupplicantStaNetwork.getEapCAPathCallback cb)
1076                    throws RemoteException {
1077                cb.onValues(mStatusSuccess, mSupplicantVariables.eapCAPath);
1078            }
1079        }).when(mISupplicantStaNetworkMock)
1080                .getEapCAPath(any(ISupplicantStaNetwork.getEapCAPathCallback.class));
1081
1082        /** EAP Proactive Key Caching */
1083        doAnswer(new AnswerWithArguments() {
1084            public SupplicantStatus answer(boolean enable) throws RemoteException {
1085                mSupplicantVariables.eapProactiveKeyCaching = enable;
1086                return mStatusSuccess;
1087            }
1088        }).when(mISupplicantStaNetworkMock).setProactiveKeyCaching(any(boolean.class));
1089    }
1090
1091    private SupplicantStatus createSupplicantStatus(int code) {
1092        SupplicantStatus status = new SupplicantStatus();
1093        status.code = code;
1094        return status;
1095    }
1096
1097    /**
1098     * Need this for tests which wants to manipulate context before creating the instance.
1099     */
1100    private void createSupplicantStaNetwork() {
1101        mSupplicantNetwork =
1102                new SupplicantStaNetworkHal(mISupplicantStaNetworkMock, mContext, mWifiMonitor);
1103    }
1104
1105    // Private class to to store/inspect values set via the HIDL mock.
1106    private class SupplicantNetworkVariables {
1107        public ArrayList<Byte> ssid;
1108        public int networkId;
1109        public byte[/* 6 */] bssid;
1110        public int keyMgmtMask;
1111        public int protoMask;
1112        public int authAlgMask;
1113        public int groupCipherMask;
1114        public int pairwiseCipherMask;
1115        public boolean scanSsid;
1116        public boolean requirePmf;
1117        public String idStr;
1118        public int updateIdentifier;
1119        public String pskPassphrase;
1120        public ArrayList<Byte>[] wepKey = new ArrayList[4];
1121        public int wepTxKeyIdx;
1122        public int eapMethod = -1;
1123        public int eapPhase2Method = -1;
1124        public ArrayList<Byte> eapIdentity;
1125        public ArrayList<Byte> eapAnonymousIdentity;
1126        public ArrayList<Byte> eapPassword;
1127        public String eapCACert;
1128        public String eapCAPath;
1129        public String eapClientCert;
1130        public String eapPrivateKey;
1131        public String eapSubjectMatch;
1132        public String eapAltSubjectMatch;
1133        public boolean eapEngine;
1134        public String eapEngineID;
1135        public String eapDomainSuffixMatch;
1136        public boolean eapProactiveKeyCaching;
1137    }
1138}
1139